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

#include <algorithm>

namespace rika {
template <typename T> class edge {
public:
  int to;
  T cost;

  edge() = default;
  edge(const edge<T> &) = default;
  edge(edge<T> &&) = default;
  edge &operator=(const edge<T> &) = default;
  edge &operator=(edge<T> &&) = default;

  ~edge() = default;

  edge(int _to, T _cost) : to(_to), cost(_cost) {}
};
} // namespace rika

#include <bits/stdc++.h>
using namespace std;
using namespace rika;
namespace rika {
namespace internal {
template <typename TValue, typename TGraph>
std::pair<TValue, int> visit(int from, int v, const TGraph &g) {
  auto r = std::make_pair(TValue(), v);
  try {
    for (auto it = g[v].begin(); it != g[v].end(); ++it) {
      if (it->to == from)
        continue;
      auto r2 = visit<TValue>(v, it->to, g);
      if (r2.first + it->cost >= r.first)
        r = std::make_pair(r2.first + it->cost, r2.second);
    }
  } catch (const std::out_of_range &e) {
  }
  return r;
}
} // namespace internal

template <typename TValue, typename TGraph>
TValue tree_diameter(const TGraph &g) {
  using namespace internal;
  auto r1 = visit<TValue>(-1, 0, g);
  auto r2 = visit<TValue>(-1, r1.second, g);
  return r2.first;
}
} // namespace rika

#include <bits/stdc++.h>
using namespace std;
using namespace rika;
int main() {
  int N;
  cin >> N;
  vector<vector<edge<int>>> g(N * 10);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(edge<int>(t, w));
    g[t].push_back(edge<int>(s, w));
  }
  cout << tree_diameter<int>(g) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>

namespace rika {
template <typename T> class edge {
public:
  int to;
  T cost;

  edge() = default;
  edge(const edge<T> &) = default;
  edge(edge<T> &&) = default;
  edge &operator=(const edge<T> &) = default;
  edge &operator=(edge<T> &&) = default;

  ~edge() = default;

  edge(int _to, T _cost) : to(_to), cost(_cost) {}
};
} // namespace rika

#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
using namespace rika;
namespace rika {
namespace internal {
template <typename TValue, typename TGraph>
std::pair<TValue, int> visit(int from, int v, const TGraph &g) {
  auto r = std::make_pair(TValue(), v);
  try {
    for (auto it = g[v].begin(); it != g[v].end(); ++it) {
      if (it->to == from)
        continue;
      auto r2 = visit<TValue>(v, it->to, g);
      if (r2.first + it->cost >= r.first)
        r = std::make_pair(r2.first + it->cost, r2.second);
    }
  } catch (const std::out_of_range &e) {
    if (v < 0)
      return make_pair(INF, INF);
    else
      return r;
  }
  return r;
}
} // namespace internal

template <typename TValue, typename TGraph>
TValue tree_diameter(const TGraph &g) {
  using namespace internal;
  auto r1 = visit<TValue>(-1, 0, g);
  if (r1.first == INF)
    return INF;
  auto r2 = visit<TValue>(-1, r1.second, g);
  if (r2.first == INF)
    return INF;
  return r2.first;
}
} // namespace rika

#include <bits/stdc++.h>
using namespace std;
using namespace rika;
int main() {
  int N;
  cin >> N;
  vector<vector<edge<int>>> g(N * 10);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(edge<int>(t, w));
    g[t].push_back(edge<int>(s, w));
  }
  auto a = tree_diameter<int>(g);
  cout << a << endl;
  if (a == INF)
    return -1;
  return 0;
}