alexjercan/bugnet huggingface dataset:
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

// ???????????????
#include <tuple>
#include <vector>
// ???
typedef int Weight;
// ?????????
class Tree {
public:
  explicit Tree(int n);
  void join(int u, int v, Weight w);
  Weight diameter() const;

private:
  struct Edge {
    int from, to;
    Weight weight;
  };
  struct Farthest {
    int vertex;
    Weight cost;
  };
  const Farthest diameter_dfs(int current, int previous) const;
  std::vector<std::vector<Edge>> edge_;
};
// ?????????????????????
Tree::Tree(int n) : edge_(n) {}
// ????????????
void Tree::join(int u, int v, Weight w) {
  edge_[u].push_back({u, v, w});
  edge_[v].push_back({v, u, w});
}
// ??´???
Weight Tree::diameter() const {
  int v = diameter_dfs(0, 0).vertex;
  return diameter_dfs(v, v).cost;
}
const Tree::Farthest Tree::diameter_dfs(int current, int previous) const {
  Farthest result({current, 0});
  for (const auto &e : edge_[current]) {
    if (e.to == previous)
      continue;
    auto f = diameter_dfs(e.to, e.from);
    f.cost += e.weight;
    if (result.cost < f.cost)
      result = f;
  }
  return result;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  Tree tree(n);
  for (int i = 0; i < n - 1; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    tree.join(s, t, w);
  }
  cout << tree.diameter() << endl;
}

Error: 0

Fixed submission:

#include <tuple>
#include <unordered_map>
//
template <typename Label, typename Weight> class Tree {
public:
  void join(const Label &u, const Label &v, const Weight &w);
  const Weight diameter() const;

private:
  struct Farthest {
    Label label;
    Weight cost;
  };
  const Farthest diameter_dfs(const Label &current,
                              const Label &previous) const;
  std::unordered_map<Label, std::unordered_map<Label, Weight>> weight_;
};
// ????????????
template <typename Label, typename Weight>
void Tree<Label, Weight>::join(const Label &u, const Label &v,
                               const Weight &w) {
  weight_[u][v] = weight_[v][u] = w;
}
// ??´???
template <typename Label, typename Weight>
const Weight Tree<Label, Weight>::diameter() const {
  Label v = begin(weight_)->first;
  v = diameter_dfs(v, v).label;
  return diameter_dfs(v, v).cost;
}
template <typename Label, typename Weight>
const typename Tree<Label, Weight>::Farthest
Tree<Label, Weight>::diameter_dfs(const Label &current,
                                  const Label &previous) const {
  Farthest result({current, 0});
  if (weight_.count(current))
    for (auto e : weight_.at(current)) {
      if (e.first == previous)
        continue;
      auto f = diameter_dfs(e.first, current);
      f.cost += e.second;
      if (result.cost < f.cost)
        result = f;
    }
  return result;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  Tree<int, int> tree;
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    tree.join(s, t, w);
  }
  cout << tree.diameter() << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1 << 28;

template <class T> ostream &operator<<(ostream &os, const vector<T> xs) {
  if (xs.empty())
    return os << "[]";
  os << "[" << xs[0];
  for (int i = 1; i < xs.size(); i++)
    os << " " << xs[i];
  return os << "]";
}

struct Edge {
  int from, to, cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

int N;
vector<vector<Edge>> G;
void calcDist(int v, vector<int> &D) {
  D.clear();
  D.resize(N, INF);
  D[v] = 0;
  queue<int> Q;
  Q.push(v);
  while (!Q.empty()) {
    int c = Q.front();
    Q.pop();
    for (int i = 0; i < G[c].size(); i++) {
      Edge &e = G[c][i];
      if (D[e.to] > D[c] + e.cost) {
        D[e.to] = D[c] + e.cost;
        Q.push(e.to);
      }
    }
  }
}
// vの最遠点のひとつを返す
int farthest(int v) {
  static vector<int> D;
  calcDist(v, D);
  int ret = -1, dist = -1;
  for (int i = 0; i < N; i++) {
    if (dist < D[i]) {
      ret = i;
      dist = D[i];
    }
  }
  return ret;
}
int diameter() {
  int u = farthest(0);
  int v = farthest(u);
  static vector<int> D;
  calcDist(v, D);
  return D[u];
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1 << 28;

struct Edge {
  int from, to, cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

int N;
vector<vector<Edge>> G;
void calcDist(int v, vector<int> &D) {
  D.clear();
  D.resize(N, INF);
  D[v] = 0;
  queue<int> Q;
  Q.push(v);
  while (!Q.empty()) {
    int c = Q.front();
    Q.pop();
    for (int i = 0; i < G[c].size(); i++) {
      Edge &e = G[c][i];
      if (D[e.to] > D[c] + e.cost) {
        D[e.to] = D[c] + e.cost;
        Q.push(e.to);
      }
    }
  }
}
// vの最遠点のひとつを返す
int farthest(int v) {
  static vector<int> D;
  calcDist(v, D);
  int ret = -1, dist = -1;
  for (int i = 0; i < N; i++) {
    if (dist < D[i]) {
      ret = i;
      dist = D[i];
    }
  }
  return ret;
}
int diameter() {
  int u = farthest(0);
  int v = farthest(u);
  static vector<int> D;
  calcDist(v, D);
  return D[u];
}

int main() {
  cin >> N;
  G.clear();
  G.resize(N);
  for (int i = 0; i < N; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(Edge(s, t, w));
    G[t].push_back(Edge(t, s, w));
  }
  cout << diameter() << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

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

struct Tree {
  int N, diameter;
  vector<vector<pair<int, int>>> E;
  vector<int> d; // ある点からの距離, -1なら未探索を表す
  Tree(int n) : N(n), E(n), d(n){};

  // 辺を追加する //
  void addPoint(int a, int b, int c) {
    E[a].push_back(make_pair(b, c));
    E[b].push_back(make_pair(a, c));
  }

  // 任意の節を始点として、幅優先で各節の距離を計算する(BFS) //
  void bfsDistance(int s) {
    queue<int> q;
    for (int i = 0; i < N; i++)
      d[i] = -1; // 距離計算用の初期化
    d[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (unsigned int i = 0; i < E[cur].size(); i++) {
        int next = E[cur][i].first;
        if (d[next] != -1)
          continue; // 探索済みはスキップ(BFS探索における親)
        d[next] = d[cur] + E[cur][i].second;
        q.push(next);
      }
    }
  }

  // 木の直径を求め、表示する //
  void solveDiameter() {
    int maxv, point;
    // ある節からの最大距離となる節を求める(直径の片側の節)
    bfsDistance(0);
    maxv = point = 0;
    for (int i = 0; i < N; i++) {
      if (maxv < d[i]) {
        maxv = d[i];
        point = i;
      }
    }

    // 直径の片側から最大の距離を求め、直径となる2つの節を決定する
    bfsDistance(point);
    maxv = 0;
    for (int i = 0; i < N; i++) {
      if (maxv < d[i])
        maxv = d[i];
    }
    cout << maxv << endl;
  }
};

int main() {
  int n;
  cin >> n;
  Tree T(n);
  for (int i = 0; i < (n - 1); i++) {
    int s, t, w;
    cin >> s >> t >> w;
    T.addPoint(s, t, w);
  }

  T.solveDiameter();

  return 0;
}


Error: 0

Fixed submission:

// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

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

struct Tree {
  int N, diameter;
  vector<vector<pair<int, int>>> E;
  vector<int> d; // ある点からの距離, -1なら未探索を表す
  Tree(int n) : N(n), E(n), d(n){};

  // 辺を追加する //
  void addPoint(int a, int b, int c) {
    E[a].push_back(make_pair(b, c));
    E[b].push_back(make_pair(a, c));
  }

  // 任意の節を始点として、幅優先で各節の距離を計算する(BFS) //
  void bfsDistance(int s) {
    queue<int> q;
    for (int i = 0; i < N; i++)
      d[i] = -1; // 距離計算用の初期化
    d[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (unsigned int i = 0; i < E[cur].size(); i++) {
        int next = E[cur][i].first;
        if (d[next] != -1)
          continue; // 探索済みはスキップ(BFS探索における親)
        d[next] = d[cur] + E[cur][i].second;
        q.push(next);
      }
    }
  }

  // 木の直径を求め、表示する //
  void solveDiameter() {
    int maxv, point;
    // ある節からの最大距離となる節を求める(直径の片側の節)
    bfsDistance(0);
    maxv = 0;
    for (int i = 0; i < N; i++) {
      if (maxv < d[i]) {
        maxv = d[i];
        point = i;
      }
    }

    // 直径の片側から最大の距離を求め、直径となる2つの節を決定する
    bfsDistance(point);
    maxv = 0;
    for (int i = 0; i < N; i++) {
      if (maxv < d[i])
        maxv = d[i];
    }
    cout << maxv << endl;
  }
};

int main() {
  int n;
  cin >> n;
  Tree T(n);
  for (int i = 0; i < (n - 1); i++) {
    int s, t, w;
    cin >> s >> t >> w;
    T.addPoint(s, t, w);
  }

  T.solveDiameter();

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

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 100010
#define INF (1 << 29)

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

struct State {
  int v, cost;
  State(int v, int cost) : v(v), cost(cost) {}
  bool operator<(const State &s) const { return cost > s.cost; }
};

int V, maxi, start;
int dist[MAX_V];
bool visited[MAX_V];
vector<Edge> G[MAX_V];

void getStart(int v, int d) {
  if (dist[v] < INF) {
    return;
  }
  dist[v] = d;
  if (maxi < d) {
    maxi = d;
    start = v;
  }
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    getStart(e.to, d + e.cost);
  }
}

int getTreeDiameter(int v) {
  int res = 0;
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (!visited[e.to]) {
      visited[e.to] = true;
      res = max(res, e.cost + getTreeDiameter(e.to));
    }
  }
  return res;
}

int main() {
  int a, b, w;
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> a >> b >> w;
    G[a].push_back(Edge(b, w));
    G[b].push_back(Edge(a, w));
  }
  maxi = 0;
  fill(dist, dist + V, INF);
  getStart(0, 0);
  memset(visited, false, sizeof(visited));
  visited[start] = true;
  cout << getTreeDiameter(start) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 200010
#define INF (1 << 29)

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

struct State {
  int v, cost;
  State(int v, int cost) : v(v), cost(cost) {}
  bool operator<(const State &s) const { return cost > s.cost; }
};

int V;
vector<Edge> G[MAX_V];

int getTreeDiameter() {
  priority_queue<State> Q;
  Q.push(State(0, 0));
  int d[MAX_V], start, m;
  fill(d, d + MAX_V, INF);
  d[0] = m = 0;
  start = -1;

  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();
    int v = s.v;
    if (d[v] < s.cost) {
      continue;
    }
    for (int i = 0; i < (int)G[v].size(); i++) {
      Edge e = G[v][i];
      if (d[v] + e.cost < d[e.to]) {
        d[e.to] = d[v] + e.cost;
        Q.push(State(e.to, d[e.to]));
        if (m < d[e.to]) {
          m = d[e.to];
          start = e.to;
        }
      }
    }
  }

  Q.push(State(start, 0));
  fill(d, d + MAX_V, INF);
  d[start] = m = 0;

  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();
    int v = s.v;
    if (d[v] < s.cost) {
      continue;
    }
    for (int i = 0; i < (int)G[v].size(); i++) {
      Edge e = G[v][i];
      if (d[v] + e.cost < d[e.to]) {
        d[e.to] = d[v] + e.cost;
        Q.push(State(e.to, d[e.to]));
        m = max(m, d[e.to]);
      }
    }
  }
  return m;
}

int main() {
  int a, b, w;
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> a >> b >> w;
    G[a].push_back(Edge(b, w));
    G[b].push_back(Edge(a, w));
  }
  cout << getTreeDiameter() << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
const int MAX = 100000;
const int INF = 123456789;
vector<pair<int, int>> G[MAX];
bool vis[MAX];
int d[MAX];
int maxv;

void bfs(int s) {
  for (int i = 0; i < n; i++)
    d[i] = INF;

  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++) {
      int v = G[u][i].first;
      if (d[v] == INF) {
        d[v] = d[u] + G[u][i].second;
        q.push(v);
      }
    }
  }
}

void solve() {
  bfs(0);
  maxv = 0;
  int tgt = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INF)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      tgt = i;
    }
  }

  bfs(tgt);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INF)
      continue;
    maxv = max(maxv, d[i]);
  }
}

int main() {
  cin >> 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));
  }

  solve();

  cout << maxv << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
const int MAX = 100000;
const int INF = 123456789;
vector<pair<int, int>> G[MAX];
bool vis[MAX];
int d[MAX];
int maxv;

void bfs(int s) {
  for (int i = 0; i < n; i++)
    d[i] = INF;

  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++) {
      int v = G[u][i].first;
      if (d[v] == INF) {
        d[v] = d[u] + G[u][i].second;
        q.push(v);
      }
    }
  }
}

void solve() {
  bfs(0);
  maxv = 0;
  int tgt = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INF)
      continue;
    if (maxv < d[i]) {
      maxv = d[i];
      tgt = i;
    }
  }

  bfs(tgt);
  maxv = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] == INF)
      continue;
    maxv = max(maxv, d[i]);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; 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));
  }

  solve();

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

#include <tuple>
#include <vector>
//
class Tree {
public:
  explicit Tree(int n);
  void join(int u, int v, int w);
  int diameter() const;

private:
  struct Edge {
    int from, to;
    int weight;
  };
  struct Farthest {
    int vertex;
    int cost;
  };
  const Farthest diameter_dfs(int current, int previous) const;
  std::vector<std::vector<Edge>> edge_;
};
// ?????????????????????
Tree::Tree(int n) : edge_(n) {}
// ????????????
void Tree::join(int u, int v, int w) {
  edge_[u].push_back({u, v, w});
  edge_[v].push_back({v, u, w});
}
// ??´???
int Tree::diameter() const {
  int v = diameter_dfs(0, 0).vertex;
  return diameter_dfs(v, v).cost;
}
const Tree::Farthest Tree::diameter_dfs(int current, int previous) const {
  Farthest result({current, 0});
  for (const auto &e : edge_[current]) {
    if (e.to == previous)
      continue;
    auto f = diameter_dfs(e.to, e.from);
    f.cost += e.weight;
    if (result.cost < f.cost)
      result = f;
  }
  return result;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  Tree tree(n);
  for (int i = 0; i < n - 1; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    tree.join(s, t, w);
  }
  cout << tree.diameter() << endl;
}

Error: 0

Fixed submission:

#include <tuple>
#include <unordered_map>
//
template <typename Label, typename Weight> class Tree {
public:
  void join(const Label &u, const Label &v, const Weight &w);
  const Weight diameter() const;

private:
  const std::tuple<Label, Weight> diameter_dfs(const Label &current,
                                               const Label &previous,
                                               const Weight &cost) const;
  std::unordered_map<Label, std::unordered_map<Label, Weight>> weight_;
};
// ????????????
template <typename Label, typename Weight>
void Tree<Label, Weight>::join(const Label &u, const Label &v,
                               const Weight &w) {
  weight_[u][v] = weight_[v][u] = w;
}
// ??´???
template <typename Label, typename Weight>
const Weight Tree<Label, Weight>::diameter() const {
  Label v = begin(weight_)->first;
  v = std::get<0>(diameter_dfs(v, v, Weight()));
  return std::get<1>(diameter_dfs(v, v, Weight()));
}
template <typename Label, typename Weight>
const std::tuple<Label, Weight>
Tree<Label, Weight>::diameter_dfs(const Label &current, const Label &previous,
                                  const Weight &cost) const {
  Label result_v = current;
  Weight result_w = cost;
  if (weight_.count(current))
    for (auto e : weight_.at(current)) {
      if (e.first == previous)
        continue;
      Label v;
      Weight w;
      std::tie(v, w) = diameter_dfs(e.first, current, cost + e.second);
      if (w <= result_w)
        continue;
      result_v = v;
      result_w = w;
    }
  return std::tuple<Label, Weight>(result_v, result_w);
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  Tree<int, int> tree;
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    tree.join(s, t, w);
  }
  cout << tree.diameter() << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;

P visit(int v, int prev, vector<vector<P>> &G) {
  P res(v, 0);
  REP(i, G[v].size()) {
    if (G[v][i].first != prev) {
      P t = visit(G[v][i].first, v, G);
      t.second += G[v][i].second;
      if (res.second < t.second)
        res = t;
    }
  }
  return res;
}
int diameter(vector<vector<P>> &G) {
  P r = visit(0, -1, G);
  P t = visit(r.first, -1, G);
  return t.second; // (r.first, t.first) is farthest pair
}

int main() {
  int N;
  cin >> N;
  vector<vector<P>> G(N);
  REP(i, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  cout << diameter(G) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;

P visit(int v, int prev, vector<vector<P>> &G) {
  P res(v, 0);
  REP(i, G[v].size()) {
    if (G[v][i].first != prev) {
      P t = visit(G[v][i].first, v, G);
      t.second += G[v][i].second;
      if (res.second < t.second)
        res = t;
    }
  }
  return res;
}
int diameter(vector<vector<P>> &G) {
  P r = visit(0, -1, G);
  P t = visit(r.first, -1, G);
  return t.second; // (r.first, t.first) is farthest pair
}

int main() {
  int N;
  cin >> N;
  vector<vector<P>> G(N + 1);
  REP(i, N) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(P(t, w));
    G[t].push_back(P(s, w));
  }
  cout << diameter(G) << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> Pair;

struct Edge {
  int to;
  int cost;
};

int n;
bool used[100000];
vector<Edge> es[100000];

Pair dfs(int v) {
  used[v] = true;

  Pair ret = Pair(0, v);
  for (int i = 0; i < es[v].size(); i++) {
    const int u = es[v][i].to;
    const int w = es[v][i].cost;
    if (used[u])
      continue;
    Pair tmp = dfs(u);
    ret = max(ret, Pair(w + tmp.first, tmp.second));
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    Edge e;
    int a, b;
    scanf("%d%d%d", &a, &b, &e.cost);
    e.to = b;
    es[a].push_back(e);
    e.to = a;
    es[b].push_back(e);
  }

  Pair p = dfs(0);
  fill(used, used + n, false);
  printf("%d\\n", dfs(p.second).first);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> Pair;

struct Edge {
  int to;
  int cost;
};

int n;
vector<Edge> es[100000];

Pair dfs(int v, int p) {
  Pair ret = Pair(0, v);
  for (int i = 0; i < es[v].size(); i++) {
    int u = es[v][i].to;
    int w = es[v][i].cost;
    if (u == p)
      continue;
    Pair tmp = dfs(u, v);
    ret = max(ret, Pair(w + tmp.first, tmp.second));
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    Edge e;
    int a, b;
    scanf("%d%d%d", &a, &b, &e.cost);
    e.to = b;
    es[a].push_back(e);
    e.to = a;
    es[b].push_back(e);
  }

  Pair p = dfs(0, 0);
  printf("%d\\n", dfs(p.second, p.second).first);
}