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

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

using namespace std;

const int MAX = 100000;
const int INFTY = (1 << 30);
int n, d[MAX];
vector<pair<int, int>> G[MAX];

// sから各頂点への距離テーブルを作る
void bfs(int s) {
  bool visited[MAX];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    d[i] = INFTY;
  }
  queue<int> q;
  visited[s] = true;
  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 next = G[u][i].first;
      int w = G[u][i].second;
      if (!visited[next]) {
        q.push(next);
        d[next] = d[u] + w;
        visited[next] = true;
      }
    }
  }
}

int solve() {
  bfs(0);
  int maxcost = 0, tgt;
  for (int i = 0; i < n; i++) {
    if (d[i] > maxcost) {
      maxcost = d[i];
      tgt = i;
    }
  }

  bfs(tgt);
  maxcost = 0;
  for (int i = 0; i < n; i++) {
    if (d[i] != INFTY && d[i] > maxcost) {
      maxcost = d[i];
    }
  }

  return maxcost;
}

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

  // if(n == 0) cout << 0 << endl;
  cout << solve() << endl;
  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

const int MAX = 100000;
const int INFTY = (1 << 30);
int n, d[MAX];
vector<pair<int, int>> G[MAX];

// sから各頂点への距離テーブルを作る
void bfs(int s) {
  bool visited[MAX];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    d[i] = INFTY;
  }
  queue<int> q;
  visited[s] = true;
  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 next = G[u][i].first;
      int w = G[u][i].second;
      if (!visited[next]) {
        q.push(next);
        d[next] = d[u] + w;
        visited[next] = true;
      }
    }
  }
}

int solve() {
  bfs(0);
  int maxcost = 0, tgt;
  for (int i = 0; i < n; i++) {
    if (d[i] > maxcost) {
      maxcost = d[i];
      tgt = i;
    }
  }

  bfs(tgt);
  for (int i = 0; i < n; i++) {
    if (d[i] > maxcost) {
      maxcost = d[i];
    }
  }

  return maxcost;
}

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

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

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

using namespace std;

static const int MAX = 100000;

vector<pair<int, int>> graph[MAX];

vector<int> bfs(int start) {
  vector<int> d(MAX, 0);
  vector<bool> discovered(MAX, false);
  queue<int> visiting;

  visiting.push(start);
  discovered[start] = true;

  while (!visiting.empty()) {
    int now = visiting.front();
    visiting.pop();

    for (auto it = graph[now].begin(); it != graph[now].end(); ++it) {
      if (!discovered[(*it).first]) {
        discovered[(*it).first] = true;
        visiting.push((*it).first);
        d[(*it).first] = d[now] + (*it).second;
      }
    }
  }

  return d;
}

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

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

  vector<int> d = bfs(0);
  int x = distance(d.begin(), max_element(d.begin(), d.end()));

  d = bfs(x);
  int diameter = *max_element(d.begin(), d.end());
  cout << diameter << endl;

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

static const int MAX = 100000;

vector<pair<int, int>> graph[MAX];

vector<int> bfs(int start) {
  vector<int> d(MAX, 0);
  vector<bool> discovered(MAX, false);
  queue<int> visiting;

  visiting.push(start);
  discovered[start] = true;

  while (!visiting.empty()) {
    int now = visiting.front();
    visiting.pop();

    for (auto it = graph[now].begin(); it != graph[now].end(); ++it) {
      if (!discovered[(*it).first]) {
        discovered[(*it).first] = true;
        visiting.push((*it).first);
        d[(*it).first] = d[now] + (*it).second;
      }
    }
  }

  return d;
}

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

  for (int i = 0; i < n; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    graph[s].push_back(make_pair(t, w));
    graph[t].push_back(make_pair(s, w));
  }

  vector<int> d = bfs(0);
  int x = distance(d.begin(), max_element(d.begin(), d.end()));

  d = bfs(x);
  int diameter = *max_element(d.begin(), d.end());
  cout << diameter << endl;

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

#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int INFTY = 1 << 29;

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int INFTY = 1 << 29;

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

  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

// Diameter of a Tree
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

static const int MAX = 100000;

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

int N;
vector<Edge> G[MAX];
queue<int> Q;
bool visited[MAX];
int d[MAX];

void bfs(int u) {
  int v;
  for (int i = 0; i < MAX; i++) {
    visited[i] = false;
    d[i] = 0;
  }
  Q.push(u);
  visited[u] = true;
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    for (auto e : G[v]) {
      if (!visited[e.t]) {
        Q.push(e.t);
        visited[e.t] = true;
        d[e.t] = d[v] + e.w;
      }
    }
  }
}

int diameter() {
  int x, y, maxd = 0;
  bfs(0);
  for (int i = 0; i < N; i++) {
    if (maxd < d[i]) {
      maxd = d[i];
      x = i;
    }
  }
  bfs(x);
  maxd = 0;
  for (int i = 0; i < N; i++) {
    if (maxd < d[i])
      maxd = d[i];
  }
  return maxd;
}

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

  cout << diameter() << endl;
}


Error: 0

Fixed submission:

// Diameter of a Tree
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

static const int MAX = 100000;

int N;
vector<pair<int, int>> G[MAX];
queue<int> Q;
bool visited[MAX];
int d[MAX];

pair<int, int> bfs(int u) {
  int v, x, maxd = 0;
  while (!Q.empty())
    Q.pop();
  for (int i = 0; i < MAX; i++) {
    visited[i] = false;
    d[i] = 0;
  }
  Q.push(u);
  visited[u] = true;
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    for (auto p : G[v]) {
      if (!visited[p.first]) {
        Q.push(p.first);
        visited[p.first] = true;
        d[p.first] = d[v] + p.second;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (maxd < d[i]) {
      maxd = d[i];
      x = i;
    }
  }
  return make_pair(x, maxd);
}

int diameter() {
  int x;
  pair<int, int> p;
  p = bfs(0);
  x = p.first;
  p = bfs(x);
  return p.second;
}

int main() {
  int s, t, w;
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> s >> t >> w;
    G[s].emplace_back(make_pair(t, w));
    G[t].emplace_back(make_pair(s, w));
  }

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

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to;
  int cost;
  edge(int a, int b) : to(a), cost(b) {}
};

vector<vector<edge>> e;
int N;

pair<int, int> dfs(int p, int par = -1) {
  if (e[p].size() == 1 && par >= 0)
    return make_pair(0, p);
  pair<int, int> res = make_pair(0, 0);
  for (auto x : e[p]) {
    if (x.to == par)
      continue;
    auto t = dfs(x.to, p);
    t.first += x.cost;
    res = max(res, t);
  }
  return res;
}

int main() {
  cin >> N;
  e.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e[s].emplace_back(t, w);
    e[t].emplace_back(s, w);
  }
  auto res = dfs(0);
  res = dfs(res.second);
  cout << res.first << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to;
  int cost;
  edge(int a, int b) : to(a), cost(b) {}
};

vector<vector<edge>> e;
int N;

pair<int, int> dfs(int p, int par = -1) {
  if (e[p].size() == 1 && par >= 0)
    return make_pair(0, p);
  pair<int, int> res = make_pair(0, 1);
  for (auto x : e[p]) {
    if (x.to == par)
      continue;
    auto t = dfs(x.to, p);
    t.first += x.cost;
    res = max(res, t);
  }
  return res;
}

int main() {
  cin >> N;
  e.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e[s].emplace_back(t, w);
    e[t].emplace_back(s, w);
  }
  auto res = dfs(0);
  res = dfs(res.second);
  cout << res.first << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to;
  int cost;
  edge(int a, int b) : to(a), cost(b) {}
};

vector<vector<edge>> e;
int N;

pair<int, int> dfs(int p, int par = -1) {
  if (e[p].size() == 1 && par >= 0)
    return make_pair(0, p);
  pair<int, int> res = make_pair(-1, -1);
  for (auto x : e[p]) {
    if (x.to == par)
      continue;
    auto t = dfs(x.to, p);
    t.first += x.cost;
    res = max(res, t);
  }
  return res;
}

int main() {
  cin >> N;
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  e.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e[s].emplace_back(t, w);
    e[t].emplace_back(s, w);
  }
  auto res = dfs(0);
  res = dfs(res.second);
  cout << res.first << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to;
  int cost;
  edge(int a, int b) : to(a), cost(b) {}
};

vector<vector<edge>> e;
int N;

pair<int, int> dfs(int p, int par = -1) {
  if (e[p].size() == 1 && par >= 0)
    return make_pair(0, p);
  pair<int, int> res = make_pair(-1, -1);
  int cnt = 0;
  for (auto x : e[p]) {
    if (x.to == par)
      continue;
    auto t = dfs(x.to, p);
    t.first += x.cost;
    res = max(res, t);
    cnt++;
  }
  assert(e[p].size() > 0);
  assert(cnt > 0);
  assert(res.first != -1);
  return res;
}

int main() {
  cin >> N;
  e.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e[s].emplace_back(t, w);
    e[t].emplace_back(s, w);
  }
  auto res = dfs(0);
  res = dfs(res.second);
  cout << res.first << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> P;

int n, dis[100005];
vector<P> v[100005];

void dfs1(int idx, int pre) {
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].first;
    int cost = v[idx][i].second;
    if (next == pre)
      continue;
    dfs1(next, idx);
    dis[idx] = max(dis[idx], dis[next] + cost);
  }
}

int dfs2(int idx, int d_pre, int pre) {
  vector<P> child;
  child.push_back(P(0, -1));
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].first;
    int cost = v[idx][i].second;
    if (next == pre)
      child.push_back(P(d_pre + cost, next));
    else
      child.push_back(P(dis[next] + cost, next));
  }
  sort(child.begin(), child.end(), greater<P>());
  int res = child[0].first + child[1].first;
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].first;
    int cost = v[idx][i].second;
    if (next == pre)
      continue;
    res = max(res, dfs2(next, child[next == child[0].second].first, idx));
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    v[a].push_back(P(b, w));
    v[b].push_back(P(a, w));
  }
  dfs1(n / 2, -1);
  cout << dfs2(n / 2, 0, -1) << endl;
}


Error: -11

Fixed submission:

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

typedef pair<int, int> P;

int n, dis[100005];
vector<P> v[100005];

void dfs1(int idx, int pre) {
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].second;
    int cost = v[idx][i].first;
    dfs1(next, idx);
    dis[idx] = max(dis[idx], dis[next] + cost);
  }
}

int dfs2(int idx, int d_pre, int pre) {
  vector<P> child;
  child.push_back(P(0, -1));
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].second;
    int cost = v[idx][i].first;
    if (next == pre)
      child.push_back(P(d_pre + cost, next));
    else
      child.push_back(P(dis[next] + cost, next));
  }
  sort(child.begin(), child.end());
  int res = child[0].first + child[1].first;
  for (int i = 0; i < v[idx].size(); i++) {
    int next = v[idx][i].second;
    int cost = v[idx][i].first;
    if (next == pre)
      continue;
    res = max(res, dfs2(next, child[next == child[0].second].first, idx));
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    v[a].push_back(P(b, w));
    v[b].push_back(P(a, w));
  }
  dfs1(n / 2, -1);
  cout << dfs2(n / 2, 0, -1) << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

struct edge {
  int dest, weight;
};
struct result {
  int dest, distance;
};

result dfs(int previous, int node, vector<vector<edge>> &adjacentList) {
  result r = {node, 0};
  for (edge e : adjacentList[node])
    if (e.dest != previous) {
      result t = dfs(node, e.dest, adjacentList);
      t.distance += e.weight;
      if (r.distance < t.distance)
        r = t;
    }
  return r;
}

int diameterOfTree(vector<vector<edge>> &adjacentList) {
  result r = dfs(-1, 0, adjacentList);
  result t = dfs(-1, r.dest, adjacentList);
  return t.distance;
}

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

  vector<vector<edge>> adjacentList(N);

  REP(i, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    adjacentList[s].push_back(edge{t, w});
    adjacentList[t].push_back(edge{s, w});
  }

  cout << diameterOfTree(adjacentList) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

struct edge {
  int dest, weight;
};
struct result {
  int dest, distance;
};

result dfs(int previous, int node, vector<vector<edge>> adjacentList) {
  result r = {node, 0};
  for (edge e : adjacentList[node])
    if (e.dest != previous) {
      result t = dfs(node, e.dest, adjacentList);
      t.distance += e.weight;
      if (r.distance < t.distance)
        r = t;
    }
  return r;
}

int diameterOfTree(vector<vector<edge>> adjacentList) {
  result r = dfs(-1, 0, adjacentList);
  result t = dfs(-1, r.dest, adjacentList);
  return t.distance;
}

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

  vector<vector<edge>> adjacentList(N);

  REP(i, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    adjacentList[s].push_back(edge{t, w});
    adjacentList[t].push_back(edge{s, w});
  }

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

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to, cost;
};

vector<edge> g[100000];
long long dist[100000];

void dfs1(int idx, int par) {
  for (edge &e : g[idx]) {
    if (e.to == par)
      continue;
    dfs1(e.to, idx);
    dist[idx] = max(dist[idx], dist[e.to] + e.cost);
  }
}

int dfs2(int idx, int d_par, int par) {
  vector<pair<int, int>> d_child;
  d_child.emplace_back(0, -1);
  for (edge &e : g[idx]) {
    if (e.to == par)
      d_child.emplace_back(d_par + e.cost, e.to);
    else
      d_child.emplace_back(e.cost + dist[e.to], e.to);
  }
  sort(d_child.rbegin(), d_child.rend());
  int ret = d_child[0].first + d_child[1].first;
  for (edge &e : g[idx]) {
    if (e.to == par)
      continue;
    ret = max(ret, dfs2(e.to, d_child[d_child[0].second == e.to].first, idx));
  }
  return (ret);
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    g[a].push_back((edge){b, w});
    g[b].push_back((edge){a, w});
  }

  dfs1(N / 2, -1);
  cout << dfs2(N / 2, 0, -1) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int to, cost;
};

vector<edge> g[100000];
long long dist[100000];

void dfs1(int idx, int par) {
  for (edge &e : g[idx]) {
    if (e.to == par)
      continue;
    dfs1(e.to, idx);
    dist[idx] = max(dist[idx], dist[e.to] + e.cost);
  }
}

int dfs2(int idx, int d_par, int par) {
  vector<pair<int, int>> d_child;
  d_child.emplace_back(0, -1);
  for (edge &e : g[idx]) {
    if (e.to == par)
      d_child.emplace_back(d_par + e.cost, e.to);
    else
      d_child.emplace_back(e.cost + dist[e.to], e.to);
  }
  sort(d_child.rbegin(), d_child.rend());
  int ret = d_child[0].first + d_child[1].first;
  for (edge &e : g[idx]) {
    if (e.to == par)
      continue;
    ret = max(ret, dfs2(e.to, d_child[d_child[0].second == e.to].first, idx));
  }
  return (ret);
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    g[a].push_back((edge){b, w});
    g[b].push_back((edge){a, w});
  }

  dfs1(0, -1);
  cout << dfs2(0, 0, -1) << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 100005;
struct edge {
  int u, v, w;
};
vector<edge> E;
vector<int> g[maxn];
int s, t, n, deep[maxn] = {0}, r[maxn];
bool vis[maxn] = {0};
void dfs(int u, int d) {
  deep[u] = d;
  vis[u] = 1;
  int size = g[u].size();
  for (int i = 0; i < size; i++) {
    edge e = E[g[u][i]];
    if (!vis[e.v])
      dfs(e.v, d + e.w);
  }
}
bool cmp(int a, int b) { return deep[a] > deep[b]; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    r[i] = i;
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    E.push_back((edge){u, v, w});
    g[u].push_back(E.size() - 1);
    E.push_back((edge){v, u, w});
    g[v].push_back(E.size() - 1);
  }
  r[0] = 0;
  dfs(0, 0);
  sort(r, r + n, cmp);
  s = r[0];
  memset(vis, 0, sizeof(vis));
  dfs(s, 0);
  sort(r, r + n, cmp);
  t = r[0];
  printf("%d\\n", deep[t]);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int du[100020], maxn = -1;
int n, g[100020], inq[100020], d[100020];
struct Edge {
  int u, v, w, next;
};
Edge e[100020 * 2];
void spfa(int s) {
  for (int i = 1; i <= n; i++) {
    inq[i] = 0;
    d[i] = 20000;
  }
  queue<int> q;
  while (!q.empty())
    q.pop();
  q.push(s);
  inq[s] = 1;
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = g[u]; i > 0; i = e[i].next) {
      int v = e[i].v, w = e[i].w;
      if (d[u] + w < d[v]) {
        d[v] = d[u] + w;
        if (inq[v] == 0) {
          q.push(v);
          inq[v] = 1;
        }
      }
    }
  }
  return;
}
int main() {
  scanf("%d", &n);
  int x, y, z;
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d%d", &x, &y, &z);
    x++;
    y++;
    e[i] = (Edge){x, y, z, g[x]};
    g[x] = i;
    e[i + n - 1] = (Edge){y, x, z, g[y]};
    g[y] = i + n - 1;
    du[x]++;
    du[y]++;
  }
  int s;
  for (int i = 1; i <= n; i++) {
    if (du[i] == 1) {
      s = i;
      break;
    }
  }
  spfa(s);
  for (int i = 1; i <= n; i++) {
    if (d[i] > maxn) {
      s = i;
      maxn = d[i];
    }
  }
  spfa(s);
  maxn = -1;
  for (int i = 1; i <= n; i++) {
    if (d[i] > maxn) {
      maxn = d[i];
    }
  }
  printf("%d\\n", maxn);
  return 0;
}