Problem p03754 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

vector<int> G[MAX_N];
double dp[MAX_N];
double dp2[MAX_N];
double ans[MAX_N];
int eda[MAX_N];

void dfs(int u, int p) {
  int cnt = 0;
  double res = 0;
  if ((int)G[u].size() == 1 && G[u][0] == p) {
    dp[u] = 0;
    return;
  }
  rep(i, G[u].size()) {
    if (G[u][i] != p) {
      cnt++;
      dfs(G[u][i], u);
      res += dp[G[u][i]];
    }
  }
  eda[u] = cnt;
  dp[u] = res / cnt + 1;
}

void dfs2(int u, int p) {
  rep(i, G[u].size()) {
    if (G[u][i] != p) {
      if (u == 0) {
        if (eda[u] == 1) {
          dp2[G[u][i]] = 1;
        } else {
          dp2[G[u][i]] =
              (dp[u] * eda[u] - (1 + dp[G[u][i]])) / (eda[u] - 1) + 1;
        }
      } else {
        dp2[G[u][i]] = dp2[u] / eda[u] + dp[u] - (1 + dp[G[u][i]]) / eda[u] + 1;
      }
      ans[G[u][i]] = dp2[G[u][i]] / (eda[G[u][i]] + 1) +
                     dp[G[u][i]] * eda[G[u][i]] / (eda[G[u][i]] + 1);
      dfs2(G[u][i], u);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1), G[b - 1].pb(a - 1);
  }
  dfs(0, -1);
  ans[0] = dp[0];
  dp2[0] = 0;
  dfs2(0, -1);
  // rep(i,n){
  //     cout << i << " " << dp[i] << endl;
  // }
  rep(i, n) { printf("%.12lf\n", ans[i]); }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 150005;

vector<int> G[MAX_N];
double dp[MAX_N];
double dp2[MAX_N];
double ans[MAX_N];
int eda[MAX_N];

void dfs(int u, int p) {
  int cnt = 0;
  double res = 0;
  if ((int)G[u].size() == 1 && G[u][0] == p) {
    dp[u] = 0;
    return;
  }
  rep(i, G[u].size()) {
    if (G[u][i] != p) {
      cnt++;
      dfs(G[u][i], u);
      res += dp[G[u][i]];
    }
  }
  eda[u] = cnt;
  dp[u] = res / cnt + 1;
}

void dfs2(int u, int p) {
  rep(i, G[u].size()) {
    if (G[u][i] != p) {
      if (u == 0) {
        if (eda[u] == 1) {
          dp2[G[u][i]] = 1;
        } else {
          dp2[G[u][i]] =
              (dp[u] * eda[u] - (1 + dp[G[u][i]])) / (eda[u] - 1) + 1;
        }
      } else {
        dp2[G[u][i]] = dp2[u] / eda[u] + dp[u] - (1 + dp[G[u][i]]) / eda[u] + 1;
      }
      ans[G[u][i]] = dp2[G[u][i]] / (eda[G[u][i]] + 1) +
                     dp[G[u][i]] * eda[G[u][i]] / (eda[G[u][i]] + 1);
      dfs2(G[u][i], u);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a - 1].pb(b - 1), G[b - 1].pb(a - 1);
  }
  dfs(0, -1);
  ans[0] = dp[0];
  dp2[0] = 0;
  dfs2(0, -1);
  // rep(i,n){
  //     cout << i << " " << dp[i] << endl;
  // }
  rep(i, n) { printf("%.12lf\n", ans[i]); }
  return 0;
}