Problem p01907 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

double P;
int N;

struct Edge {
  int to, cost;
};

vector<vector<Edge>> G;

long double solve(int v, int pre, long double prob, long double additional) {
  long double res = prob * additional;

  for (auto e : G[v]) {
    if (e.to == pre)
      continue;
    res += (P * prob) * e.cost;
    res += solve(e.to, v, prob * P, additional);
  }

  return res;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> P >> N;
  G.resize(N);
  rep(i, N - 1) {
    int X, Y, C;
    cin >> X >> Y >> C;
    X--, Y--;
    G[X].push_back({Y, C});
    G[Y].push_back({X, C});
  }

  long double e = solve(0, -1, 1, 0);
  long double ans = solve(0, -1, 1, e);

  cout << fixed << setprecision(10) << ans << endl;
}

Error: 0
(v, pre) = (0, -1)
e.to=1
(v, pre) = (1, 0)
e.to=2
(v, pre) = (2, 1)
e.to=3
(v, pre) = (3, 2)
(v, pre) = (0, -1)
e.to=1
(v, pre) = (1, 0)
e.to=2
(v, pre) = (2, 1)
e.to=3
(v, pre) = (3, 2)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

double P;
int N;

struct Edge {
  int to, cost;
};

vector<vector<Edge>> G;

long double solve(int v, int pre, long double prob, long double additional) {
  DUMP2(v, pre);

  long double res = prob * additional;

  for (auto e : G[v]) {
    if (e.to == pre)
      continue;
    DUMP(e.to);
    res += (P * prob) * e.cost;
    res += solve(e.to, v, prob * P, additional);
  }

  return res;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> P >> N;
  G.resize(N);
  rep(i, N - 1) {
    int X, Y, C;
    cin >> X >> Y >> C;
    X--, Y--;
    G[X].push_back({Y, C});
    G[Y].push_back({X, C});
  }

  long double e = solve(0, -1, 1, 0);

  long double ans = solve(0, -1, 1, e);

  cout << fixed << setprecision(10) << ans << endl;
}