alexjercan/bugnet huggingface dataset:
Problem p02371 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
enum COLOR { BLACK, WHITE };

void dikstra(vector<vector<pair<int, int>>> &G, int s, vector<int> &d) {
  vector<COLOR> color(G.size(), WHITE);
  priority_queue<pair<int, int>> Q;
  d[s] = 0;
  color[s] = BLACK;
  int u = s;
  while (1) {
    for (int j = 0, s_j = G[u].size(); j < s_j; j++) {
      int v = G[u][j].first;
      int w = G[u][j].second;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        Q.push(make_pair(d[v] * -1, v));
      }
    }
    if (Q.empty())
      break;
    u = Q.top().second;
    color[u] = BLACK;
    Q.pop();
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(make_pair(t, w));
    G[t].push_back(make_pair(s, w));
  }

  vector<int> distance(n, 1e9);
  dikstra(G, 0, distance);
  int y = 0;
  for (int i = 0; i < n; i++) {
    if (distance[y] < distance[i])
      y = i;
  }
  vector<int> distance2(n, 1e9);
  dikstra(G, y, distance2);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (ans < distance2[i])
      ans = distance2[i];
  }
  cout << ans << endl;

  // int max = 0;
  // for (int i = 0; i < n; i++) {
  // }
  // cout << max << endl;

  // for (int i = 0; i < n; i++) {
  //     for (int j = 0; j < G[i].size(); j++) {
  //         cout << G[i][j].first << ":" << G[i][j].second << " ";
  //     }
  //     cout << endl;
  // }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
enum COLOR { BLACK, WHITE };

void dikstra(vector<vector<pair<int, int>>> &G, int s, vector<int> &d) {
  vector<COLOR> color(G.size(), WHITE);
  priority_queue<pair<int, int>> Q;
  d[s] = 0;
  color[s] = BLACK;
  int u = s;
  // for (int i = 0, s_i = G.size(); i < s_i; i++) {
  while (1) {
    for (int j = 0, s_j = G[u].size(); j < s_j; j++) {
      int v = G[u][j].first;
      int w = G[u][j].second;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        Q.push(make_pair(d[v] * -1, v));
      }
    }
    if (Q.empty())
      break;
    u = Q.top().second;
    color[u] = BLACK;
    Q.pop();
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(make_pair(t, w));
    G[t].push_back(make_pair(s, w));
  }
  int max = 0;
  for (int i = 0; i < n; i++) {
    vector<int> distance(n, 1e9);
    dikstra(G, i, distance);
    for (int j = 0; j < n; j++) {
      if (max < distance[j])
        max = distance[j];
    }
  }
  cout << max << endl;

  // for (int i = 0; i < n; i++) {
  //     for (int j = 0; j < G[i].size(); j++) {
  //         cout << G[i][j].first << ":" << G[i][j].second << " ";
  //     }
  //     cout << endl;
  // }

  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

const int V_MAX = 100000;

struct Edge {
  int v;
  int w;
};

int solve(vector<Edge> graph[], int v, int prev, int n, int &m) {
  int first = 0;
  int second = 0;

  for (int i = 0; i < graph[v].size(); i++) {
    if (graph[v][i].v == prev)
      continue;
    int res =
        solve(graph, graph[v][i].v, v, n + graph[v][i].w, m) + graph[v][i].w;

    if (second < res) {
      if (first < res) {
        second = first;
        first = res;
      } else {
        second = res;
      }
    }
  }

  m = max(m, first + max(n, second));

  return first;
}

int main() {
  int n;
  vector<Edge> graph[V_MAX];

  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    Edge e1, e2;
    scanf("%d%d%d", &s, &t, &w);

    e1.v = t;
    e1.w = w;
    e2.v = s;
    e2.w = w;

    graph[s].push_back(e1);
    graph[t].push_back(e2);
  }

  int m = 0;
  solve(graph, 0, 0, 0, m);

  printf("%d\\n", m);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

const int V_MAX = 100;

struct Edge {
  int v;
  int w;
};

int solve(vector<Edge> graph[], int v, int prev, int n, int &m) {
  int first = 0;
  int second = 0;

  for (int i = 0; i < graph[v].size(); i++) {
    if (graph[v][i].v == prev)
      continue;
    int res =
        solve(graph, graph[v][i].v, v, n + graph[v][i].w, m) + graph[v][i].w;

    if (second < res) {
      if (first < res) {
        second = first;
        first = res;
      } else {
        second = res;
      }
    }
  }

  m = max(m, first + max(n, second));

  return first;
}

int main() {
  int n;
  vector<Edge> graph[V_MAX];

  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    Edge e1, e2;
    scanf("%d%d%d", &s, &t, &w);

    e1.v = t;
    e1.w = w;
    e2.v = s;
    e2.w = w;

    graph[s].push_back(e1);
    graph[t].push_back(e2);
  }

  int m = 0;
  solve(graph, 0, 0, 0, m);

  printf("%d\\n", m);

  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#include <bits/stdc++.h>
using namespace std;

struct Diameter {
  Diameter() {}
  Diameter(int n) : G(n) {}

  struct edge {
    edge() {}
    edge(int to, int cost) : to(to), cost(cost) {}

    int to, cost;
  };

  const pair<int, int> id = make_pair(0, -1);
  vector<vector<edge>> G;

  void add_edge(int u, int v, int cost) {
    G[u].push_back(edge(v, cost));
    G[v].push_back(edge(u, cost));
  }

  void add_edge(int u, int v) { add_edge(u, v, 1); }

  pair<int, int> dfs(int v, int pre) {
    pair<int, int> res = id;
    for (const auto &e : G[v]) {
      if (e.to == pre)
        continue;
      pair<int, int> p = dfs(e.to, v);
      p.first += e.cost;
      res = max(res, p);
    }

    if (res == id) {
      res = make_pair(0, v);
    }
    return res;
  }

  int dist() {
    pair<int, int> l = dfs(0, -1);
    // cerr << l.second << endl;
    pair<int, int> r = dfs(l.second, -1);
    // cerr << r.second << endl;
    return r.first;
  }
};

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

  Diameter d(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    d.add_edge(u, v, c);
  }

  cout << d.dist() << endl;
}

Error: 0
3
0


Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#include <bits/stdc++.h>
using namespace std;

struct Diameter {
  Diameter() {}
  Diameter(int n) : G(n) {}

  struct edge {
    edge() {}
    edge(int to, int cost) : to(to), cost(cost) {}

    int to, cost;
  };

  const pair<int, int> id = make_pair(0, -1);
  vector<vector<edge>> G;

  void add_edge(int u, int v, int cost) {
    G[u].push_back(edge(v, cost));
    G[v].push_back(edge(u, cost));
  }

  void add_edge(int u, int v) { add_edge(u, v, 1); }

  pair<int, int> dfs(int v, int pre) {
    pair<int, int> res = id;
    for (const auto &e : G[v]) {
      if (e.to == pre)
        continue;
      pair<int, int> p = dfs(e.to, v);
      p.first += e.cost;
      res = max(res, p);
    }

    if (res == id) {
      res = make_pair(0, v);
    }
    return res;
  }

  int dist() {
    pair<int, int> l = dfs(0, -1);
    cerr << l.second << endl;
    pair<int, int> r = dfs(l.second, -1);
    cerr << r.second << endl;
    return r.first;
  }
};

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

  Diameter d(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    d.add_edge(u, v, c);
  }

  cout << d.dist() << endl;
}
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) {
    exit(0);
  }
  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

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

int N;
namespace rika {
namespace internal {
template <typename TValue, typename TGraph>
std::pair<TValue, int> visit(int from, int v, const TGraph &g) {
  if (v >= N)
    return std::make_pair(static_cast<TValue>(-1), -1);
  auto r = std::make_pair(TValue(), v);
  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);
  }
  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 < 0)
    return -1;
  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() {
  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;
}
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

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>
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);
  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);
  }
  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

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;
}
Problem p02371 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

class Edge {
public:
  int from, to, weight;
  Edge(int f, int t, int w) : from(f), to(t), weight(w) {}
};

int memo[100001] = {0};

int height(vector<vector<Edge>> &Graph, int parent) {
  if (memo[parent])
    return memo[parent];
  int ret = 0;
  for (int i = 0; i < (int)Graph[parent].size(); ++i) {
    ret =
        max(ret, Graph[parent][i].weight + height(Graph, Graph[parent][i].to));
  }
  return memo[parent] = ret;
}

int diameter(vector<vector<Edge>> &Graph, int parent) {
  int ret = 0;
  int h1 = 0, h2 = 0;
  for (int i = 0; i < (int)Graph[parent].size(); ++i) {
    int h = Graph[parent][i].weight + height(Graph, Graph[parent][i].to);
    if (h1 < h) {
      h2 = h1;
      h1 = h;
    } else if (h2 < h) {
      h2 = h;
    }
    ret = max(ret, diameter(Graph, Graph[parent][i].to));
  }
  ret = max(ret, h1 + h2);
  return ret;
}

int main() {
  int n, s, t, w;
  cin >> n;
  vector<vector<Edge>> Graph(n + 1);
  for (int i = 1; i <= n - 1; ++i) {
    cin >> s >> t >> w;
    Graph[s].push_back(Edge(s, t, w));
  }
  cout << diameter(Graph, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

class Edge {
public:
  int from, to, weight;
  Edge(int f, int t, int w) : from(f), to(t), weight(w) {}
};

int height(vector<vector<Edge>> &Graph, int parent) {
  int ret = 0;
  for (int i = 0; i < (int)Graph[parent].size(); ++i) {
    ret =
        max(ret, Graph[parent][i].weight + height(Graph, Graph[parent][i].to));
  }
  return ret;
}

int diameter(vector<vector<Edge>> &Graph, int parent) {
  int ret = 0;
  int h1 = 0, h2 = 0;
  for (int i = 0; i < (int)Graph[parent].size(); ++i) {
    int h = Graph[parent][i].weight + height(Graph, Graph[parent][i].to);
    if (h1 < h) {
      h2 = h1;
      h1 = h;
    } else if (h2 < h) {
      h2 = h;
    }
    ret = max(ret, diameter(Graph, Graph[parent][i].to));
  }
  ret = max(ret, h1 + h2);
  return ret;
}

int main() {
  int n, s, t, w;
  cin >> n;
  vector<vector<Edge>> Graph(n + 1);
  for (int i = 1; i <= n - 1; ++i) {
    cin >> s >> t >> w;
    Graph[s].push_back(Edge(s, t, w));
  }
  cout << diameter(Graph, 0) << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 100000
#define INFTY (1 << 30)

class Edge {
public:
  int t, w;
  Edge() {}
  Edge(int t, int w) : t(t), w(w) {}
};

vector<Edge> G[MAX];
int n, d[MAX];

bool vis[MAX];
int cnt;

void bfs(int s) {
  for (int i = 0; i < n; i++)
    d[i] = INFTY;
  queue<int> Q;
  Q.push(s);
  d[s] = 0;
  int u;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      Edge e = G[u][i];
      if (d[e.t] == INFTY) {
        d[e.t] = d[u] + e.w;
        Q.push(e.t);
      }
    }
  }
}

void solve() {
  // 適当な視点 s から最も遠い節点 tgt を求める
  bfs(0);
  int maxv = 0;
  int tgt = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      tgt = i;
    }
  }

  // tgt から最も遠い節点の距離 maxv を求める
  bfs(tgt);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    maxv = max(maxv, d[i]);
  }

  cout << maxv << endl;
}

main() {
  int s, t, w;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    cin >> s >> t >> w;

    G[s].push_back(Edge(t, w));
    G[t].push_back(Edge(s, w));
  }
  solve();
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 100000
#define INFTY (1 << 30)

class Edge {
public:
  int t, w;
  Edge() {}
  Edge(int t, int w) : t(t), w(w) {}
};

vector<Edge> G[MAX];
int n, d[MAX];

bool vis[MAX];
int cnt;

void bfs(int s) {
  for (int i = 0; i < n; i++)
    d[i] = INFTY;
  queue<int> Q;
  Q.push(s);
  d[s] = 0;
  int u;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      Edge e = G[u][i];
      if (d[e.t] = INFTY) {
        d[e.t] = d[u] + e.w;
        Q.push(e.t);
      }
    }
  }
}

void solve() {
  // 適当な視点 s から最も遠い節点 tgt を求める
  bfs(0);
  int maxv = 0;
  int tgt = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      tgt = i;
    }
  }

  // tgt から最も遠い節点の距離 maxv を求める
  bfs(tgt);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    maxv = max(maxv, d[i]);
  }

  cout << maxv << endl;
}

int main() {
  int s, t, w;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    cin >> s >> t >> w;

    G[s].push_back(Edge(t, w));
    G[t].push_back(Edge(s, w));
  }
  solve();

  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

void dfs1(int &u, int nowcost, int &dis, int node, vector<bool> &used,
          vector<vector<pair<int, int>>> &adj) {
  used[node] = true;
  if (dis < nowcost) {
    dis = nowcost;
    u = node;
  }
  // cout << node << " " << nowcost << endl;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    // cout << "ok" << endl;
    int to = adj[node][i].first;
    int cost = adj[node][i].second;
    if (!used[to]) {
      dfs1(u, nowcost + cost, dis, to, used, adj);
    }
  }
}

void dfs2(int nowcost, int &dis, int node, vector<bool> &used,
          vector<vector<pair<int, int>>> &adj) {
  used[node] = true;
  dis = max(dis, nowcost);
  // cout << node << " " << nowcost << endl;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    // cout << "ok" << endl;
    int to = adj[node][i].first;
    int cost = adj[node][i].second;
    if (!used[to]) {
      dfs2(nowcost + cost, dis, to, used, adj);
    }
  }
}

int main() {
  int n;
  cin >> n;
  if (n == 1)
    return !printf("0\\n");
  vector<vector<pair<int, int>>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }

  int u;
  int dis = 0;
  vector<bool> used(n, false);
  dfs1(u, 0, dis, 0, used, adj);
  for (int i = 0; i < n; i++)
    used[i] = false;
  dis = 0;
  dfs2(0, dis, u, used, adj);
  cout << dis << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void dfs1(int &u, int nowcost, int &dis, int node, vector<bool> &used,
          vector<vector<pair<int, int>>> &adj) {
  used[node] = true;
  if (dis < nowcost) {
    dis = nowcost;
    u = node;
  }
  // cout << node << " " << nowcost << endl;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    // cout << "ok" << endl;
    int to = adj[node][i].first;
    int cost = adj[node][i].second;
    if (!used[to]) {
      dfs1(u, nowcost + cost, dis, to, used, adj);
    }
  }
}

void dfs2(int nowcost, int &dis, int node, vector<bool> &used,
          vector<vector<pair<int, int>>> &adj) {
  used[node] = true;
  dis = max(dis, nowcost);
  // cout << node << " " << nowcost << endl;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    // cout << "ok" << endl;
    int to = adj[node][i].first;
    int cost = adj[node][i].second;
    if (!used[to]) {
      dfs2(nowcost + cost, dis, to, used, adj);
    }
  }
}

int main() {
  int n;
  cin >> n;
  if (n == 1)
    !printf("0\\n");
  vector<vector<pair<int, int>>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }

  int u;
  int dis = 0;
  vector<bool> used(n, false);
  dfs1(u, 0, dis, 0, used, adj);
  for (int i = 0; i < n; i++)
    used[i] = false;
  dis = 0;
  dfs2(0, dis, u, used, adj);
  cout << dis << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 100000;
static const int INFTY = (1 << 20);
int n;
int d[MAX];

class Edge {
public:
  int t, w;
  Edge() {}
  Edge(int t, int w) : t(t), w(w) {}
};

vector<Edge> G[MAX];

void bfs(int s) {
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  queue<int> Q;
  Q.push(s);
  d[s] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      Edge e = G[u][i];
      if (d[e.t] == INFTY) {
        d[e.t] = d[u] + e.w;
        Q.push(e.t);
      }
    }
  }
}

void solve() {
  bfs(0);
  int maxv = 0;
  int target;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      target = i;
    }
  }
  bfs(target);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    maxv = max(maxv, d[i]);
  }
  cout << maxv << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(Edge(t, w));
    G[t].push_back(Edge(s, w));
  }
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 10000;
static const int INFTY = (1 << 20);
int n;
int d[MAX];

class Edge {
public:
  int t, w;
  Edge() {}
  Edge(int t, int w) : t(t), w(w) {}
};

vector<Edge> G[MAX];

void bfs(int s) {
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  queue<int> Q;
  Q.push(s);
  d[s] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      Edge e = G[u][i];
      if (d[e.t] == INFTY) {
        d[e.t] = d[u] + e.w;
        Q.push(e.t);
      }
    }
  }
}

void solve() {
  bfs(0);
  int maxv = 0;
  int target;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      target = i;
    }
  }
  bfs(target);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INFTY)
      continue;
    maxv = max(maxv, d[i]);
  }
  cout << maxv << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(Edge(t, w));
    G[t].push_back(Edge(s, w));
  }
  solve();
  return 0;
}