Problem p03754 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007

// edit
class Solve {
public:
  Int N;

  vector<vector<Int>> tree;
  vector<vector<double>> dp;
  vector<Int> pi;

  double dfs1(Int v, Int p = -1) {
    dp[v].resize(tree[v].size());
    if (tree[v].size() == 1 && p != -1) {
      return 0.0;
    }

    Int cnt = 0;
    double sum = 0.0;

    for (int i = 0; i < tree[v].size(); ++i) {
      Int u = tree[v][i];
      if (u == p) {
        pi[u] = i;
        continue;
      }

      cnt++;
      double tmp = dfs1(u, v);
      dp[v][i] = tmp;
      sum += tmp + 1.0;
    }

    return sum / cnt;
  }

  void dfs2(Int v, double res_p = 0.0, Int p = -1) {
    if (p != -1) {
      dp[v][pi[v]] = res_p;
    }
    if (tree[v].size() == 1 && p != -1) {
      return;
    }

    Int cnt = tree[v].size() - 1;
    double sum = 0.0;
    for (int i = 0; i < tree[v].size(); ++i) {
      sum += dp[v][i] + 1.0;
    }

    for (int i = 0; i < tree[v].size(); ++i) {
      Int u = tree[v][i];
      if (u == p)
        continue;
      double n_res = cnt > 0 ? (sum - dp[v][i] - 1.0) / cnt : 0;
      dfs2(u, n_res, v);
    }
  }

  void solve() {
    cin >> N;
    if (N == 1) {
      cout << 0 << endl;
      return;
    }
    tree.resize(N);
    dp.resize(N);
    pi.resize(N);
    for (int i = 1; i < N; ++i) {
      Int a, b;
      cin >> a >> b;
      a--, b--;
      tree[a].push_back(b);
      tree[b].push_back(a);
    }

    dfs1(0);
    dfs2(0);

    vector<double> ans(N);
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < dp[i].size(); ++j) {
        ans[i] += dp[i][j];
      }
      ans[i] /= dp[i].size();
      ans[i] += 1;

      cout << ans[i] << endl;
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  Solve().solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, m, n) for (int i = int(m); i < int(n); i++)
#define all(c) begin(c), end(c)

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007

// edit
class Solve {
public:
  Int N;

  vector<vector<Int>> tree;
  vector<vector<double>> dp;
  vector<Int> pi;

  double dfs1(Int v, Int p = -1) {
    dp[v].resize(tree[v].size());
    if (tree[v].size() == 1 && p != -1) {
      return 0.0;
    }

    Int cnt = 0;
    double sum = 0.0;

    for (int i = 0; i < tree[v].size(); ++i) {
      Int u = tree[v][i];
      if (u == p) {
        pi[v] = i;
        continue;
      }

      cnt++;
      double tmp = dfs1(u, v);
      dp[v][i] = tmp;
      sum += tmp + 1.0;
    }

    return sum / cnt;
  }

  void dfs2(Int v, double res_p = 0.0, Int p = -1) {
    if (p != -1) {
      dp[v][pi[v]] = res_p;
    }
    if (tree[v].size() == 1 && p != -1) {
      return;
    }

    Int cnt = tree[v].size() - 1;
    double sum = 0.0;
    for (int i = 0; i < tree[v].size(); ++i) {
      sum += dp[v][i] + 1.0;
    }

    for (int i = 0; i < tree[v].size(); ++i) {
      Int u = tree[v][i];
      if (u == p)
        continue;
      double n_res = cnt > 0 ? (sum - dp[v][i] - 1.0) / cnt : 0;
      dfs2(u, n_res, v);
    }
  }

  void solve() {
    cin >> N;
    if (N == 1) {
      cout << 0 << endl;
      return;
    }
    tree.resize(N);
    dp.resize(N);
    pi.resize(N);
    for (int i = 1; i < N; ++i) {
      Int a, b;
      cin >> a >> b;
      a--, b--;
      tree[a].push_back(b);
      tree[b].push_back(a);
    }

    dfs1(0);
    dfs2(0);

    vector<double> ans(N);
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < dp[i].size(); ++j) {
        ans[i] += dp[i][j];
      }
      ans[i] /= dp[i].size();
      ans[i] += 1;

      cout << ans[i] << endl;
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  Solve().solve();

  return 0;
}