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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<P> G[32];
int N;

int dfs1(int cur, int prev) {
  int res = 0;
  rep(i, G[cur].size()) {
    if (G[cur][i].first == prev)
      continue;
    res += dfs1(G[cur][i].first, cur) + G[cur][i].second;
  }

  if (res == 0 && G[cur].size() == 1 && G[cur][0].first == prev)
    res -= G[cur][0].second;
  return res;
}

int dfs2(int cur, int prev) {
  int res = 0;
  rep(i, G[cur].size()) {
    if (G[cur][i].first == prev)
      continue;
    res = max(res, dfs2(G[cur][i].first, cur) + G[cur][i].second);
  }
  if (!res && G[cur].size() == 1 && G[cur][0].first == prev)
    res -= G[cur][0].second;
  return res;
}

int main() {
  while (cin >> N, N) {
    rep(i, 32) G[i].clear();
    rep(i, N - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      a--;
      b--;
      G[a].push_back(P(b, t));
      G[b].push_back(P(a, t));
    }
    cout << 2 * dfs1(0, -1) - dfs2(0, -1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<P> G[32];
int N;

int dfs1(int cur, int prev) {
  int res = 0;
  rep(i, G[cur].size()) {
    if (G[cur][i].first == prev)
      continue;
    res += dfs1(G[cur][i].first, cur) + G[cur][i].second;
  }

  if (res == 0 && G[cur].size() == 1 && G[cur][0].first == prev) {
    //    cout << cur << endl;
    res -= G[cur][0].second;
  }
  return res;
}

int dfs2(int cur, int prev) {
  int res = 0;
  rep(i, G[cur].size()) {
    if (G[cur][i].first == prev)
      continue;
    res = max(res, dfs2(G[cur][i].first, cur) + G[cur][i].second);
  }
  if (!res && G[cur].size() == 1 && G[cur][0].first == prev)
    res -= G[cur][0].second;
  return res;
}

int main() {
  while (cin >> N, N) {
    rep(i, N - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      a--;
      b--;
      G[a].push_back(P(b, t));
      G[b].push_back(P(a, t));
    }
    //    cout << dfs1(0, -1) << endl; cout << dfs2(0, -1) << endl;
    cout << 2 * dfs1(0, -1) - dfs2(0, -1) << endl;
  }
  return 0;
}