Problem p03754 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

double memo[100010];
double ans[100010];
vector<int> G[100010];

void dfs(int idx, int par) {
  int sz = G[idx].size();
  for (auto &to : G[idx]) {
    if (to == par) {
      sz--;
      continue;
    }
    dfs(to, idx);
    memo[idx] += memo[to] + 1.0;
  }
  if (sz)
    memo[idx] /= sz;
}

void dfs2(int idx, double d_par, int par) {
  double sum = 0.0;
  int sz = G[idx].size();
  for (auto &to : G[idx]) {
    if (to == par)
      sum += d_par + 1.0;
    else
      sum += memo[to] + 1.0;
  }
  // printf("idx = %lld, d_par = %.12lf, par = %lld, sum = %.12lf\n", idx,
  // d_par, par, sum);
  ans[idx] = (sz > 0 ? sum / sz : 0.0);
  for (auto &to : G[idx]) {
    if (to == par)
      continue;
    dfs2(to, sz > 1 ? (sum - memo[to] - 1.0) / (sz - 1) : 0.0, idx);
  }
}

signed main() {
  int n;
  cin >> n;
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, -1);
  dfs2(0, 0.0, -1);
  rep(i, 0, n) printf("%.12f\n", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {
  return (!n) ? 1 : (mod_pow((x * x) % MOD, n / 2) * ((n & 1) ? x : 1)) % MOD;
}
int madd(int a, int b) { return (a + b) % MOD; }
int msub(int a, int b) { return (a - b + MOD) % MOD; }
int mmul(int a, int b) { return (a * b) % MOD; }
int minv(int a) { return mod_pow(a, MOD - 2); }
int mdiv(int a, int b) { return mmul(a, minv(b)); }

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

double memo[150010];
double ans[150010];
vector<int> G[150010];

void dfs(int idx, int par) {
  int sz = G[idx].size();
  for (auto &to : G[idx]) {
    if (to == par) {
      sz--;
      continue;
    }
    dfs(to, idx);
    memo[idx] += memo[to] + 1.0;
  }
  if (sz)
    memo[idx] /= sz;
}

void dfs2(int idx, double d_par, int par) {
  double sum = 0.0;
  int sz = G[idx].size();
  for (auto &to : G[idx]) {
    if (to == par)
      sum += d_par + 1.0;
    else
      sum += memo[to] + 1.0;
  }
  // printf("idx = %lld, d_par = %.12lf, par = %lld, sum = %.12lf\n", idx,
  // d_par, par, sum);
  ans[idx] = (sz > 0 ? sum / sz : 0.0);
  for (auto &to : G[idx]) {
    if (to == par)
      continue;
    dfs2(to, sz > 1 ? (sum - memo[to] - 1.0) / (sz - 1) : 0.0, idx);
  }
}

signed main() {
  int n;
  cin >> n;
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, -1);
  dfs2(0, 0.0, -1);
  rep(i, 0, n) printf("%.12f\n", ans[i]);
  return 0;
}