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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100010];

pair<int, int> tddfs(int v, int par = -1) {
  pair<int, int> ret = {0, v};
  for (auto &x : e[v]) {
    if (x == par)
      continue;
    auto r = tddfs(x, v);
    ret = max(ret, {1 + r.first, r.second});
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e[u].emplace_back(v);
    e[v].emplace_back(u);
  }
  auto c = tddfs(0);
  auto w = tddfs(c.second);
  if (w.first % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> e[200010];

pair<int, int> tddfs(int v, int par = -1) {
  pair<int, int> ret = {0, v};
  for (auto &x : e[v]) {
    if (x == par)
      continue;
    auto r = tddfs(x, v);
    ret = max(ret, {1 + r.first, r.second});
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e[u].emplace_back(v);
    e[v].emplace_back(u);
  }
  auto c = tddfs(0);
  auto w = tddfs(c.second);
  if (w.first % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}
Problem p03055 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(2 * 10**5)


def dfs(v, p, links):
    farthest_d = 0
    farthest_v = v

    for u in links[v]:
        if u == p:
            continue
        res_d, res_v = dfs(u, v, links)
        if res_d > farthest_d:
            farthest_d = res_d
            farthest_v = res_v
    return farthest_d + 1, farthest_v


def solve(n, links):
    if n == 1:
        return True

    d, v = dfs(0, -1, links)
    d, v = dfs(v, -1, links)
    if (d + 1) % 3 == 0:
        return False
    return True


n = int(input())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print("First" if solve(n, links) else "Second")


Fixed submission:
import sys

sys.setrecursionlimit(2 * 10**5 + 5)


def dfs(v, p, links):
    farthest_d = 0
    farthest_v = v

    for u in links[v]:
        if u == p:
            continue
        res_d, res_v = dfs(u, v, links)
        if res_d > farthest_d:
            farthest_d = res_d
            farthest_v = res_v
    return farthest_d + 1, farthest_v


def solve(n, links):
    if n == 1:
        return True

    d, v = dfs(0, -1, links)
    d, v = dfs(v, -1, links)
    if (d + 1) % 3 == 0:
        return False
    return True


n = int(input())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print("First" if solve(n, links) else "Second")
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int N = 100010;

struct Edge {
  int nex, v;
} edge[N << 1];
int tp;

int n, e[N], sg[N], f[N], Ans;

inline void add(int x, int y) {
  edge[++tp].v = y;
  edge[tp].nex = e[x];
  e[x] = tp;
  return;
}

void DFS(int x, int fa) {
  int a = 1, b = 0;
  for (int i = e[x]; i; i = edge[i].nex) {
    int y = edge[i].v;
    if (y == fa)
      continue;
    DFS(y, x);
    if (a < f[y] + 1) {
      b = a;
      a = f[y] + 1;
    } else {
      b = std::max(b, f[y] + 1);
    }
  }
  Ans = std::max(Ans, std::max(a + b - 1, a));
  f[x] = a;
  return;
}

int main() {

  int n;
  scanf("%d", &n);
  for (int i = 1, x, y; i < n; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }

  DFS(1, 0);

  /// cal Ans
  sg[1] = 1;
  sg[2] = 0;
  for (int i = 3; i <= Ans; i++) {
    if (std::min(sg[i - 1], sg[i - 2]) == 0) {
      sg[i] = 1;
    } else {
      sg[i] = 0;
    }
  }

  if (sg[Ans]) {
    printf("First\n");
  } else {
    printf("Second\n");
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

const int N = 200010;

struct Edge {
  int nex, v;
} edge[N << 1];
int tp;

int n, e[N], sg[N], f[N], Ans;

inline void add(int x, int y) {
  edge[++tp].v = y;
  edge[tp].nex = e[x];
  e[x] = tp;
  return;
}

void DFS(int x, int fa) {
  int a = 1, b = 0;
  for (int i = e[x]; i; i = edge[i].nex) {
    int y = edge[i].v;
    if (y == fa)
      continue;
    DFS(y, x);
    if (a < f[y] + 1) {
      b = a;
      a = f[y] + 1;
    } else {
      b = std::max(b, f[y] + 1);
    }
  }
  Ans = std::max(Ans, std::max(a + b - 1, a));
  f[x] = a;
  return;
}

int main() {

  int n;
  scanf("%d", &n);
  for (int i = 1, x, y; i < n; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }

  DFS(1, 0);

  /// cal Ans
  sg[1] = 1;
  sg[2] = 0;
  for (int i = 3; i <= Ans; i++) {
    if (std::min(sg[i - 1], sg[i - 2]) == 0) {
      sg[i] = 1;
    } else {
      sg[i] = 0;
    }
  }

  if (sg[Ans]) {
    printf("First\n");
  } else {
    printf("Second\n");
  }

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define nmax 100005
using namespace std;
int n, nod1, viz1[nmax], viz2[nmax], maxi1, maxi2, nod2;
vector<int> v[nmax];
void dfs1(int nod) {
  for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
    if (!viz1[*it]) {
      viz1[*(it)] = viz1[nod] + 1;
      if (maxi1 < (viz1[*(it)])) {
        nod1 = *(it);
        maxi1 = viz1[*(it)];
      }
      dfs1(*it);
    }
}
void dfs2(int nod) {
  for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
    if (!viz2[*it]) {
      viz2[*(it)] = viz2[nod] + 1;
      if (maxi2 < (viz2[*(it)])) {
        nod2 = *(it);
        maxi2 = viz2[*(it)];
      }
      dfs2(*it);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int i, x, y;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  maxi1 = 1;
  nod1 = 1;
  viz1[1] = 1;
  dfs1(1);

  viz2[nod1] = 1;
  maxi2 = 1;
  dfs2(nod1);

  if (maxi2 % 3 == 2)
    cout << "Second\n";
  else
    cout << "First\n";
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
#define nmax 200005
using namespace std;
int n, nod1, viz1[nmax], viz2[nmax], maxi1, maxi2, nod2;
vector<int> v[nmax];
void dfs1(int nod) {
  for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
    if (!viz1[*it]) {
      viz1[*(it)] = viz1[nod] + 1;
      if (maxi1 < (viz1[*(it)])) {
        nod1 = *(it);
        maxi1 = viz1[*(it)];
      }
      dfs1(*it);
    }
}
void dfs2(int nod) {
  for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
    if (!viz2[*it]) {
      viz2[*(it)] = viz2[nod] + 1;
      if (maxi2 < (viz2[*(it)])) {
        nod2 = *(it);
        maxi2 = viz2[*(it)];
      }
      dfs2(*it);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int i, x, y;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  maxi1 = 1;
  nod1 = 1;
  viz1[1] = 1;
  dfs1(1);

  viz2[nod1] = 1;
  maxi2 = 1;
  dfs2(nod1);

  if (maxi2 % 3 == 2)
    cout << "Second\n";
  else
    cout << "First\n";
  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj(200200);

int depth[200200];
int dfs2(int i, int p, int d = 0) {
  depth[i] = d;
  for (int j : adj[i])
    if (p != j)
      dfs2(j, i, d + 1);
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs2(0, -1);
  int s, t, dmax = 0;
  for (int i = 0; i < n; i++)
    if (dmax < depth[i]) {
      dmax = depth[i];
      s = i;
    }
  dmax = 0;
  memset(depth, 0, sizeof(depth));
  dfs2(s, -1);
  for (int i = 0; i < n; i++)
    if (dmax < depth[i]) {
      dmax = depth[i];
      t = i;
    }
  cerr << s << ' ' << t << ' ' << depth[t] << endl;
  cout << ((depth[t] % 3 != 1) ? "First" : "Second") << endl;
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj(200200);

int depth[200200];
int dfs2(int i, int p, int d = 0) {
  depth[i] = d;
  for (int j : adj[i])
    if (p != j)
      dfs2(j, i, d + 1);
}

int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << "First\n";
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs2(0, -1);
  int s, t, dmax = 0;
  for (int i = 0; i < n; i++)
    if (dmax < depth[i]) {
      dmax = depth[i];
      s = i;
    }
  dmax = 0;
  memset(depth, 0, sizeof(depth));
  dfs2(s, -1);
  for (int i = 0; i < n; i++)
    if (dmax < depth[i]) {
      dmax = depth[i];
      t = i;
    }
  cerr << s << ' ' << t << ' ' << depth[t] << endl;
  cout << ((depth[t] % 3 != 1) ? "First" : "Second") << endl;
}
Problem p03055 (C++)

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

using namespace std;

using Graph = vector<vector<int>>;

constexpr int INF = numeric_limits<int>::max() / 3;

void dfs(Graph const &tree, vector<int> &dists, int pos) {
  for (auto v : tree[pos]) {
    if (dists[v] < INF)
      continue;
    dists[v] = dists[pos] + 1;
    dfs(tree, dists, v);
  }
}

int diameter(Graph const &tree) {
  int V = tree.size();
  vector<int> dists(V, INF);
  dists[0] = 0;
  dfs(tree, dists, 0);
  auto farthest = max_element(begin(dists), end(dists));
  int v = farthest - begin(dists);
  vector<int> dists2(V, INF);
  dists2[v] = 0;
  dfs(tree, dists2, v);
  return *max_element(begin(dists2), end(dists2));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  Graph tree(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  int d = diameter(tree);
  cout << (d % 3 == 1 ? "Second" : "First") << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

using Graph = vector<vector<int>>;

constexpr int INF = numeric_limits<int>::max() / 3;

void dfs(Graph const &tree, vector<int> &dists, int pos) {
  for (auto v : tree[pos]) {
    if (dists[v] < INF)
      continue;
    dists[v] = dists[pos] + 1;
    dfs(tree, dists, v);
  }
}

int diameter(Graph const &tree) {
  int V = tree.size();
  vector<int> dists(V, INF);
  dists[0] = 0;
  dfs(tree, dists, 0);
  auto farthest = max_element(begin(dists), end(dists));
  int v = farthest - begin(dists);
  vector<int> dists2(V, INF);
  dists2[v] = 0;
  dfs(tree, dists2, v);
  return *max_element(begin(dists2), end(dists2));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  Graph tree(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  int d = diameter(tree);
  cout << (d % 3 == 1 ? "Second" : "First") << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

vector<vector<int>> neighbor;

void dfs(int node, int d, vector<int> &dist, vector<bool> &isVisited) {
  isVisited[node] = true;
  dist[node] = d;

  for (int ch : neighbor[node]) {
    if (!isVisited[ch]) {
      dfs(ch, d + 1, dist, isVisited);
    }
  }
}

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

  neighbor.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    neighbor[a].push_back(b);
    neighbor[b].push_back(a);
  }

  vector<int> dist(n);
  vector<bool> isVisited(n, false);
  dfs(0, 0, dist, isVisited);
  int d = 0, v;
  rep(i, n) {
    if (d < dist[i]) {
      d = dist[i];
      v = i;
    }
  }

  fill(isVisited.begin(), isVisited.end(), false);
  dfs(v, 0, dist, isVisited);
  int diameter = 0;
  rep(i, n) { diameter = max(diameter, dist[i]); }

  if ((diameter + 1) % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

vector<vector<int>> neighbor;

void dfs(int node, int d, vector<int> &dist, vector<bool> &isVisited) {
  isVisited[node] = true;
  dist[node] = d;

  for (int ch : neighbor[node]) {
    if (!isVisited[ch]) {
      dfs(ch, d + 1, dist, isVisited);
    }
  }
}

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

  neighbor.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    neighbor[a].push_back(b);
    neighbor[b].push_back(a);
  }

  vector<int> dist(n);
  vector<bool> isVisited(n, false);
  dfs(0, 0, dist, isVisited);
  int d = 0, v = 0;
  rep(i, n) {
    if (d < dist[i]) {
      d = dist[i];
      v = i;
    }
  }

  fill(isVisited.begin(), isVisited.end(), false);
  dfs(v, 0, dist, isVisited);
  int diameter = 0;
  rep(i, n) { diameter = max(diameter, dist[i]); }

  if ((diameter + 1) % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> e(n + 1);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  vector<int> d(n + 1);
  using P = pair<int, int>;
  function<void(int, vector<int> &)> dfs = [&](int s, vector<int> &d) {
    queue<P> que;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.front();
      int v = p.second;
      int cost = p.first;
      que.pop();
      for (auto next : e[v]) {
        if (next != s && d[next] == 0) {
          que.push(P(cost + 1, next));
          d[next] = cost + 1;
        }
      }
    }
  };
  dfs(1, d);
  int ma = 0;
  int pos = 0;
  for (int i = 1; i <= n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  vector<int> d2(n + 1);
  dfs(pos, d2);
  int ma2 = 0;

  for (int i = 1; i <= n; i++) {
    if (ma2 < d2[i]) {
      ma2 = d2[i];
    }
  }
  if ((ma2 + 1) % 3 != 2) {
    cout << "First" << endl;
  } else
    cout << "Second" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> e(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  vector<int> d(n + 1);
  using P = pair<int, int>;
  function<void(int, vector<int> &)> dfs = [&](int s, vector<int> &d) {
    queue<P> que;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.front();
      int v = p.second;
      int cost = p.first;
      que.pop();
      for (auto next : e[v]) {
        if (next != s && d[next] == 0) {
          que.push(P(cost + 1, next));
          d[next] = cost + 1;
        }
      }
    }
  };
  dfs(1, d);
  int ma = 0;
  int pos = 0;
  for (int i = 1; i <= n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  vector<int> d2(n + 1);
  dfs(pos, d2);
  int ma2 = 0;

  for (int i = 1; i <= n; i++) {
    if (ma2 < d2[i]) {
      ma2 = d2[i];
    }
  }
  if ((ma2 + 1) % 3 != 2) {
    cout << "First" << endl;
  } else
    cout << "Second" << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> d(n);
  for (int i = 0; i < n; i++) {
    d[i] = -1;
  }
  function<void(int)> dfs = [&](int v) {
    for (int nx : g[v]) {
      if (d[nx] != -1)
        continue;
      d[nx] = d[v] + 1;
      dfs(nx);
    }
  };
  d[0] = 0;
  dfs(0);
  int pos = 0;
  int ma = 0;
  for (int i = 0; i < n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  for (int i = 0; i < n; i++) {
    d[i] = -1;
  }
  d[pos] = 0;
  dfs(pos);
  ma = 0;
  pos = 0;
  for (int i = 0; i < n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  ma++;
  if (ma % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> d(n);
  for (int i = 0; i < n; i++) {
    d[i] = -1;
  }
  function<void(int)> dfs = [&](int v) {
    for (int nx : g[v]) {
      if (d[nx] != -1)
        continue;
      d[nx] = d[v] + 1;
      dfs(nx);
    }
  };
  d[0] = 0;
  dfs(0);
  int pos = 0;
  int ma = 0;
  for (int i = 0; i < n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  for (int i = 0; i < n; i++) {
    d[i] = -1;
  }
  d[pos] = 0;
  dfs(pos);
  ma = 0;
  pos = 0;
  for (int i = 0; i < n; i++) {
    if (ma < d[i]) {
      ma = d[i];
      pos = i;
    }
  }
  ma++;
  if (ma % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int MAXN = 8e4 + 5, MAXM = 2e5 + 5, BOUND = 1e7, MOD = 1073741824,
          INF = 0x3f3f3f3f, base = 10000;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0), eps = 1e-9;
#define mid l + ((r - l) >> 1)
#define lson o << 1, l, m
#define rson o << 1 | 1, m + 1, r
#define lc(x) ch[x][0]
#define pii pair<int, int>
#define vi vector<int>
#define RR register int
#define rc(x) ch[x][1]
#define rep(i, a, b) for (RR i = (a); i <= (b); ++i)
#define random(a, b) ((a) + rand() % ((b) - (a) + 1))
int n, x, y, d[MAXN], diameter;
vi G[MAXN];
void dfs(int u, int f) {
  int mx = 0;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v == f)
      continue;
    dfs(v, u);
    diameter = max(diameter, mx + d[v] + 1);
    mx = max(mx, d[v] + 1);
    d[u] = max(d[v] + 1, d[u]);
  }
}
int main() {
  cin >> n;
  rep(i, 2, n) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  if (diameter % 3 == 1)
    cout << "Second\n";
  else
    cout << "First\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int MAXN = 2e5 + 5, MAXM = 2e5 + 5, BOUND = 1e7, MOD = 1073741824,
          INF = 0x3f3f3f3f, base = 10000;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0), eps = 1e-9;
#define mid l + ((r - l) >> 1)
#define lson o << 1, l, m
#define rson o << 1 | 1, m + 1, r
#define lc(x) ch[x][0]
#define pii pair<int, int>
#define vi vector<int>
#define RR register int
#define rc(x) ch[x][1]
#define rep(i, a, b) for (RR i = (a); i <= (b); ++i)
#define random(a, b) ((a) + rand() % ((b) - (a) + 1))
int n, x, y, d[MAXN], diameter;
vi G[MAXN];
void dfs(int u, int f) {
  int mx = 0;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v == f)
      continue;
    dfs(v, u);
    diameter = max(diameter, mx + d[v] + 1);
    mx = max(mx, d[v] + 1);
    d[u] = max(d[v] + 1, d[u]);
  }
}
int main() {
  cin >> n;
  rep(i, 2, n) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  if (diameter % 3 == 1)
    cout << "Second\n";
  else
    cout << "First\n";
  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, top, x, y, ans, ma, dis[N], head[N];
struct node {
  int too, next;
} edge[N * 2];
void add(int a, int b) {
  edge[++top].too = b;
  edge[top].next = head[a];
  head[a] = top;
}
void dfs(int u, int fa) {
  dis[u] = dis[fa] + 1;
  if (dis[u] > ma) {
    ma = dis[u];
    ans = u;
  }
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].too;
    if (v == fa)
      continue;
    dfs(v, u);
  }
}
int main() {
  int T;
  T = 1;
  while (T--) {
    scanf("%d", &n);
    top = 0;
    memset(head, 0, sizeof(head));
    for (int i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      add(x, y);
      add(y, x);
    }
    memset(dis, 0, sizeof(dis));
    ma = 0, ans = 0;
    dfs(1, 0);
    memset(dis, 0, sizeof(dis));
    x = ans;
    ma = 0, ans = 0;
    dfs(x, 0);
    if (ma == 1) {
      puts("First");
      continue;
    }
    if ((ma - 2) % 3 == 0)
      puts("Second");
    else
      puts("First");
  }
}
/*
如果选的是叶子，最后一层就只有这一个点了
如果不是叶子，叶子全消除
说明要么去掉一层，要么去掉两层
直径一定是两个叶子
*/
/*
3
3
1 2
2 3
6
1 2
2 3
2 4
4 6
5 6
7
1 7
7 4
3 4
7 5
6 3
2 1
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, top, x, y, ans, ma, dis[N], head[N];
struct node {
  int too, next;
} edge[N * 2];
void add(int a, int b) {
  edge[++top].too = b;
  edge[top].next = head[a];
  head[a] = top;
}
void dfs(int u, int fa) {
  dis[u] = dis[fa] + 1;
  if (dis[u] > ma) {
    ma = dis[u];
    ans = u;
  }
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].too;
    if (v == fa)
      continue;
    dfs(v, u);
  }
}
int main() {
  int T;
  T = 1;
  while (T--) {
    scanf("%d", &n);
    top = 0;
    memset(head, 0, sizeof(head));
    for (int i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      add(x, y);
      add(y, x);
    }
    memset(dis, 0, sizeof(dis));
    ma = 0, ans = 0;
    dfs(1, 0);
    memset(dis, 0, sizeof(dis));
    x = ans;
    ma = 0, ans = 0;
    dfs(x, 0);
    if (ma == 1) {
      puts("First");
      continue;
    }
    if ((ma - 2) % 3 == 0)
      puts("Second");
    else
      puts("First");
  }
}
/*
如果选的是叶子，最后一层就只有这一个点了
如果不是叶子，叶子全消除
说明要么去掉一层，要么去掉两层
直径一定是两个叶子
*/
/*
3
3
1 2
2 3
6
1 2
2 3
2 4
4 6
5 6
7
1 7
7 4
3 4
7 5
6 3
2 1
*/
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// T1: Give you a tree.
// You can delete all of the leaves or save one leave when it's your turn.
// The person who can't do anything will lose the game.

const int N = 200005;

int n, head[N];

struct Edge {
  int next, to;
};
Edge E[N << 1];
int tot;
void add(int u, int v) {
  E[++tot].next = head[u];
  E[tot].to = v;
  head[u] = tot;
}

namespace subtask3 {

int main() {
  if (n % 3 == 2)
    puts("Second");
  else
    puts("First");
  return 0;
}

} // namespace subtask3

int dis[N];
void dfs(int u, int fa) {
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (v == fa)
      continue;
    dis[v] = dis[u] + 1;
    dfs(v, u);
  }
}

int rho() {
  dis[1] = 0;
  dfs(1, 0);
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if (dis[i] > dis[res])
      res = i;
  }
  dis[res] = 0;
  dfs(res, 0);
  for (int i = 1; i <= n; ++i) {
    if (dis[i] > dis[res])
      res = i;
  }
  return dis[res] + 1;
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    bool s3_flag = 1;
    tot = 0;
    memset(head, 0x00, sizeof(head));
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) {
      int u, v;
      scanf("%d%d", &u, &v);
      add(u, v);
      add(v, u);
      if (u != i || v != i + 1)
        s3_flag = 0;
    }
    if (s3_flag) {
      subtask3::main();
      continue;
    }
    n = rho();
    subtask3::main();
  }
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// T1: Give you a tree.
// You can delete all of the leaves or save one leave when it's your turn.
// The person who can't do anything will lose the game.

const int N = 200005;

int n, head[N];

struct Edge {
  int next, to;
};
Edge E[N << 1];
int tot;
void add(int u, int v) {
  E[++tot].next = head[u];
  E[tot].to = v;
  head[u] = tot;
}

namespace subtask3 {

int main() {
  if (n % 3 == 2)
    puts("Second");
  else
    puts("First");
  return 0;
}

} // namespace subtask3

int dis[N];
void dfs(int u, int fa) {
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (v == fa)
      continue;
    dis[v] = dis[u] + 1;
    dfs(v, u);
  }
}

int rho() {
  dis[1] = 0;
  dfs(1, 0);
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    if (dis[i] > dis[res])
      res = i;
  }
  dis[res] = 0;
  dfs(res, 0);
  for (int i = 1; i <= n; ++i) {
    if (dis[i] > dis[res])
      res = i;
  }
  return dis[res] + 1;
}

int main() {
  int T = 1;
  while (T--) {
    bool s3_flag = 1;
    tot = 0;
    memset(head, 0x00, sizeof(head));
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) {
      int u, v;
      scanf("%d%d", &u, &v);
      add(u, v);
      add(v, u);
      if (u != i || v != i + 1)
        s3_flag = 0;
    }
    if (s3_flag) {
      subtask3::main();
      continue;
    }
    n = rho();
    subtask3::main();
  }
}
Problem p03055 (C++)

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

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

const int N = 200000;
int n;
vector<int> graph[N];

void dfs(int cur, int par, int d, int &dist, int &deepest) {
  if (d > dist) {
    deepest = cur;
    dist = d;
  }
  for (int child : graph[cur]) {
    if (child != par) {
      dfs(child, cur, d + 1, dist, deepest);
    }
  }
}

int findDiam() {
  int deepest = 1, dist = 0;
  dfs(1, 0, 0, dist, deepest);

  int ignore = 1, diam = 0;
  dfs(deepest, 0, 0, diam, ignore);
  return diam;
}

bool firstWins(int diam) {
  if (diam == 0)
    return true;
  int m = diam % 3;
  return m == 0 || m == 2;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  int diam = findDiam();

  if (firstWins(diam)) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}


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

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

const int N = 200000;
int n;
vector<int> graph[N + 1];

void dfs(int cur, int par, int d, int &dist, int &deepest) {
  if (d > dist) {
    deepest = cur;
    dist = d;
  }
  for (int child : graph[cur]) {
    if (child != par) {
      dfs(child, cur, d + 1, dist, deepest);
    }
  }
}

int findDiam() {
  int deepest = 1, dist = 0;
  dfs(1, 0, 0, dist, deepest);

  int ignore = 1, diam = 0;
  dfs(deepest, 0, 0, diam, ignore);
  return diam;
}

bool firstWins(int diam) {
  if (diam == 0)
    return true;
  int m = diam % 3;
  return m == 0 || m == 2;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  int diam = findDiam();

  if (firstWins(diam)) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

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

  struct Element {
    ll cost, node;
    bool operator<(const Element &rhs) const { return cost > rhs.cost; }
  };

  vector<ll> cost1(N, INT_MAX);

  {
    priority_queue<Element> pq;

    cost1[0] = 0;
    pq.push({0, 0});
    while (!pq.empty()) {
      auto t = pq.top();
      pq.pop();

      for (auto next : edges[t.node]) {
        if (t.cost + 1 < cost1[next]) {
          cost1[next] = t.cost + 1;
          pq.push({t.cost + 1, next});
        }
      }
    }
  }

  ll max_cost = 0, max_index = -1;
  for (ll i = 0; i < N; i++) {
    if (cost1[i] > max_cost) {
      max_cost = cost1[i];
      max_index = i;
    }
  }

  vector<ll> cost2(N, INT_MAX);

  {
    priority_queue<Element> pq;

    cost2[max_index] = 0;
    pq.push({0, max_index});
    while (!pq.empty()) {
      auto t = pq.top();
      pq.pop();

      for (auto next : edges[t.node]) {
        if (t.cost + 1 < cost2[next]) {
          cost2[next] = t.cost + 1;
          pq.push({t.cost + 1, next});
        }
      }
    }
  }

  ll diameter = *max_element(cost2.begin(), cost2.end());
  cout << (diameter % 3 == 1 ? "Second" : "First") << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

int main() {
  ll N;
  cin >> N;
  if (N == 1) {
    cout << "First" << endl;
    return 0;
  }
  vector<vector<ll>> edges(N);
  for (ll i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  struct Element {
    ll cost, node;
    bool operator<(const Element &rhs) const { return cost > rhs.cost; }
  };

  vector<ll> cost1(N, INT_MAX);

  {
    priority_queue<Element> pq;

    cost1[0] = 0;
    pq.push({0, 0});
    while (!pq.empty()) {
      auto t = pq.top();
      pq.pop();

      for (auto next : edges[t.node]) {
        if (t.cost + 1 < cost1[next]) {
          cost1[next] = t.cost + 1;
          pq.push({t.cost + 1, next});
        }
      }
    }
  }

  ll max_cost = 0, max_index = -1;
  for (ll i = 0; i < N; i++) {
    if (cost1[i] > max_cost) {
      max_cost = cost1[i];
      max_index = i;
    }
  }

  vector<ll> cost2(N, INT_MAX);

  {
    priority_queue<Element> pq;

    cost2[max_index] = 0;
    pq.push({0, max_index});
    while (!pq.empty()) {
      auto t = pq.top();
      pq.pop();

      for (auto next : edges[t.node]) {
        if (t.cost + 1 < cost2[next]) {
          cost2[next] = t.cost + 1;
          pq.push({t.cost + 1, next});
        }
      }
    }
  }

  ll diameter = *max_element(cost2.begin(), cost2.end());
  cout << (diameter % 3 == 1 ? "Second" : "First") << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

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

  queue<int> q;
  vector<int> ds(N, inINF);
  ds[0] = 0;
  q.push(0);
  int tmp;
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    for (int v : G[tmp]) {
      if (ds[v] < inINF) {
        continue;
      }
      ds[v] = ds[tmp] + 1;
      q.push(v);
    }
  }

  int m = 0;
  int ind;
  for (int i = 0; i < N; i++) {
    if (ds[i] > m) {
      ind = i;
      m = ds[i];
    }
  }

  fill(ALL(ds), inINF);
  q.push(ind);
  ds[ind] = 0;
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    for (int v : G[tmp]) {
      if (ds[v] < inINF) {
        continue;
      }
      ds[v] = ds[tmp] + 1;
      q.push(v);
    }
  }
  m = 0;
  for (int i = 0; i < N; i++) {
    if (ds[i] > m) {
      ind = i;
      m = ds[i];
    }
  }

  if (m % 3 == 1) {
    printf("Second\n");
  } else {
    printf("First\n");
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

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

  queue<int> q;
  vector<int> ds(N, inINF);
  ds[0] = 0;
  q.push(0);
  int tmp;
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    for (int v : G[tmp]) {
      if (ds[v] < inINF) {
        continue;
      }
      ds[v] = ds[tmp] + 1;
      q.push(v);
    }
  }

  int m = 0;
  int ind;
  for (int i = 0; i < N; i++) {
    if (ds[i] > m) {
      ind = i;
      m = ds[i];
    }
  }

  fill(ALL(ds), inINF);
  q.push(ind);
  ds[ind] = 0;
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    for (int v : G[tmp]) {
      if (ds[v] < inINF) {
        continue;
      }
      ds[v] = ds[tmp] + 1;
      q.push(v);
    }
  }
  m = 0;
  for (int i = 0; i < N; i++) {
    if (ds[i] > m) {
      ind = i;
      m = ds[i];
    }
  }

  if (m % 3 == 1) {
    printf("Second\n");
  } else {
    printf("First\n");
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> E(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].insert(b);
    E[b].insert(a);
  }
  vector<ll> d1(n, -1);
  vector<ll> d2(n, -1);
  queue<ll> q;
  q.push(0);
  d1[0] = 0;
  while (q.size()) {
    ll f = q.front();
    q.pop();
    for (ll t : E[f]) {
      if (d1[t] < 0) {
        d1[t] = d1[f] + 1;
        q.push(t);
      }
    }
  }
  ll mx = 0, mi;
  for (ll i = 0; i < n; i++) {
    if (d1[i] > mx) {
      mx = d1[i];
      mi = i;
    }
  }
  d2[mi] = 0;
  q.push(mi);
  while (q.size()) {
    ll f = q.front();
    q.pop();
    for (ll t : E[f]) {
      if (d2[t] < 0) {
        d2[t] = d2[f] + 1;
        q.push(t);
      }
    }
  }
  mx = 0;
  for (ll i = 0; i < n; i++) {
    mx = max(mx, d2[i]);
  }
  if (mx % 3 == 1) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> E(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].insert(b);
    E[b].insert(a);
  }
  vector<ll> d1(n, -1);
  vector<ll> d2(n, -1);
  queue<ll> q;
  q.push(0);
  d1[0] = 0;
  while (q.size()) {
    ll f = q.front();
    q.pop();
    for (ll t : E[f]) {
      if (d1[t] < 0) {
        d1[t] = d1[f] + 1;
        q.push(t);
      }
    }
  }
  ll mx = 0, mi = 0;
  for (ll i = 0; i < n; i++) {
    if (d1[i] > mx) {
      mx = d1[i];
      mi = i;
    }
  }
  d2[mi] = 0;
  q.push(mi);
  while (q.size()) {
    ll f = q.front();
    q.pop();
    for (ll t : E[f]) {
      if (d2[t] < 0) {
        d2[t] = d2[f] + 1;
        q.push(t);
      }
    }
  }
  mx = 0;
  for (ll i = 0; i < n; i++) {
    mx = max(mx, d2[i]);
  }
  if (mx % 3 == 1) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}
Problem p03055 (C++)

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

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

  int dist = 0;
  vector<int> v(n);
  function<int(int, int)> dfs = [&](int v, int p) {
    int res = 0;
    vector<int> ds;
    for (auto to : g[v]) {
      if (to == p)
        continue;
      ds.push_back(dfs(to, v) + 1);
    }
    sort(rbegin(ds), rend(ds));
    if (!ds.empty()) {
      res = ds[0];
      dist = max(dist, ds[0]);
    }
    if (ds.size() >= 2u) {
      dist = max(dist, ds[0] + ds[1]);
    }
    if (ds.size())
      dist = max(dist, res);
    return res;
  };
  dfs(0, -1);

  vector<int> dp(n + 1, -1);
  function<int(int)> solve = [&](int x) {
    int &res = dp[x];
    if (x == 0)
      return res = 1;
    if (x == 1)
      return res = 0;
    if (res != -1)
      return res;
    return res = !solve(x - 1) || !solve(x - 2);
  };
  if (solve(dist) == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}

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

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

  int dist = 0;
  vector<int> v(n);
  function<int(int, int)> dfs = [&](int v, int p) {
    int res = 0;
    vector<int> ds;
    for (auto to : g[v]) {
      if (to == p)
        continue;
      ds.push_back(dfs(to, v) + 1);
    }
    sort(rbegin(ds), rend(ds));
    if (!ds.empty()) {
      res = ds[0];
      dist = max(dist, ds[0]);
    }
    if (ds.size() >= 2u) {
      dist = max(dist, ds[0] + ds[1]);
    }
    if (ds.size())
      dist = max(dist, res);
    return res;
  };
  dfs(0, -1);

  vector<int> dp(n + 1, -1);
  function<int(int)> solve = [&](int x) {
    int &res = dp[x];
    if (x == 0)
      return res = 1;
    if (x == 1)
      return res = 0;
    if (res != -1)
      return res;
    return res = !solve(x - 1) || !solve(x - 2);
  };
  if (solve(dist) == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N = 1e5 + 5;
vector<ll> adj[N];
ll node, dist;
void dfs(ll src, ll pr, ll d) {
  if (d >= dist) {
    node = src;
    dist = d;
  }
  for (auto &v : adj[src])
    if (v != pr)
      dfs(v, src, d + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 0, u, v; i < n - 1; i++)
    cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u);
  dfs(1, 1, 0);
  dist = 0;
  dfs(node, node, 0);

  if (dist % 3 == 1)
    cout << "Second";
  else
    cout << "First";
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N = 2e5 + 5;
vector<ll> adj[N];
ll node, dist;
void dfs(ll src, ll pr, ll d) {
  if (d >= dist) {
    node = src;
    dist = d;
  }
  for (auto &v : adj[src])
    if (v != pr)
      dfs(v, src, d + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 0, u, v; i < n - 1; i++)
    cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u);
  dfs(1, 1, 0);
  dist = 0;
  dfs(node, node, 0);

  if (dist % 3 == 1)
    cout << "Second";
  else
    cout << "First";
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, x, y, ne[N], fi[N], zz[N], ans, tot, Maxsd[N], sd[N];
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x, int y, int z) {
  Maxsd[x] = sd[x] = z;
  for (int i = fi[x]; i; i = ne[i])
    if (zz[i] != y) {
      dfs(zz[i], x, z + 1);
      ans = max(ans, Maxsd[x] + Maxsd[zz[i]] - 2 * sd[x]);
      Maxsd[x] = max(Maxsd[x], Maxsd[zz[i]]);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    jb(x, y);
    jb(y, x);
  }
  dfs(1, 0, 0);
  if (ans % 3 == 1)
    puts("Second");
  else
    puts("First");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int n, x, y, ne[N], fi[N], zz[N], ans, tot, Maxsd[N], sd[N];
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x, int y, int z) {
  Maxsd[x] = sd[x] = z;
  for (int i = fi[x]; i; i = ne[i])
    if (zz[i] != y) {
      dfs(zz[i], x, z + 1);
      ans = max(ans, Maxsd[x] + Maxsd[zz[i]] - 2 * sd[x]);
      Maxsd[x] = max(Maxsd[x], Maxsd[zz[i]]);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    jb(x, y);
    jb(y, x);
  }
  dfs(1, 0, 0);
  if (ans % 3 == 1)
    puts("Second");
  else
    puts("First");
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int n;
vector<int> g[100100];
int dist[100100];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, n) dist[i] = INF;
  dist[0] = 0;
  queue<int> que;
  que.push(0);
  int vfar = 0, dfar = 0;
  while (que.size()) {
    int v = que.front();
    que.pop();
    if (chmax(dfar, dist[v]))
      vfar = v;
    rep(i, g[v].size()) {
      if (dist[g[v].at(i)] > dist[v] + 1) {
        dist[g[v].at(i)] = dist[v] + 1;
        que.push(g[v].at(i));
      }
    }
  }
  rep(i, n) dist[i] = INF;
  dist[vfar] = 0;
  queue<int> que2;
  que2.push(vfar);
  dfar = 0;
  while (que2.size()) {
    int v = que2.front();
    que2.pop();
    chmax(dfar, dist[v]);
    rep(i, g[v].size()) {
      if (dist[g[v].at(i)] > dist[v] + 1) {
        dist[g[v].at(i)] = dist[v] + 1;
        que2.push(g[v].at(i));
      }
    }
  }
  cout << (dfar % 3 != 1 ? "First" : "Second") << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
#define endl '\n'
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int n;
vector<int> g[200100];
int dist[200100];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, n) dist[i] = INF;
  dist[0] = 0;
  queue<int> que;
  que.push(0);
  int vfar = 0, dfar = 0;
  while (que.size()) {
    int v = que.front();
    que.pop();
    if (chmax(dfar, dist[v]))
      vfar = v;
    rep(i, g[v].size()) {
      if (dist[g[v].at(i)] > dist[v] + 1) {
        dist[g[v].at(i)] = dist[v] + 1;
        que.push(g[v].at(i));
      }
    }
  }
  rep(i, n) dist[i] = INF;
  dist[vfar] = 0;
  queue<int> que2;
  que2.push(vfar);
  dfar = 0;
  while (que2.size()) {
    int v = que2.front();
    que2.pop();
    chmax(dfar, dist[v]);
    rep(i, g[v].size()) {
      if (dist[g[v].at(i)] > dist[v] + 1) {
        dist[g[v].at(i)] = dist[v] + 1;
        que2.push(g[v].at(i));
      }
    }
  }
  cout << (dfar % 3 != 1 ? "First" : "Second") << endl;
}
Problem p03056 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define N 200

int n, m, ans, f[N][N][N], g[N][N][N];
char s[N][N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
  }
  for (int l = n; l; --l) {
    for (int r = l; r <= n; ++r) {
      for (int h = m; h; --h) {
        if (l == r || (s[l][h] == s[l + 1][h] && f[l + 1][r][h] > h)) {
          if (h + 1 <= m && s[l][h] == s[l][h + 1]) {
            f[l][r][h] = f[l][r][h + 1];
          } else {
            f[l][r][h] = h + 1;
          }
        } else {
          f[l][r][h] = h;
        }
      }
    }
  }
  while (f[1][n][1] <= m) {
    for (int l = n; l; --l) {
      for (int r = l; r <= n; ++r) {
        for (int h = m; h; --h) {
          g[l][r][h] = f[l][r][h] <= m ? f[l][r][f[l][r][h]] : m;
          if (l < r) {
            int mn = l, mx = r - 1;
            while (mn <= mx) {
              int mid = (mn + mx) >> 1;
              g[l][r][h] = max(g[l][r][h], min(f[l][mid][h], f[mid + 1][r][h]));
              if (f[l][mid][h] > f[mid + 1][r][h])
                mn = mid + 1;
              else
                mx = mid - 1;
            }
          }
        }
      }
    }
    memcpy(f, g, sizeof f);
    ++ans;
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define N 200

int n, m, ans, f[N][N][N], g[N][N][N];
char s[N][N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
  }
  for (int l = n; l; --l) {
    for (int r = l; r <= n; ++r) {
      for (int h = m; h; --h) {
        if (l == r || (s[l][h] == s[l + 1][h] && f[l + 1][r][h] > h)) {
          if (h + 1 <= m && s[l][h] == s[l][h + 1]) {
            f[l][r][h] = f[l][r][h + 1];
          } else {
            f[l][r][h] = h + 1;
          }
        } else {
          f[l][r][h] = h;
        }
      }
    }
  }
  while (f[1][n][1] < m) {
    for (int l = n; l; --l) {
      for (int r = l; r <= n; ++r) {
        for (int h = m; h; --h) {
          g[l][r][h] = f[l][r][h] <= m ? f[l][r][f[l][r][h]] : m;
          if (l < r) {
            int mn = l, mx = r - 1;
            while (mn <= mx) {
              int mid = (mn + mx) >> 1;
              g[l][r][h] = max(g[l][r][h], min(f[l][mid][h], f[mid + 1][r][h]));
              if (f[l][mid][h] > f[mid + 1][r][h])
                mn = mid + 1;
              else
                mx = mid - 1;
            }
          }
        }
      }
    }
    memcpy(f, g, sizeof f);
    ++ans;
  }
  printf("%d\n", ans);

  return 0;
}