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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int d[1300200];
struct Edge {
  int u, v, w, next;
};
Edge e[1300200];
int inq[1300200];
int h[1300200];
int in[1300200];
int h2[1300200];
Edge e2[1400200];
queue<int> q;
void spfa(int s) {
  memset(d, 0x3f, sizeof(d));
  memset(inq, 0, sizeof(inq));
  d[s] = 0;
  q.push(s);
  inq[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = h[u]; i != -1; i = e[i].next) {
      int v = e[i].v;
      int w = e[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        if (inq[v] == 0) {
          q.push(v);
          inq[v] = 1;
        }
      }
    }
  }
}
void spfa2(int s) {
  memset(d, 0x3f, sizeof(d));
  memset(inq, 0, sizeof(inq));
  d[s] = 0;
  q.push(s);
  inq[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = h2[u]; i != -1; i = e2[i].next) {
      int v = e2[i].v;
      int w = e2[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        if (inq[v] == 0) {
          q.push(v);
          inq[v] = 1;
        }
      }
    }
  }
}
int main() {
  memset(h, -1, sizeof(h));
  memset(h2, -1, sizeof(h2));
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    e[i] = (Edge){u, v, w, h[u]};
    h[u] = i;
    e2[i + n - 1] = (Edge){v, u, w, h2[v]};
    h2[v] = i + n - 1;
    e2[i] = (Edge){u, v, w, h2[u]};
    h2[u] = i;
    in[v]++;
  }
  for (int i = 0; i < n; i++) {
    if (in[i] == 0) {
      spfa(i);
      break;
    }
  }

  long long maxx = -1;
  long long cur;
  for (int i = 0; i < n; i++) {
    if (d[i] > maxx && d[i] != d[n + 1]) {
      maxx = d[i];
      cur = i;
    }
  }
  int use = d[cur];
  spfa2(cur);
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (d[i] > ans) {
      ans = d[i];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int d[300200];
struct Edge {
  int u, v, w, next;
};
Edge e[300200];
int inq[300200];
int h[300200];
int in[300200];
int h2[300200];
Edge e2[400200];
queue<int> q;
void spfa(int s) {
  memset(d, 0x3f, sizeof(d));
  memset(inq, 0, sizeof(inq));
  d[s] = 0;
  q.push(s);
  inq[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = h[u]; i != -1; i = e[i].next) {
      int v = e[i].v;
      int w = e[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        if (inq[v] == 0) {
          q.push(v);
          inq[v] = 1;
        }
      }
    }
  }
}
void spfa2(int s) {
  memset(d, 0x3f, sizeof(d));
  memset(inq, 0, sizeof(inq));
  d[s] = 0;
  q.push(s);
  inq[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = h2[u]; i != -1; i = e2[i].next) {
      int v = e2[i].v;
      int w = e2[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        if (inq[v] == 0) {
          q.push(v);
          inq[v] = 1;
        }
      }
    }
  }
}
int main() {
  memset(h, -1, sizeof(h));
  memset(h2, -1, sizeof(h2));
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    e[i] = (Edge){u, v, w, h[u]};
    h[u] = i;
    e2[i + n - 1] = (Edge){v, u, w, h2[v]};
    h2[v] = i + n - 1;
    e2[i] = (Edge){u, v, w, h2[u]};
    h2[u] = i;
    in[v]++;
  }
  for (int i = 0; i < n; i++) {
    if (in[i] == 0) {
      spfa(i);
      break;
    }
  }
  long long maxx = 0;
  long long cur;
  for (int i = 0; i < n; i++) {
    if (d[i] > maxx && d[i] != d[n + 1]) {
      maxx = d[i];
      cur = i;
    }
  }
  int use = d[cur];
  spfa2(cur);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] > ans) {
      ans = d[i];
    }
  }
  cout << ans << endl;
  return 0;
}