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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define RREP(i, n) for (int(i) = (int)(n)-1; i >= 0; i--)
#define FILL(Itr, n) fill((Itr).begin(), (Itr).end(), n)
#define REMOVE(Itr, n)                                                         \\
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define UNIQUE(Itr)                                                            \\
  sort((Itr).begin(), (Itr).end());                                            \\
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;
#define MOD 1000000007

struct Edge {
  int to;
  int w;
  Edge(int _to, int _w) : to(_to), w(_w) {}
};

vector<vector<Edge>> G;
vector<int> dist;
int Root;

template <class T> struct TreeDiameter {

  TreeDiameter(int size) { G.resize(size), dist.resize(size, -1); }

  void add_edge(int a, int b, T cost) {
    G[a].push_back(Edge(b, cost));
    G[b].push_back(Edge(a, cost));
    Root = a;
  }

  void dfs(int v) {
    stack<Edge> sta;
    sta.push(Edge(v, 0));
    while (sta.size() != 0) {
      Edge et = sta.top();
      sta.pop();
      dist[et.to] = et.w;
      for (int i = 0; i < G[et.to].size(); i++) {
        if (dist[G[et.to][i].to] < 0) {
          sta.push(Edge(G[et.to][i].to, G[et.to][i].w + et.w));
        }
      }
    }
  }

  T getDiameter() {
    dfs(Root);
    int ver = -1;
    T d = -1;
    for (int i = 0; i < dist.size(); i++) {
      if (dist[i] > d)
        d = dist[ver = i];
      dist[i] = -1;
    }
    dfs(ver);
    T ret = 0;
    for (int i = 0; i < dist.size(); i++)
      ret = max(ret, dist[i]);
    return ret;
  }
};

int main() {

  TreeDiameter<int> inst(100010);
  int n;
  cin >> n;
  REP(i, n - 1) {
    int a, b, s;
    cin >> a >> b >> s;
    inst.add_edge(a, b, s);
  }

  cout << inst.getDiameter() << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define RREP(i, n) for (int(i) = (int)(n)-1; i >= 0; i--)
#define FILL(Itr, n) fill((Itr).begin(), (Itr).end(), n)
#define REMOVE(Itr, n)                                                         \\
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define UNIQUE(Itr)                                                            \\
  sort((Itr).begin(), (Itr).end());                                            \\
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;
#define MOD 1000000007

template <class T> struct TreeDiameter {
  struct Edge {
    int to;
    T w;
    Edge(int _to, T _w) : to(_to), w(_w) {}
  };

  vector<vector<Edge>> G;
  vector<T> dist;
  int Root;

  TreeDiameter(int size) : G(size), dist(size, -1) {}

  void add_edge(int a, int b, T cost) {
    G[a].push_back(Edge(b, cost));
    G[b].push_back(Edge(a, cost));
    Root = a;
  }

  void dfs(int v) {
    stack<Edge> sta;
    sta.push(Edge(v, 0));
    while (sta.size() != 0) {
      Edge et = sta.top();
      sta.pop();
      dist[et.to] = et.w;
      for (int i = 0; i < G[et.to].size(); i++) {
        if (dist[G[et.to][i].to] < 0) {
          sta.push(Edge(G[et.to][i].to, G[et.to][i].w + et.w));
        }
      }
    }
  }

  T getDiameter() {
    dfs(Root);
    int ver = -1;
    T d = -1;
    for (int i = 0; i < dist.size(); i++) {
      if (dist[i] > d)
        d = dist[ver = i];
      dist[i] = -1;
    }
    dfs(ver);
    T ret = 0;
    for (int i = 0; i < dist.size(); i++)
      ret = max(ret, dist[i]);
    return ret;
  }
};

int main() {

  TreeDiameter<ll> inst(100010);
  int n;
  cin >> n;
  REP(i, n - 1) {
    int a, b, s;
    cin >> a >> b >> s;
    inst.add_edge(a, b, s);
  }

  cout << inst.getDiameter() << endl;

  return 0;
}