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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> X[1 << 17], Y[1 << 17];
int dist[1 << 17], N, A, B, C, root;
queue<int> Q;
int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
    Y[B].push_back(make_pair(A, C));
  }
  for (int i = 0; i < N; i++) {
    if (Y[i].size() == 0) {
      root = i;
    }
  }
  for (int i = 0; i < N; i++) {
    dist[i] = 1145141919;
  }
  Q.push(root);
  dist[root] = 0;
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();
    for (int i = 0; i < X[a1].size(); i++) {
      int to = X[a1][i].first, a2 = X[a1][i].second;
      if (dist[to] == 1145141919) {
        dist[to] = dist[a1] + a2;
        Q.push(to);
      }
    }
  }
  int maxn = 0, maxid = 0;
  for (int i = 0; i < N; i++) {
    if (maxn < dist[i]) {
      maxn = dist[i];
      maxid = i;
    }
  }
  Q.push(maxid);
  for (int i = 0; i < N; i++) {
    dist[i] = 1145141919;
  }
  dist[maxid] = 0;
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();
    maxn = 0;
    maxid = 0;
    for (int i = 0; i < X[a1].size(); i++) {
      int to = X[a1][i].first, a2 = X[a1][i].second;
      if (dist[to] > dist[a1] + a2) {
        dist[to] = dist[a1] + a2;
        Q.push(to);
      }
    }
    for (int i = 0; i < Y[a1].size(); i++) {
      int to = Y[a1][i].first, a2 = Y[a1][i].second;
      if (dist[to] > dist[a1] + a2) {
        dist[to] = dist[a1] + a2;
        Q.push(to);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (maxn < dist[i]) {
      maxn = dist[i];
      maxid = i;
    }
  }
  cout << maxn << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> X[1 << 17], Y[1 << 17];
int dist[1 << 17], N, A, B, C, root;
queue<int> Q;
int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
    Y[B].push_back(make_pair(A, C));
  }
  for (int i = 0; i < N; i++) {
    if (Y[i].size() == 0) {
      root = i;
    }
  }
  for (int i = 0; i < N; i++) {
    dist[i] = 1145141919;
  }
  Q.push(root);
  dist[root] = 0;
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();
    for (int i = 0; i < X[a1].size(); i++) {
      int to = X[a1][i].first, a2 = X[a1][i].second;
      if (dist[to] == 1145141919) {
        dist[to] = a1 + a2;
        Q.push(to);
      }
    }
  }
  int maxn = 0, maxid = 0;
  for (int i = 0; i < N; i++) {
    if (maxn < dist[i]) {
      maxn = dist[i];
      maxid = i;
    }
  }
  Q.push(maxid);
  for (int i = 0; i < N; i++) {
    dist[i] = 1145141919;
  }
  dist[maxid] = 0;
  while (!Q.empty()) {
    int a1 = Q.front();
    Q.pop();
    for (int i = 0; i < X[a1].size(); i++) {
      int to = X[a1][i].first, a2 = X[a1][i].second;
      if (dist[to] > a1 + a2) {
        dist[to] = a1 + a2;
        Q.push(to);
      }
    }
    for (int i = 0; i < Y[a1].size(); i++) {
      int to = X[a1][i].first, a2 = X[a1][i].second;
      if (dist[to] > a1 + a2) {
        dist[to] = a1 + a2;
        Q.push(to);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (maxn < dist[i]) {
      maxn = dist[i];
      maxid = i;
    }
  }
  cout << maxn << endl;
  return 0;
}