Problem p02884 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int n;
V g[610];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int m, s[610], t[610];

  cin >> n >> m;
  REP(i, m) {
    cin >> s[i] >> t[i];
    g[s[i]].push_back(t[i]);
  }

  double dp[610];
  dp[n] = 0;

  for (int i = n - 1; i >= 1; i--) {
    dp[i] = 0;
    for (int nex : g[i]) {
      dp[i] += (1 + dp[nex]) / g[i].size();
    }
  }

  double res = 1e9;

  for (int i = 1; i <= n; i++) {
    int mai = -1;
    double ma = 0;

    if (g[i].size() > 1)
      for (int nex : g[i]) {
        if (ma < dp[nex]) {
          ma = dp[nex];
          mai = nex;
        }
      }

    double dp2[610];
    dp2[n] = 0;

    for (int j = n - 1; j >= 1; j--) {
      dp2[j] = 0;
      for (int nex : g[j]) {
        if (j == i && nex == mai)
          continue;

        if (j == i && g[j].size() > 1) {
          dp2[j] += (1 + dp2[nex]) / (g[j].size() - 1);
        } else {
          dp2[j] += (1 + dp2[nex]) / (g[j].size());
        }
      }
    }

    res = min(res, dp2[1]);
  }

  cout << setprecision(30) << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int n;
V g[610];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int m, s[180010], t[180010];

  cin >> n >> m;
  REP(i, m) {
    cin >> s[i] >> t[i];
    g[s[i]].push_back(t[i]);
  }

  double dp[610];
  dp[n] = 0;

  for (int i = n - 1; i >= 1; i--) {
    dp[i] = 0;
    for (int nex : g[i]) {
      dp[i] += (1 + dp[nex]) / g[i].size();
    }
  }

  double res = 1e9;

  for (int i = 1; i <= n; i++) {
    int mai = -1;
    double ma = 0;

    if (g[i].size() > 1)
      for (int nex : g[i]) {
        if (ma < dp[nex]) {
          ma = dp[nex];
          mai = nex;
        }
      }

    double dp2[610];
    dp2[n] = 0;

    for (int j = n - 1; j >= 1; j--) {
      dp2[j] = 0;
      for (int nex : g[j]) {
        if (j == i && nex == mai)
          continue;

        if (j == i && g[j].size() > 1) {
          dp2[j] += (1 + dp2[nex]) / (g[j].size() - 1);
        } else {
          dp2[j] += (1 + dp2[nex]) / (g[j].size());
        }
      }
    }

    res = min(res, dp2[1]);
  }

  cout << setprecision(30) << res << endl;

  return 0;
}