prithivMLmods/Coder-Stat huggingface dataset:
Problem p02370 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <vector>
#define N 10000
using namespace std;

list<int> l;
bool color[N] = {false};
vector<int> adj[N];
int n;

void topo(int k) {
  for (int i = 0; i < adj[k].size(); i++)
    topo(adj[k][i]);
  if (!color[k])
    l.push_front(k);
  color[k] = true;
}

int main(void) {
  int u, v;
  int e;
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!color[i])
      topo(i);
  }
  for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    cout << *it << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <vector>
#define N 10000
using namespace std;

list<int> l;
bool color[N] = {false};
vector<int> adj[N];
int n;

void topo(int k) {
  for (int i = 0; i < adj[k].size(); i++) {
    if (!color[adj[k][i]])
      topo(adj[k][i]);
  }
  if (!color[k])
    l.push_front(k);
  color[k] = true;
}

int main(void) {
  int u, v;
  int e;
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!color[i])
      topo(i);
  }
  for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    cout << *it << endl;
  return 0;
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):

// O(|E| + |V|)

#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>

using namespace std;

class TopologicalSort {
#define MAX_V 101
private:
  struct Edge {
    int to;
    Edge(int to) : to(to) {}
  };

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

public:
  TopologicalSort(int V) : V(V) {
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to) { G[from].push_back(Edge(to)); }

  vector<int> Solve() {
    int k = 0;
    vector<int> ord(V), in(V);
    for (int i = 0; i < V; i++) {
      for (auto &e : G[i]) {
        in[e.to]++;
      }
    }
    queue<int> q;
    for (int i = 0; i < V; i++) {
      if (in[i] == 0) {
        q.push(i);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      ord[k++] = v;
      for (auto &e : G[v]) {
        if (--in[e.to] == 0) {
          q.push(e.to);
        }
      }
    }

    return (*max_element(in.begin(), in.end()) == 0) ? ord : vector<int>();
  }
};

#include <iostream>

int main() {
  int V, E;
  cin >> V >> E;
  TopologicalSort ts(V);

  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    ts.AddEdge(s, t);
  }

  vector<int> ans = ts.Solve();

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:

// O(|E| + |V|)

#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>

using namespace std;

class TopologicalSort {
#define MAX_V 10001
private:
  struct Edge {
    int to;
    Edge(int to) : to(to) {}
  };

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

public:
  TopologicalSort(int V) : V(V) {
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to) { G[from].push_back(Edge(to)); }

  vector<int> Solve() {
    int k = 0;
    vector<int> ord(V), in(V);
    for (int i = 0; i < V; i++) {
      for (auto &e : G[i]) {
        in[e.to]++;
      }
    }
    queue<int> q;
    for (int i = 0; i < V; i++) {
      if (in[i] == 0) {
        q.push(i);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      ord[k++] = v;
      for (auto &e : G[v]) {
        if (--in[e.to] == 0) {
          q.push(e.to);
        }
      }
    }

    return (*max_element(in.begin(), in.end()) == 0) ? ord : vector<int>();
  }
};

#include <iostream>

int main() {
  int V, E;
  cin >> V >> E;
  TopologicalSort ts(V);

  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    ts.AddEdge(s, t);
  }

  vector<int> ans = ts.Solve();

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct edge {
  int src;
  int dst;
  int weight;
};

using graph = vector<vector<edge>>;

pair<int, int> visit(int v, int parent, const graph &G) {
  pair<int, int> r(0, v);
  for (const auto &e : G[v]) {
    if (e.dst == parent) {
      continue;
    }
    auto t = visit(e.dst, v, G);
    t.first += e.weight;
    if (r.first < t.first) {
      r = t;
    }
  }
  return r;
}

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

  graph G(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});
  }
  auto r = visit(0, -1, G);
  auto t = visit(r.second, -1, G);
  cout << t.first << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct edge {
  int src;
  int dst;
  int weight;
};

using graph = vector<vector<edge>>;

pair<int, int> visit(int v, int parent, const graph &G) {
  pair<int, int> r(0, v);
  for (const auto &e : G[v]) {
    if (e.dst == parent) {
      continue;
    }
    auto t = visit(e.dst, v, G);
    t.first += e.weight;
    if (r.first < t.first) {
      r = t;
    }
  }
  return r;
}

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

  graph G(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});
  }
  auto r = visit(0, -1, G);
  auto t = visit(r.second, -1, G);
  cout << t.first << endl;

  return 0;
}
Problem p02371 (C++)

Buggy submission (status: 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 = 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;
}


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 = 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;
}
Problem p02371 (C++)

Buggy submission (status: 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; 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;
}

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 - 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;
}
Problem p02371 (C++)

Buggy submission (status: 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;
}

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 (status: 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)
    !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;
}


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)
    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;
}
Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02371 (C++)

Buggy submission (status: 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; 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;
}


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 - 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;
}
Problem p02371 (C++)

Buggy submission (status: 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 + 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;
}

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);
  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;
}
Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

#define MAX_V 100

struct edge {
  int to, weight;
};

vector<edge> G[MAX_V];

pii dfs(int prev, int v) { // searching farthest node
  pii r(0, v);

  rep(i, G[v].size()) {
    struct edge e = G[v][i];
    if (e.to == prev)
      continue;
    pii tmp = dfs(v, e.to);
    tmp.first += e.weight;
    if (r.first < tmp.first)
      r = tmp;
  }
  return r;
}

int diameter() {
  pii r = dfs(-1, 0);
  pii t = dfs(-1, r.second);
  return t.first; // (r.second, t.second) is farthest pair
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].pb(edge{t, w});
    G[t].pb(edge{s, w});
  }
  cout << diameter() << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

#define MAX_V 100000

struct edge {
  int to, weight;
};

vector<edge> G[MAX_V];

pii dfs(int prev, int v) { // searching farthest node
  pii r(0, v);

  rep(i, G[v].size()) {
    struct edge e = G[v][i];
    if (e.to == prev)
      continue;
    pii tmp = dfs(v, e.to);
    tmp.first += e.weight;
    if (r.first < tmp.first)
      r = tmp;
  }
  return r;
}

int diameter() {
  pii r = dfs(-1, 0);
  pii t = dfs(-1, r.second);
  return t.first; // (r.second, t.second) is farthest pair
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].pb(edge{t, w});
    G[t].pb(edge{s, w});
  }
  cout << diameter() << endl;
}
Problem p02371 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02371 (C++)

Buggy submission (status: 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(0, -1);
  cout << dfs2(0, 0, -1) << endl;
}

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(N / 2, -1);
  cout << dfs2(N / 2, 0, -1) << endl;
}
Problem p02371 (C++)

Buggy submission (status: 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, 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;
}


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, 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;
}
Problem p02372 (C++)

Buggy submission (status: Time 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, height;
};

int dfs(int prev, int node, vector<vector<edge>> &adjacentList) {
  int res = 0;
  for (edge e : adjacentList[node]) {
    if (e.dest == prev)
      continue;
    if (e.height == -1)
      e.height = e.weight + dfs(node, e.dest, adjacentList);
    res = max(res, e.height);
  }
  return res;
}

vector<int> heightsOfTree(vector<vector<edge>> &adjacentList) {
  int n = adjacentList.size();
  vector<int> heights(n);
  for (int i = 0; i < n; i++)
    heights[i] = dfs(-1, i, adjacentList);
  return heights;
}

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

  vector<int> heights = heightsOfTree(adjacentList);
  for (int i : heights) {
    cout << i << endl;
  }

  return 0;
}

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

int dfs(int prev, int node, vector<vector<edge>> &adjacentList) {
  int res = 0;
  for (edge &e : adjacentList[node]) {
    if (e.dest == prev)
      continue;
    if (e.height == -1)
      e.height = e.weight + dfs(node, e.dest, adjacentList);
    res = max(res, e.height);
  }
  return res;
}

vector<int> heightsOfTree(vector<vector<edge>> &adjacentList) {
  int n = adjacentList.size();
  vector<int> heights(n);
  for (int i = 0; i < n; i++)
    heights[i] = dfs(-1, i, adjacentList);
  return heights;
}

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

  vector<int> heights = heightsOfTree(adjacentList);
  for (int i : heights) {
    cout << i << endl;
  }

  return 0;
}
Problem p02372 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int V, E, s, t, w;
vector<vector<pair<int, int>>> v(10001);
int visited[10001] = {0};
int ans[10001] = {0}, d[10001] = {0};

void di(int n) {
  visited[n] = 1;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i].first] == 0) {
      d[v[n][i].first] = d[n] + v[n][i].second;
      di(v[n][i].first);
    }
  }
}

/*void dfs(int n){
        visited[n] = 1;
        for(int i=0;i<v[n].size();i++){
                if(visited[v[n][i].first]==0){
                        dfs(v[n][i].first);
                        d[n] = max(d[n],d[v[n][i].first]+v[n][i].second);
                }
        }
}
*/
int main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> s >> t >> w;
    v[s].push_back({t, w});
    v[t].push_back({s, w});
  }
  di(0);
  int a, b, va = 0, vb = 0;
  for (int i = 0; i < V; i++) {
    if (va < d[i]) {
      a = i;
      va = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    d[i] = 0;
  }
  di(a);
  for (int i = 0; i < V; i++) {
    if (vb < d[i]) {
      b = i;
      vb = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    ans[i] = d[i];
    d[i] = 0;
  }
  di(b);
  for (int i = 0; i < V; i++) {
    ans[i] = max(d[i], ans[i]);
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int V, E, s, t, w;
vector<vector<pair<int, int>>> v(10001);
int visited[10001] = {0};
int ans[10001] = {0}, d[10001] = {0};

void di(int n) {
  visited[n] = 1;
  for (int i = 0; i < v[n].size(); i++) {
    if (visited[v[n][i].first] == 0) {
      d[v[n][i].first] = d[n] + v[n][i].second;
      di(v[n][i].first);
    }
  }
}

/*void dfs(int n){
        visited[n] = 1;
        for(int i=0;i<v[n].size();i++){
                if(visited[v[n][i].first]==0){
                        dfs(v[n][i].first);
                        d[n] = max(d[n],d[v[n][i].first]+v[n][i].second);
                }
        }
}
*/
int main() {
  cin >> V;
  for (int i = 0; i < V - 1; i++) {
    cin >> s >> t >> w;
    v[s].push_back({t, w});
    v[t].push_back({s, w});
  }
  di(0);
  int a = 0, b = 0, va = 0, vb = 0;
  for (int i = 0; i < V; i++) {
    if (va < d[i]) {
      a = i;
      va = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    d[i] = 0;
  }
  di(a);
  for (int i = 0; i < V; i++) {
    if (vb < d[i]) {
      b = i;
      vb = d[i];
    }
  }
  for (int i = 0; i < V; i++) {
    visited[i] = 0;
    ans[i] = d[i];
    d[i] = 0;
  }
  di(b);
  for (int i = 0; i < V; i++) {
    ans[i] = max(d[i], ans[i]);
    cout << ans[i] << endl;
  }
}
Problem p02372 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for (Int i = 0; i < (Int)(n); ++i)
#define guard(x)                                                               \
  if (not(x))                                                                  \
    continue;
#ifndef LOCAL_
#define fprintf                                                                \
  if (false)                                                                   \
  fprintf
#endif

int main() {
  Int n;
  std::cin >> n;
  std::vector<Int> ss(n - 1), ts(n - 1), ws(n - 1);
  rep(i, n - 1) std::cin >> ss[i] >> ts[i] >> ws[i];
  std::vector<std::vector<std::pair<Int, Int>>> nexts(n);
  rep(i, n - 1) {
    Int s = ss[i], t = ts[i], w = ws[i];
    nexts[s].emplace_back(t, w);
    nexts[t].emplace_back(s, w);
  }
  std::vector<Int> xs(n, -1);
  std::vector<Int> ys(n, -1);
  Int as[2];
  for (Int t = 0; t < 2; ++t) {
    Int c = t == 0 ? 0 : as[0];
    std::vector<Int> &zs = t == 0 ? xs : ys;
    std::queue<Int> q;
    zs[c] = 0;
    q.emplace(c);
    while (not q.empty()) {
      Int i = q.front();
      q.pop();
      for (auto kw : nexts[i]) {
        Int k, w;
        std::tie(k, w) = kw;
        guard(zs[k] == -1);
        zs[k] = zs[i] + w;
        q.emplace(k);
      }
    }
    rep(i, n) if (zs[as[t]] < zs[i]) as[t] = i;
  }
  rep(i, n) printf("%ld\n", std::max(xs[i], ys[i]));
}


Fixed submission:
#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for (Int i = 0; i < (Int)(n); ++i)
#define guard(x)                                                               \
  if (not(x))                                                                  \
    continue;
#ifndef LOCAL_
#define fprintf                                                                \
  if (false)                                                                   \
  fprintf
#endif

int main() {
  Int n;
  std::cin >> n;
  std::vector<Int> ss(n - 1), ts(n - 1), ws(n - 1);
  rep(i, n - 1) std::cin >> ss[i] >> ts[i] >> ws[i];
  std::vector<std::vector<std::pair<Int, Int>>> nexts(n);
  rep(i, n - 1) {
    Int s = ss[i], t = ts[i], w = ws[i];
    nexts[s].emplace_back(t, w);
    nexts[t].emplace_back(s, w);
  }
  std::vector<Int> xs(n, -1);
  std::vector<Int> ys(n, -1);
  Int as[3] = {};
  for (Int t = 0; t < 3; ++t) {
    Int c = as[std::max((Int)0, t - 1)];
    std::vector<Int> &zs = (t % 2 == 0) ? xs : ys;
    zs.clear();
    zs.resize(n, -1);
    std::queue<Int> q;
    zs[c] = 0;
    q.emplace(c);
    while (not q.empty()) {
      Int i = q.front();
      q.pop();
      for (auto kw : nexts[i]) {
        Int k, w;
        std::tie(k, w) = kw;
        guard(zs[k] == -1);
        zs[k] = zs[i] + w;
        q.emplace(k);
      }
    }
    rep(i, n) if (zs[as[t]] < zs[i]) as[t] = i;
  }
  rep(i, n) printf("%ld\n", std::max(xs[i], ys[i]));
}
Problem p02373 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vii> vvii;
typedef vector<int>::iterator vit;

const int MAXN = 100005;
const int LOGN = 50;

int idx[MAXN]; // i : index of node i in dfs path
ii path[MAXN << 1];
vi adjList[MAXN];

ii ST[LOGN][MAXN << 1];

void build(int n) {
  int h = (n == 1 ? 1 : ceil(log2(n)));

  for (int i = 0; i < n; i++)
    ST[0][i] = path[i];
  for (int i = 1; i < h; i++)
    for (int j = 0; j + (1 << i) <= n; j++)
      ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << (i - 1))]);
}

int RMQ(int l, int r) { // range [l,r)
  int p = 31 - __builtin_clz(r - l);
  return min(ST[p][l], ST[p][r - (1 << p)]).se;
}

// build dfs path, depth
int cnt = 0;
void dfs(int curr, int d) {
  idx[curr] = cnt;

  path[cnt++] = ii(d, curr);
  for (vit it = adjList[curr].begin(); it != adjList[curr].end(); it++) {
    dfs(*it, d + 1);

    path[cnt++] = ii(d, curr);
  }
}

int N, M, Q, v;

int main() {

  scanf("%d", &N);
  for (int u = 0; u < N; u++) {
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
      scanf("%d", &v);
      adjList[u].push_back(v);
    }
  }

  // rooted as 0
  dfs(0, 0);
  build(cnt);

  scanf("%d", &Q);
  for (int i = 0, u, v, l, r; i < Q; i++) {
    scanf("%d%d", &u, &v);

    l = idx[u], r = idx[v];
    if (l > r)
      swap(l, r);

    printf("%d\n", RMQ(l, r + 1));
  }

  return 0;
}

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

#define fi first
#define se second

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vii> vvii;
typedef vector<int>::iterator vit;

const int MAXN = 100005;
const int LOGN = 20;

int idx[MAXN]; // i : index of node i in dfs path
ii path[MAXN << 1];
vi adjList[MAXN];

ii ST[LOGN][MAXN << 1];

void build(int n) {
  int h = (n == 1 ? 1 : ceil(log2(n)));

  for (int i = 0; i < n; i++)
    ST[0][i] = path[i];
  for (int i = 1; i < h; i++)
    for (int j = 0; j + (1 << i) <= n; j++)
      ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << (i - 1))]);
}

int RMQ(int l, int r) { // range [l,r)
  int p = 31 - __builtin_clz(r - l);
  return min(ST[p][l], ST[p][r - (1 << p)]).se;
}

// build dfs path, depth
int cnt = 0;
void dfs(int curr, int d) {
  idx[curr] = cnt;

  path[cnt++] = ii(d, curr);
  for (vit it = adjList[curr].begin(); it != adjList[curr].end(); it++) {
    dfs(*it, d + 1);

    path[cnt++] = ii(d, curr);
  }
}

int N, M, Q, v;

int main() {

  scanf("%d", &N);
  for (int u = 0; u < N; u++) {
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
      scanf("%d", &v);
      adjList[u].push_back(v);
    }
  }

  // rooted as 0
  dfs(0, 0);
  build(cnt);

  scanf("%d", &Q);
  for (int i = 0, u, v, l, r; i < Q; i++) {
    scanf("%d%d", &u, &v);

    l = idx[u], r = idx[v];
    if (l > r)
      swap(l, r);

    printf("%d\n", RMQ(l, r + 1));
  }

  return 0;
}
Problem p02373 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAX = 1e5;
pii table[17][MAX * 2 - 1];

int n;
vector<int> G[MAX];
int id[MAX];

int it = 0;

void dfs(int v, int d) {
  id[v] = it;
  table[0][it++] = {d, v};
  for (auto to : G[v]) {
    dfs(to, d + 1);
    table[0][it++] = {d, v};
  }
}

void init() {
  dfs(0, 0);
  int m = (n << 1) - 1;
  int h = 31 - __builtin_clz(m);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j + (1 << i) < m; j++) {
      table[i + 1][j] = min(table[i][j], table[i][j + (1 << i)]);
    }
  }
}

int lca(int u, int v) {
  u = id[u], v = id[v];
  if (u > v)
    swap(u, v);
  int b = 31 - __builtin_clz(v + 1 - u);
  return min(table[b][u], table[b][v + 1 - (1 << b)]).second;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].push_back(c);
    }
  }
  init();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    printf("%d\n", lca(u, v));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAX = 1e5;
pii table[18][MAX * 2 - 1];

int n;
vector<int> G[MAX];
int id[MAX];

int it = 0;

void dfs(int v, int d) {
  id[v] = it;
  table[0][it++] = {d, v};
  for (auto to : G[v]) {
    dfs(to, d + 1);
    table[0][it++] = {d, v};
  }
}

void init() {
  dfs(0, 0);
  int m = (n << 1) - 1;
  int h = 31 - __builtin_clz(m);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j + (1 << i) < m; j++) {
      table[i + 1][j] = min(table[i][j], table[i][j + (1 << i)]);
    }
  }
}

int lca(int u, int v) {
  u = id[u], v = id[v];
  if (u > v)
    swap(u, v);
  int b = 31 - __builtin_clz(v + 1 - u);
  return min(table[b][u], table[b][v + 1 - (1 << b)]).second;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].push_back(c);
    }
  }
  init();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    printf("%d\n", lca(u, v));
  }
  return 0;
}
Problem p02373 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define MAXV 1000000
#define MAXLV 20
using namespace std;

vector<int> e[MAXV];
int n, root = 0;

int parent[MAXLV][MAXV];
int depth[MAXV];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < e[v].size(); i++) {
    if (e[v][i] != p)
      dfs(e[v][i], v, d + 1);
  }
}

void init() {
  dfs(root, -1, 0);
  for (int k = 0; k + 1 < MAXLV; k++) {
    for (int v = 0; v < n; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAXLV; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  for (int k = MAXLV - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main() {
  int q, u, v, k, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> c;
      e[i].push_back(c);
    }
  }
  init();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXV 100010
#define MAXLV 20
using namespace std;

vector<int> e[MAXV];
int n, root = 0;

int parent[MAXLV][MAXV];
int depth[MAXV];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < e[v].size(); i++) {
    if (e[v][i] != p)
      dfs(e[v][i], v, d + 1);
  }
}

void init() {
  dfs(root, -1, 0);
  for (int k = 0; k + 1 < MAXLV; k++) {
    for (int v = 0; v < n; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAXLV; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  for (int k = MAXLV - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main() {
  int q, u, v, k, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> c;
      e[i].push_back(c);
    }
  }
  init();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}