Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int N;
vector<pair<int, int>> G[100010];
int d[100010];
int available[100010];

void dfs(int s) {
  available[s] = 0;
  for (vector<pair<int, int>>::iterator it = G[s].begin(); it != G[s].end();
       it++) {
    int v = it->first;
    int w = it->second;
    if (!available[v])
      continue;

    d[v] = d[s] + w;
    dfs(v);
  }
}

int main() {
  int N;
  cin >> N;

  REP(i, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(make_pair(t, w));
    G[t].push_back(make_pair(s, w));
  }

  REP(i, N) {
    d[i] = IINF;
    available[i] = 1;
  }
  d[0] = 0;
  dfs(0); // tekitou yatsu

  int farv = 0;
  REP(i, N) {
    if (d[i] == IINF)
      continue;
    if (d[farv] < d[i]) {
      farv = i;
    }
  }
  dump(farv);
  dump(d[farv]);
  dump(d[0]);
  dump(d[1]);
  dump(d[2]);
  dump(d[3]);

  REP(i, N) {
    d[i] = IINF;
    available[i] = 1;
  }
  d[farv] = 0;
  dfs(farv); // tekitou yatsu

  int maxv = 0;
  REP(i, N) {
    if (d[i] == IINF)
      continue;
    maxv = max(maxv, d[i]);
  }

  cout << maxv << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int N;
vector<pair<int, int>> G[100010];
int d[100010];
int available[100010];

void dfs(int s) {
  available[s] = 0;
  for (vector<pair<int, int>>::iterator it = G[s].begin(); it != G[s].end();
       it++) {
    int v = it->first;
    int w = it->second;
    if (!available[v])
      continue;

    d[v] = d[s] + w;
    dfs(v);
  }
}

int main() {
  int N;
  cin >> N;

  REP(i, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(make_pair(t, w));
    G[t].push_back(make_pair(s, w));
  }

  REP(i, N) {
    d[i] = IINF;
    available[i] = 1;
  }
  d[0] = 0;
  dfs(0); // tekitou yatsu

  int farv = 0;
  REP(i, N) {
    if (d[i] == IINF)
      continue;
    if (d[farv] < d[i]) {
      farv = i;
    }
  }
  // dump(farv);
  // dump(d[farv]);
  // dump(d[0]);
  // dump(d[1]);
  // dump(d[2]);
  // dump(d[3]);

  REP(i, N) {
    d[i] = IINF;
    available[i] = 1;
  }
  d[farv] = 0;
  dfs(farv); // tekitou yatsu

  int maxv = 0;
  REP(i, N) {
    if (d[i] == IINF)
      continue;
    maxv = max(maxv, d[i]);
  }

  cout << maxv << endl;
  return 0;
}