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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

int tbl[25][25];
int N;
int node[25];
int res;
int sum;
int maxt;

void dfs1(int curr, int parent) {
  bool found = false;

  rep(i, N) {
    if (i != curr && i != parent && tbl[curr][i]) {
      found = true;
      //			cout << curr << " is middle node." << endl;
      dfs1(i, curr);
    } // end if
  }   // end rep

  if (!found) {
    //		cout << curr << " is leaf. " << endl;
    node[curr] = 1;
  } // end if
}

void dfs2(int curr, int parent) {
  rep(i, N) {
    if (i != parent && tbl[curr][i] && !node[i]) {
      res += tbl[curr][i];
      sum += tbl[curr][i];
      //			cout << curr << " -> " << i << " sum:" << sum <<
      //endl;
      maxt = max(maxt, sum);
      dfs2(i, curr);
      res += tbl[curr][i];
      sum -= tbl[curr][i];
    } // end if
  }   // end rep
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.SR", "r", stdin );
  int n;
  while (cin >> n && n) {
    memset(tbl, 0, sizeof(tbl));
    memset(node, 0, sizeof(node));
    rep(i, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      N = max(N, max(a, b));
      a--;
      b--;
      tbl[a][b] = t;
      tbl[b][a] = t;
    } // end rep
    res = 0;
    maxt = 0;
    sum = 0;
    dfs1(0, -1);
    dfs2(0, -1);
    cout << res - maxt << endl;
  } // end loop

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

int tbl[25][25];
int N;
int node[25];
int res;

void found_leaf(int curr, int parent) {
  bool found = false;

  rep(i, N) {
    if (i != curr && i != parent && tbl[curr][i]) {
      found = true;
      //			cout << curr << " is middle node." << endl;
      found_leaf(i, curr);
    } // end if
  }   // end rep

  if (!found) {
    //		cout << curr << " is leaf. " << endl;
    node[curr] = 1;
  } // end if
}

void dfs(int curr, int parent) {

  int cnt = (int)count(node, node + N, 1);
  if (cnt == N)
    return;

  node[curr] = 1;

  rep(i, N) {
    if (i != parent && tbl[curr][i] && !node[i]) {
      res += tbl[curr][i];
      dfs(i, curr);
      cnt = (int)count(node, node + N, 1);
      if (cnt != N)
        res += tbl[curr][i];
    } // end if
  }   // end rep
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.SR", "r", stdin );
  int n;
  while (cin >> n && n) {
    memset(tbl, 0, sizeof(tbl));
    memset(node, 0, sizeof(node));
    res = 0;
    rep(i, n) {
      int a, b, t;
      cin >> a >> b >> t;
      N = max(N, max(a, b));
      a--;
      b--;
      tbl[a][b] = t;
      tbl[b][a] = t;
    } // end rep
    found_leaf(0, -1);
    dfs(0, -1);
    cout << res << endl;
  } // end loop

  return 0;
}