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

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

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 30;
vector<P> G[N];

int dp[N][2];

int dfs(int v, int p, int back) {
  int &res = dp[v][back];
  if (res >= 0)
    return res;

  if (G[v].size() == 1 && G[v][0].first == p)
    return -1;
  if (back) {
    res = 0;
    for (auto &e : G[v]) {
      if (e.first == p)
        continue;
      int ret = dfs(e.first, v, 1);
      if (ret < 0)
        continue;
      res += ret + e.second * 2;
    }
    return res;
  }
  int sum = dfs(v, p, true);
  res = sum;
  for (auto &e : G[v]) {
    if (e.first == p)
      continue;
    int ret = dfs(e.first, v, 1);
    if (ret < 0)
      continue;
    int ret2 = dfs(e.first, v, 0);
    res = min(res, sum - e.second - ret + ret2);
  }

  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, n) G[i].clear();
    MEMSET(dp, -1);
    rep(i, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      --a, --b;
      G[a].push_back(make_pair(b, t));
      G[b].push_back(make_pair(a, t));
    }
    cout << dfs(0, -1, 0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 30;
vector<P> G[N];

int dp[N][2];

int dfs(int v, int p, int back) {
  int &res = dp[v][back];
  if (res >= 0)
    return res;

  if (G[v].size() == 1 && G[v][0].first == p)
    return -1;
  if (back) {
    res = 0;
    for (auto &e : G[v]) {
      if (e.first == p)
        continue;
      int ret = dfs(e.first, v, 1);
      if (ret < 0)
        continue;
      res += ret + e.second * 2;
    }
    return res;
  }
  int sum = dfs(v, p, true);
  res = sum;
  for (auto &e : G[v]) {
    if (e.first == p)
      continue;
    int ret = dfs(e.first, v, 1);
    if (ret < 0)
      continue;
    int ret2 = dfs(e.first, v, 0);
    res = min(res, sum - e.second - ret + ret2);
  }

  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    MEMSET(dp, -1);
    rep(i, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      --a, --b;
      G[a].push_back(make_pair(b, t));
      G[b].push_back(make_pair(a, t));
    }
    cout << dfs(0, -1, 0) << endl;
  }

  return 0;
}