Problem p02884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll N, M;
  cin >> N >> M;
  vvc<ll> G(N);
  rep(i, M) {
    ll s, t;
    cin >> s >> t;
    s--, t--;
    G[s].eb(t);
  }

  // 頂点iからN-1までのパス長の期待値, その頂点にたどり着く確率
  vc<ld> E(N), T(N);
  repr(i, N - 1) {
    repa(to, G[i]) E[i] += E[to];
    E[i] /= G[i].size();
    E[i] += 1;
  }

  auto dfs = [&](auto &&dfs, ll now, ld p) -> void {
    T[now] += p;
    p *= 1.0L / G[now].size();
    repa(to, G[now]) { dfs(dfs, to, p); }
  };
  dfs(dfs, 0, 1.0L);

  ld ans = E[0];
  rep(now, N) {
    ll gsize = G[now].size();
    if (gsize <= 1)
      continue;
    repa(to, G[now]) {
      chmin(ans, E[0] - T[now] * (E[now] -
                                  (E[now] * gsize - E[to] - 1) / (gsize - 1)));
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll N, M;
  cin >> N >> M;
  vvc<ll> G(N);
  rep(i, M) {
    ll s, t;
    cin >> s >> t;
    s--, t--;
    G[s].eb(t);
  }

  // 頂点iからN-1までのパス長の期待値, その頂点にたどり着く確率
  vc<ld> E(N), T(N);
  repr(i, N - 1) {
    repa(to, G[i]) E[i] += E[to];
    E[i] /= G[i].size();
    E[i] += 1;
  }

  T[0] = 1;
  rep(now, N - 1) {
    ll gsize = G[now].size();
    repa(to, G[now]) T[to] += T[now] / gsize;
  }

  ld ans = E[0];
  rep(now, N) {
    ll gsize = G[now].size();
    if (gsize <= 1)
      continue;
    repa(to, G[now]) {
      chmin(ans, E[0] - T[now] * (E[now] -
                                  (E[now] * gsize - E[to] - 1) / (gsize - 1)));
    }
  }

  cout << ans << endl;
}