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

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

vector<vector<int>> edge;

void dfs(int idx, int y, int d, vector<int> &vv) {
  if (vv[idx] >= 0)
    return;
  vv[idx] = d;
  for (int x : edge[idx]) {
    if (x == y)
      continue;
    dfs(x, idx, d + 1, vv);
  }
}

int main() {
  int N;
  cin >> N;
  edge = vector<vector<int>>(N);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  vector<int> vv(N, -1);
  dfs(0, -1, 0, vv);
  int idx = 0;
  for (int i = 0; i < N; i++) {
    if (vv[idx] < vv[i]) {
      idx = i;
    }
  }
  vv = vector<int>(N, -1);
  dfs(idx, -1, 0, vv);
  int mx = 0;
  for (int i = 0; i < N; i++) {
    mx = max(mx, vv[i]);
  }
  cout << (mx % 3 == 1 ? "Second" : "First") << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> edge;

void dfs(int idx, int y, int d, vector<int> &vv) {
  if (vv[idx] >= 0)
    return;
  vv[idx] = d;
  for (int x : edge[idx]) {
    if (x == y)
      continue;
    dfs(x, idx, d + 1, vv);
  }
}

int main() {
  int N;
  cin >> N;
  edge = vector<vector<int>>(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  vector<int> vv(N, -1);
  dfs(0, -1, 0, vv);
  int idx = 0;
  for (int i = 0; i < N; i++) {
    if (vv[idx] < vv[i]) {
      idx = i;
    }
  }
  vv = vector<int>(N, -1);
  dfs(idx, -1, 0, vv);
  int mx = 0;
  for (int i = 0; i < N; i++) {
    mx = max(mx, vv[i]);
  }
  cout << (mx % 3 == 1 ? "Second" : "First") << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dist[200010] = {};
vector<ll> tree[200010];
void dfs(ll cur, ll par, ll d) {
  dist[cur] = d;
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    dfs(e, cur, d + 1);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll a[200010], b[200010];
  rep(i, 0, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    tree[a[i]].push_back(b[i]);
    tree[b[i]].push_back(a[i]);
  }
  dfs(0, -1, 0);
  ll max_val = 0, max_pos = -1;
  rep(i, 0, N) {
    if (max_val < dist[i]) {
      max_val = dist[i];
      max_pos = i;
    }
  }
  rep(i, 0, N) dist[i] = 0;
  dfs(max_pos, -1, 0);
  max_val = 0;
  rep(i, 0, N) max_val = max(max_val, dist[i]);
  if (max_val % 3 == 1) {
    print("Second");
  } else {
    print("First");
  }
}

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dist[200010] = {};
vector<ll> tree[200010];
void dfs(ll cur, ll par, ll d) {
  dist[cur] = d;
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    dfs(e, cur, d + 1);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  if (N == 1) {
    print("First");
    return 0;
  }

  ll a[200010], b[200010];
  rep(i, 0, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    tree[a[i]].push_back(b[i]);
    tree[b[i]].push_back(a[i]);
  }
  dfs(0, -1, 0);
  ll max_val = 0, max_pos = -1;
  rep(i, 0, N) {
    if (max_val < dist[i]) {
      max_val = dist[i];
      max_pos = i;
    }
  }
  rep(i, 0, N) dist[i] = 0;
  dfs(max_pos, -1, 0);
  max_val = 0;
  rep(i, 0, N) max_val = max(max_val, dist[i]);
  if (max_val % 3 == 1) {
    print("Second");
  } else {
    print("First");
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 1e5 + 10;
const int MAXM = 3e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;

int A[MAXN];
char S[MAXN];
int depth[MAXN][3];

void dfs(int here, int num) {
  int there;

  for (int i = 0; i < graph[here].size(); i++) {
    there = graph[here][i];
    if (depth[there][num] != -1)
      continue;
    depth[there][num] = depth[here][num] + 1;
    dfs(there, num);
  }
  return;
}

int getlength(int n) {
  for (int i = 1; i <= n; i++)
    depth[i][0] = depth[i][1] = -1;

  depth[1][0] = 0;

  dfs(1, 0);

  int idx = 1;
  int mx = 0;

  for (int i = 1; i <= n; i++) {
    if (mx < depth[i][0]) {
      mx = depth[i][0];
      idx = i;
    }
  }
  depth[idx][1] = 0;
  dfs(idx, 1);

  for (int i = 1; i <= n; i++)
    mx = max(mx, depth[i][1]);
  return mx;
}

int main() {
  int n, m, k, a, b, x, y;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;

  scanf("%d", &n);

  graph = vector<vector<int>>(n + 1);

  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

  int len = getlength(n);

  if (len % 3 == 1)
    printf("Second");
  else
    printf("First");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 3e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;

int A[MAXN];
char S[MAXN];
int depth[MAXN][3];

void dfs(int here, int num) {
  int there;

  for (int i = 0; i < graph[here].size(); i++) {
    there = graph[here][i];
    if (depth[there][num] != -1)
      continue;
    depth[there][num] = depth[here][num] + 1;
    dfs(there, num);
  }
  return;
}

int getlength(int n) {
  for (int i = 1; i <= n; i++)
    depth[i][0] = depth[i][1] = -1;

  depth[1][0] = 0;

  dfs(1, 0);

  int idx = 1;
  int mx = 0;

  for (int i = 1; i <= n; i++) {
    if (mx < depth[i][0]) {
      mx = depth[i][0];
      idx = i;
    }
  }
  depth[idx][1] = 0;
  dfs(idx, 1);

  for (int i = 1; i <= n; i++)
    mx = max(mx, depth[i][1]);
  return mx;
}

int main() {
  int n, m, k, a, b, x, y;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;

  scanf("%d", &n);

  graph = vector<vector<int>>(n + 1);

  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

  int len = getlength(n);

  if (len % 3 == 1)
    printf("Second");
  else
    printf("First");

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e5 + 5;
struct edge {
  int u, v, next;
} e[N * 2];
int head[N], cnt;
int dep[N], x, y;
int n, m;
void add_edge(int u, int v) {
  e[++cnt] = (edge){u, v, head[u]};
  head[u] = cnt;
}
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  if (dep[u] > dep[x])
    x = u;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].v != fa)
      dfs(e[i].v, u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  dfs(1, 0);
  memset(dep, 0, sizeof dep);
  dfs(x, 0);
  if (dep[x] < 3)
    puts(dep[x] == 1 ? "First" : "Second");
  else
    puts(dep[x] % 3 == 2 ? "Second" : "First");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e5 + 5;
struct edge {
  int u, v, next;
} e[N * 2];
int head[N], cnt;
int dep[N], x, y;
int n, m;
void add_edge(int u, int v) {
  e[++cnt] = (edge){u, v, head[u]};
  head[u] = cnt;
}
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  if (dep[u] > dep[x])
    x = u;
  for (int i = head[u]; i; i = e[i].next)
    if (e[i].v != fa)
      dfs(e[i].v, u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  dfs(1, 0);
  memset(dep, 0, sizeof dep);
  dfs(x, 0);
  if (dep[x] < 3)
    puts(dep[x] == 1 ? "First" : "Second");
  else
    puts(dep[x] % 3 == 2 ? "Second" : "First");
}
Problem p03055 (C++)

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

using namespace std;

const int MOD = 1e9 + 7;

// const int MOD=998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<vector<int>> tree(n + 1);

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }

  vector<int> d1(n + 1, -1), d2(n + 1, -1);
  int r = 1, dr = 0;

  queue<int> q;
  q.push(1);

  d1[1] = 0;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i : tree[cur]) {
      if (d1[i] == -1) {
        q.push(i);
        d1[i] = d1[cur] + 1;
        if (d1[i] > dr) {
          dr = d1[i];
          r = i;
        }
      }
    }
  }

  int diam = 0;
  q.push(r);
  d2[r] = 0;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i : tree[cur]) {
      if (d2[i] == -1) {
        q.push(i);
        d2[i] = d2[cur] + 1;
        diam = max(diam, d2[i]);
      }
    }
  }

  diam++;

  cout << (diam % 3 == 2 ? "Second" : "First");

  return 0;
}

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

using namespace std;

const int MOD = 1e9 + 7;

// const int MOD=998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<vector<int>> tree(n + 1);

  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }

  vector<int> d1(n + 1, -1), d2(n + 1, -1);
  int r = 1, dr = 0;

  queue<int> q;
  q.push(1);

  d1[1] = 0;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i : tree[cur]) {
      if (d1[i] == -1) {
        q.push(i);
        d1[i] = d1[cur] + 1;
        if (d1[i] > dr) {
          dr = d1[i];
          r = i;
        }
      }
    }
  }

  int diam = 0;
  q.push(r);
  d2[r] = 0;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i : tree[cur]) {
      if (d2[i] == -1) {
        q.push(i);
        d2[i] = d2[cur] + 1;
        diam = max(diam, d2[i]);
      }
    }
  }

  diam++;

  cout << (diam % 3 == 2 ? "Second" : "First");

  return 0;
}
Problem p03055 (C++)

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

using namespace std;

pair<uint64_t, uint64_t>
farthest(uint64_t N, map<uint64_t, set<uint64_t>> &edges, uint64_t src) {
  vector<bool> visited(N, false);

  stack<pair<uint64_t, uint64_t>> s;
  s.emplace(src, 0);

  uint64_t max_len = 0, max_len_node = -1;
  while (!s.empty()) {
    pair<uint64_t, uint64_t> p = s.top();
    s.pop();

    if (visited[p.first]) {
      continue;
    }

    visited[p.first] = true;

    if (max_len < p.second) {
      max_len_node = p.first;
      max_len = p.second;
    }

    for (uint64_t next : edges[p.first]) {
      if (!visited[next]) {
        s.emplace(next, p.second + 1);
      }
    }
  }
  return make_pair(max_len_node, max_len);
}

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

  map<uint64_t, set<uint64_t>> edges;
  for (uint64_t n = 0; n < N; ++n) {
    edges.emplace(n, set<uint64_t>());
  }

  for (uint64_t n = 0; n < N - 1; ++n) {
    uint64_t a, b;
    cin >> a >> b;

    edges[a - 1].insert(b - 1);
    edges[b - 1].insert(a - 1);
  }

  pair<uint64_t, uint64_t> pu = farthest(N, edges, 0);
  pair<uint64_t, uint64_t> pv = farthest(N, edges, pu.first);

  uint64_t diameter = pv.second;

  cout << ((diameter % 3) != 1 ? "First" : "Second") << endl;

  return 0;
}

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

using namespace std;

pair<uint64_t, uint64_t>
farthest(uint64_t N, map<uint64_t, set<uint64_t>> &edges, uint64_t src) {
  vector<bool> visited(N, false);

  stack<pair<uint64_t, uint64_t>> s;
  s.emplace(src, 0);

  uint64_t max_len = 0, max_len_node = 0;
  while (!s.empty()) {
    pair<uint64_t, uint64_t> p = s.top();
    s.pop();

    if (visited[p.first]) {
      continue;
    }

    visited[p.first] = true;

    if (max_len < p.second) {
      max_len_node = p.first;
      max_len = p.second;
    }

    for (uint64_t next : edges[p.first]) {
      if (!visited[next]) {
        s.emplace(next, p.second + 1);
      }
    }
  }
  return make_pair(max_len_node, max_len);
}

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

  map<uint64_t, set<uint64_t>> edges;
  for (uint64_t n = 0; n < N; ++n) {
    edges.emplace(n, set<uint64_t>());
  }

  for (uint64_t n = 0; n < N - 1; ++n) {
    uint64_t a, b;
    cin >> a >> b;

    edges[a - 1].insert(b - 1);
    edges[b - 1].insert(a - 1);
  }

  pair<uint64_t, uint64_t> pu = farthest(N, edges, 0);
  pair<uint64_t, uint64_t> pv = farthest(N, edges, pu.first);

  uint64_t diameter = pv.second;

  cout << ((diameter % 3) != 1 ? "First" : "Second") << endl;

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <thread>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e5 + 10;
vector<int> G[N];

pair<int, int> dfs(int v, int p = -1) {
  pair<int, int> lr = {0, v};
  for (auto x : G[v]) {
    if (x == p)
      continue;
    auto res = dfs(x, v);
    res.first++;
    lr = max(lr, res);
  }
  return lr;
}

bool res(int x) {
  if (x % 3 == 1) {
    return false;
  }
  return true;
}

int main() {
  srand(time(NULL));
  ::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> 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);
  }
  pair<int, int> lr = dfs(0);
  lr = dfs(lr.second);
  if (res(lr.first)) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <thread>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 3e5 + 10;
vector<int> G[N];

pair<int, int> dfs(int v, int p = -1) {
  pair<int, int> lr = {0, v};
  for (auto x : G[v]) {
    if (x == p)
      continue;
    auto res = dfs(x, v);
    res.first++;
    lr = max(lr, res);
  }
  return lr;
}

bool res(int x) {
  if (x % 3 == 1) {
    return false;
  }
  return true;
}

int main() {
  srand(time(NULL));
  ::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> 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);
  }
  pair<int, int> lr = dfs(0);
  lr = dfs(lr.second);
  if (res(lr.first)) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

template <typename T>
vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> &G,
                   /*vector<int> & prev,*/ const T INF = 1e9) {
  using P = pair<T, int>;
  int V = G.size();
  vector<T> dist(V, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = 0;
  que.emplace(0, s);
  /*prev.assign(V, -1);*/
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i].first;
      T cost = G[v][i].second;
      if (dist[to] > dist[v] + cost) {
        dist[to] = dist[v] + cost;
        /*prev[to] = v;*/
        que.emplace(dist[to], to);
      }
    }
  }
  return dist;
}

signed main() {
  int n;
  cin >> n;
  vector<vector<pair<int, int>>> G(n);
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].emplace_back(b, 1);
    G[b].emplace_back(a, 1);
  }
  vector<int> dist = Dijkstra(0, G);
  int v;
  int mx = 0;
  rep(i, n) {
    if (dist[i] > mx) {
      mx = dist[i];
      v = i;
    }
  }
  vector<int> distv = Dijkstra(v, G);
  int d = 0;
  rep(i, n) { d = max(d, distv[i]); }
  cout << (d % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

template <typename T>
vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> &G,
                   /*vector<int> & prev,*/ const T INF = 1e9) {
  using P = pair<T, int>;
  int V = G.size();
  vector<T> dist(V, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = 0;
  que.emplace(0, s);
  /*prev.assign(V, -1);*/
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i].first;
      T cost = G[v][i].second;
      if (dist[to] > dist[v] + cost) {
        dist[to] = dist[v] + cost;
        /*prev[to] = v;*/
        que.emplace(dist[to], to);
      }
    }
  }
  return dist;
}

signed main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << "First" << endl;
    return 0;
  }
  vector<vector<pair<int, int>>> G(n);
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].emplace_back(b, 1);
    G[b].emplace_back(a, 1);
  }
  vector<int> dist = Dijkstra(0, G);
  int v;
  int mx = 0;
  rep(i, n) {
    if (dist[i] > mx) {
      mx = dist[i];
      v = i;
    }
  }
  vector<int> distv = Dijkstra(v, G);
  int d = 0;
  rep(i, n) { d = max(d, distv[i]); }
  cout << (d % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}
Problem p03055 (C++)

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

using namespace std;
vector<int> edge[200000];
int d[2][200000];
int INF = 1e9;

int opposite = 0;
int max_dist = 0;

void f(int index, int x) {
  int dx = d[index][x];
  if (dx > max_dist) {
    opposite = x;
    max_dist = dx;
  }
  int s = edge[x].size();
  for (int i = 0; i < s; i++) {
    int y = edge[x][i];
    if (d[index][y] == INF) {
      d[index][y] = dx + 1;
      f(index, y);
    }
  }
  return;
}

int main() {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2e5; j++) {
      d[i][j] = INF;
    }
  }
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    edge[a - 1].push_back(b - 1);
    edge[b - 1].push_back(a - 1);
  }
  d[0][0] = 0;
  f(0, 0);
  d[1][opposite] = 0;
  f(1, opposite);
  string answer = (max_dist % 3 != 1 ? "First" : "Second");
  cout << answer << endl;
}

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

using namespace std;
vector<int> edge[200000];
int d[2][200000];
int INF = 1e9;

int opposite = 0;
int max_dist = 0;

void f(int index, int x) {
  int dx = d[index][x];
  if (dx > max_dist) {
    opposite = x;
    max_dist = dx;
  }
  int s = edge[x].size();
  for (int i = 0; i < s; i++) {
    int y = edge[x][i];
    if (d[index][y] == INF) {
      d[index][y] = dx + 1;
      f(index, y);
    }
  }
  return;
}

int main() {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2e5; j++) {
      d[i][j] = INF;
    }
  }
  int N;
  cin >> N;
  if (N == 1) {
    cout << "First" << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    edge[a - 1].push_back(b - 1);
    edge[b - 1].push_back(a - 1);
  }
  d[0][0] = 0;
  f(0, 0);
  d[1][opposite] = 0;
  f(1, opposite);
  string answer = (max_dist % 3 != 1 ? "First" : "Second");
  cout << answer << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const int inf = 1 << 30;

int n, a, b, x, d[100009];
vector<int> e[100009];

int bfs(int s) {
  rep(i, n + 1) d[i] = inf;
  d[s] = 0;
  queue<int> que;
  que.push(s);
  int now;
  while (!que.empty()) {
    now = que.front();
    que.pop();
    rep(i, e[now].size()) {
      if (d[e[now][i]] != inf)
        continue;
      d[e[now][i]] = d[now] + 1;
      que.push(e[now][i]);
    }
  }
  x = now;
  return d[now];
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  bfs(1);
  cout << (bfs(x) % 3 != 1 ? "First\n" : "Second\n");
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const int inf = 1 << 30;

int n, a, b, x, d[200009];
vector<int> e[200009];

int bfs(int s) {
  rep(i, n + 1) d[i] = inf;
  d[s] = 0;
  queue<int> que;
  que.push(s);
  int now;
  while (!que.empty()) {
    now = que.front();
    que.pop();
    rep(i, e[now].size()) {
      if (d[e[now][i]] != inf)
        continue;
      d[e[now][i]] = d[now] + 1;
      que.push(e[now][i]);
    }
  }
  x = now;
  return d[now];
}

int main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  bfs(1);
  cout << (bfs(x) % 3 != 1 ? "First\n" : "Second\n");
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int FindFurthest(vector<vector<int>> &adj_nodes, int start, int *p_furthest) {
  int N = adj_nodes.size();
  vector<bool> visited(N, false);
  int remaining = N;

  queue<int> q;
  q.push(start);
  visited[start] = true;
  --remaining;
  queue<int> nq;

  int dist = 1;
  bool done = false;
  while (remaining > 0) {
    while (not q.empty()) {
      int x = q.front();
      q.pop();
      for (auto nx : adj_nodes[x]) {
        if (not visited[nx]) {
          nq.push(nx);
          visited[nx] = true;
          --remaining;
          if (remaining == 0) {
            *p_furthest = nx;
            done = true;
            break;
          }
        }
      }
      if (done)
        break;
    }
    if (done)
      break;
    swap(q, nq);
    ++dist;
  }

  return dist;
}

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

  vector<vector<int>> adj_nodes(N);

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

  int dist;
  int furthest;
  dist = FindFurthest(adj_nodes, 0, &furthest);
  int furthest2;
  dist = FindFurthest(adj_nodes, furthest, &furthest2);

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int FindFurthest(vector<vector<int>> &adj_nodes, int start, int *p_furthest) {
  int N = adj_nodes.size();
  vector<bool> visited(N, false);
  int remaining = N;

  queue<int> q;
  q.push(start);
  visited[start] = true;
  --remaining;
  queue<int> nq;

  int dist = 1;
  bool done = false;
  while (remaining > 0) {
    while (not q.empty()) {
      int x = q.front();
      q.pop();
      for (auto nx : adj_nodes[x]) {
        if (not visited[nx]) {
          nq.push(nx);
          visited[nx] = true;
          --remaining;
          if (remaining == 0) {
            *p_furthest = nx;
            done = true;
            break;
          }
        }
      }
      if (done)
        break;
    }
    if (done)
      break;
    swap(q, nq);
    ++dist;
  }

  return dist;
}

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

  if (N == 1) {
    cout << "First" << endl;
    return 0;
  }

  vector<vector<int>> adj_nodes(N);

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

  int dist;
  int furthest;
  dist = FindFurthest(adj_nodes, 0, &furthest);
  int furthest2;
  dist = FindFurthest(adj_nodes, furthest, &furthest2);

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

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip> // std::setw(int), std::setfill(char)
#include <ios>     // std::left, std::right
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> G[200000];
int tree[200000] = {};

void dfs1(int x) {
  for (int j = 0; j < G[x].size(); j++) {
    if (tree[G[x][j]] < 0) {
      tree[G[x][j]] = tree[x] + 1;
      dfs1(G[x][j]);
    }
  }
  return;
}

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

  tree[0] = 0;
  dfs1(0);

  int x, y, xd, yd;
  x = 0;
  xd = -1;
  y = 0;
  yd = -1;
  for (int i = 0; i < N; ++i) {
    if (xd < tree[i]) {
      xd = tree[i];
      x = i;
    }
    tree[i] = (-1);
  }

  tree[x] = 0;
  dfs1(x);

  for (int i = 0; i < N; ++i) {
    if (yd < tree[i]) {
      yd = tree[i];
      y = i;
    }
  }
  if (yd % 3 == 1) {
    cout << "Second"
         << "\n";
  } else {
    cout << "First"
         << "\n";
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip> // std::setw(int), std::setfill(char)
#include <ios>     // std::left, std::right
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> G[200000];
int tree[200000] = {};

void dfs1(int x) {
  for (int j = 0; j < G[x].size(); j++) {
    if (tree[G[x][j]] < 0) {
      tree[G[x][j]] = tree[x] + 1;
      dfs1(G[x][j]);
    }
  }
  return;
}

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

  tree[0] = 0;
  dfs1(0);

  int x, y, xd, yd;
  x = 0;
  xd = -1;
  y = 0;
  yd = -1;
  for (int i = 0; i < N; ++i) {
    if (xd < tree[i]) {
      xd = tree[i];
      x = i;
    }
    tree[i] = (-1);
  }

  tree[x] = 0;
  dfs1(x);

  for (int i = 0; i < N; ++i) {
    if (yd < tree[i]) {
      yd = tree[i];
      y = i;
    }
  }
  if (yd % 3 == 1) {
    cout << "Second"
         << "\n";
  } else {
    cout << "First"
         << "\n";
  }
  return 0;
}
Problem p03055 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

vector<pair<int, int>> g[100000];

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

int treeDiameter() {
  auto v = tddfs(0);
  auto w = tddfs(v.second);
  return w.first;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // 1 -> o, 2 -> x, 3 -> o, 4 -> o, 5 -> x, 6 -> o, 7 -> o,
  int N;
  cin >> N;
  vector<bool> win(N + 1);
  win[1] = true;
  win[2] = false;
  for (int i = 3; i <= N; i++) {
    if (i % 3 == 2)
      win[i] = false;
    else
      win[i] = true;
  }

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back({b - 1, 1});
    g[b - 1].push_back({a - 1, 1});
  }
  int ret = treeDiameter();
  // cout << ret << endl;
  if (win[ret + 1])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

vector<pair<int, int>> g[200000];

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

int treeDiameter() {
  auto v = tddfs(0);
  auto w = tddfs(v.second);
  return w.first;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // 1 -> o, 2 -> x, 3 -> o, 4 -> o, 5 -> x, 6 -> o, 7 -> o,
  int N;
  cin >> N;
  vector<bool> win(N + 1);
  win[1] = true;
  win[2] = false;
  for (int i = 3; i <= N; i++) {
    if (i % 3 == 2)
      win[i] = false;
    else
      win[i] = true;
  }

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back({b - 1, 1});
    g[b - 1].push_back({a - 1, 1});
  }
  int ret = treeDiameter();
  // cout << ret << endl;
  if (win[ret + 1])
    cout << "First" << endl;
  else
    cout << "Second" << endl;
  return 0;
}
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 long double ld;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 100005;
int ss, s;
vector<int> v[N];
void dfs(int p, int fa, int dep) {
  if (dep > ss) {
    ss = dep;
    s = p;
  }
  for (auto i : v[p])
    if (i != fa)
      dfs(i, p, dep + 1);
}
int main() {
  int n = read();
  for (int i = 1; i < n; i++) {
    int s = read(), t = read();
    v[s].push_back(t);
    v[t].push_back(s);
  }
  dfs(1, 0, 1);
  ss = 0;
  dfs(s, 0, 1);
  if (ss % 3 == 2)
    puts("Second");
  else
    puts("First");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 200005;
int ss, s;
vector<int> v[N];
void dfs(int p, int fa, int dep) {
  if (dep > ss) {
    ss = dep;
    s = p;
  }
  for (auto i : v[p])
    if (i != fa)
      dfs(i, p, dep + 1);
}
int main() {
  int n = read();
  for (int i = 1; i < n; i++) {
    int s = read(), t = read();
    v[s].push_back(t);
    v[t].push_back(s);
  }
  dfs(1, 0, 1);
  ss = 0;
  dfs(s, 0, 1);
  if (ss % 3 == 2)
    puts("Second");
  else
    puts("First");
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
bool chmax(ll &a, ll b) {
  if (a > b)
    return 0;
  a = b;
  return 1;
}
bool chmin(ll &a, ll b) {
  if (a < b)
    return 0;
  a = b;
  return 1;
}
#define mad(a, b) a = (a + b) % mod
#define mul(a, b) a = a * b % mod

ll n;
vector<ll> g[200010];
ll ma, pnt;
void dfs(ll x, ll from, ll d) {
  if (d >= ma)
    ma = d, pnt = x;
  for (auto y : g[x])
    if (y != from) {
      dfs(y, x, d + 1);
    }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  ma = -1;
  dfs(1, 0, 0);
  dfs(pnt, 0, 0);
  if (ma % 3 != 1)
    cout << "First" << endl;
  else
    cout << "Second" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
bool chmax(ll &a, ll b) {
  if (a > b)
    return 0;
  a = b;
  return 1;
}
bool chmin(ll &a, ll b) {
  if (a < b)
    return 0;
  a = b;
  return 1;
}
#define mad(a, b) a = (a + b) % mod
#define mul(a, b) a = a * b % mod

ll n;
vector<ll> g[200010];
ll ma, pnt;
void dfs(ll x, ll from, ll d) {
  if (d >= ma)
    ma = d, pnt = x;
  for (auto y : g[x])
    if (y != from) {
      dfs(y, x, d + 1);
    }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  ma = -1;
  dfs(1, 0, 0);
  dfs(pnt, 0, 0);
  if (ma % 3 != 1)
    cout << "First" << endl;
  else
    cout << "Second" << endl;
}
Problem p03055 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef _DEBUG_
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int MAXN = 2e5 + 100;
VI e[MAXN];
int d1[MAXN], d2[MAXN];

void DFS(int x, int par, int dep, int *dis) {
  dis[x] = dep;
  for (int u : e[x]) {
    if (u == par)
      continue;
    DFS(u, x, dep + 1, dis);
  }
}

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i < N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    e[a].PB(b);
    e[b].PB(a);
  }

  DFS(1, 0, 0, d1);
  int p = 1;
  for (int i = 2; i <= N; i++)
    if (d1[i] > d1[p])
      p = i;

  DFS(p, 0, 0, d2);

  int len = 0;
  for (int i = 1; i <= N; i++)
    len = max(len, d2[i]);

  debug("%d\n", len);

  if (len % 3 == 1)
    puts("Second");
  else
    puts("First");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef _DEBUG_
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int MAXN = 2e5 + 100;
VI e[MAXN];
int d1[MAXN], d2[MAXN];

void DFS(int x, int par, int dep, int *dis) {
  dis[x] = dep;
  // for (int u : e[x]) {
  for (size_t i = 0; i < e[x].size(); i++) {
    int u = e[x][i];
    if (u == par)
      continue;
    DFS(u, x, dep + 1, dis);
  }
}

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i < N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    e[a].PB(b);
    e[b].PB(a);
  }

  DFS(1, 0, 0, d1);
  int p = 1;
  for (int i = 2; i <= N; i++)
    if (d1[i] > d1[p])
      p = i;

  DFS(p, 0, 0, d2);

  int len = 0;
  for (int i = 1; i <= N; i++)
    len = max(len, d2[i]);

  debug("%d\n", len);

  if (len % 3 == 1)
    puts("Second");
  else
    puts("First");
  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using P = pair<int, int>;

const int MAX = 1e5;
struct edge {
  int to, cost;
};
struct Diameter {
  int from, to, d;
};
vector<edge> g[MAX];

P farthest(int u, int p) {
  P r(0, u);
  for (auto e : g[u]) {
    if (e.to == p)
      continue;
    P c = farthest(e.to, u);
    c.first += e.cost;
    r = max(r, c);
  }
  return r;
}

Diameter tree_diameter() {
  P p = farthest(0, -1);
  P q = farthest(p.second, -1);
  return Diameter{p.second, q.second, q.first};
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(edge{b, 1});
    g[b].push_back(edge{a, 1});
  }
  Diameter d = tree_diameter();
  cout << (d.d % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using P = pair<int, int>;

const int MAX = 2e5;
struct edge {
  int to, cost;
};
struct Diameter {
  int from, to, d;
};
vector<edge> g[MAX];

P farthest(int u, int p) {
  P r(0, u);
  for (auto e : g[u]) {
    if (e.to == p)
      continue;
    P c = farthest(e.to, u);
    c.first += e.cost;
    r = max(r, c);
  }
  return r;
}

Diameter tree_diameter() {
  P p = farthest(0, -1);
  P q = farthest(p.second, -1);
  return Diameter{p.second, q.second, q.first};
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(edge{b, 1});
    g[b].push_back(edge{a, 1});
  }
  Diameter d = tree_diameter();
  cout << (d.d % 3 == 1 ? "Second" : "First") << endl;
  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--)
#define itn int
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
int MAX_V = 100100;
vector<vector<pair<int, int>>> g(MAX_V, vector<pair<int, int>>());
pair<int, int> treedfs(int now, vector<bool> &v, int cost = 0) {

  if (v[now])
    return {-1, -1};
  else
    v[now] = true;
  pair<int, int> ret = {cost, now};
  for (int i = 0; i < g[now].size(); i++) {
    ret = max(ret, treedfs(g[now][i].F, v, cost + g[now][i].S));
  }
  return ret;
}
int Tree_diameter() {
  vector<bool> v(MAX_V);
  pair<int, int> step1 = treedfs(0, v, 0);
  vector<bool> v2(MAX_V);
  pair<int, int> step2 = treedfs(step1.S, v2, 0);
  return step2.F;
}
signed main(void) {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back({b, 1});
    g[b].push_back({a, 1});
  }
  int x = Tree_diameter();
  if (x % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--)
#define itn int
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
int MAX_V = 200100;
vector<vector<pair<int, int>>> g(MAX_V, vector<pair<int, int>>());
pair<int, int> treedfs(int now, vector<bool> &v, int cost = 0) {

  if (v[now])
    return {-1, -1};
  else
    v[now] = true;
  pair<int, int> ret = {cost, now};
  for (int i = 0; i < g[now].size(); i++) {
    ret = max(ret, treedfs(g[now][i].F, v, cost + g[now][i].S));
  }
  return ret;
}
int Tree_diameter() {
  vector<bool> v(MAX_V);
  pair<int, int> step1 = treedfs(0, v, 0);
  vector<bool> v2(MAX_V);
  pair<int, int> step2 = treedfs(step1.S, v2, 0);
  return step2.F;
}
signed main(void) {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back({b, 1});
    g[b].push_back({a, 1});
  }
  int x = Tree_diameter();
  if (x % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using PII = pair<int, int>;

int main() {
  int n, a, b;
  cin >> n;

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

  vector<bool> used(n + 1, false);
  queue<int> que;
  used[1] = true;
  que.push(1);

  int node1 = -1;
  while (!que.empty()) {
    auto x = que.front();
    // printf("x1 %d\n", x);
    que.pop();

    for (auto y : connect[x]) {
      if (used[y]) {
        continue;
      }
      used[y] = true;
      que.push(y);
      node1 = y;
    }
  }

  // printf("node %d\n", node1);

  queue<PII> que2;
  int dist = 0;
  used.assign(n + 1, false);
  used[node1] = true;
  que2.push(make_pair(node1, 0));
  while (!que2.empty()) {
    auto p = que2.front();
    que2.pop();
    int x = p.first;
    dist = max(dist, p.second);

    // printf("p %d %d\n", p.first, p.second);

    for (auto y : connect[x]) {
      if (used[y]) {
        continue;
      }
      used[y] = true;
      que2.push(make_pair(y, p.second + 1));
    }
  }
  // printf("dist %d\n", dist);

  // v1 = win
  // v2 = lose
  // vi2 = (vi1 == lose || vi0 == lose) ? win : lose
  // win lose win が繰り返す

  string ans = (dist + 1) % 3 == 2 ? "Second" : "First";
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using PII = pair<int, int>;

int main() {
  int n, a, b;
  cin >> n;

  if (n == 1) {
    cout << "First" << endl;
    return 0;
  }

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

  vector<bool> used(n + 1, false);
  queue<int> que;
  used[1] = true;
  que.push(1);

  int node1 = -1;
  while (!que.empty()) {
    auto x = que.front();
    // printf("x1 %d\n", x);
    que.pop();

    for (auto y : connect[x]) {
      if (used[y]) {
        continue;
      }
      used[y] = true;
      que.push(y);
      node1 = y;
    }
  }

  // printf("node %d\n", node1);

  queue<PII> que2;
  int dist = 0;
  used.assign(n + 1, false);
  used[node1] = true;
  que2.push(make_pair(node1, 0));
  while (!que2.empty()) {
    auto p = que2.front();
    que2.pop();
    int x = p.first;
    dist = max(dist, p.second);

    // printf("p %d %d\n", p.first, p.second);

    for (auto y : connect[x]) {
      if (used[y]) {
        continue;
      }
      used[y] = true;
      que2.push(make_pair(y, p.second + 1));
    }
  }
  // printf("dist %d\n", dist);

  // v1 = win
  // v2 = lose
  // vi2 = (vi1 == lose || vi0 == lose) ? win : lose
  // win lose win が繰り返す

  string ans = (dist + 1) % 3 == 2 ? "Second" : "First";
  cout << ans << endl;

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int n, d[200005];
void dfs(int x, int p = 0) {
  d[x] = d[p] + 1;
  for (auto t : g[x])
    if (t != p)
      dfs(t, x);
}
int main() {
  cin >> n;
  d[0] = -1;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1);
  memset(d + 1, 0, sizeof d + 1);
  dfs(max_element(d + 1, d + n + 1) - d);
  cout << (((*max_element(d + 1, d + n + 1)) % 3 == 1) ? "Second" : "First")
       << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int n, d[200005];
void dfs(int x, int p = 0) {
  d[x] = d[p] + 1;
  for (auto t : g[x])
    if (t != p)
      dfs(t, x);
}
int main() {
  cin >> n;
  d[0] = -1;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1);
  dfs(max_element(d + 1, d + n + 1) - d);
  cout << (((*max_element(d + 1, d + n + 1)) % 3 == 1) ? "Second" : "First")
       << endl;
  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

struct Tree {
  int N;
  vector<vector<Edge>> edges;

  void addEdge(int a, int b) { addEdge(a, b, 1); }
  void addEdge(int a, int b, int w) { edges[a].emplace_back(b, w); }

  Tree(int N) : N(N), edges(N) {}

  long long diam() {
    setDist(0);
    int start;
    long long maxd = 0;
    rep(i, N) {
      if (dist[i] > maxd) {
        maxd = dist[i];
        start = i;
      }
    }
    setDist(start);
    rep(i, N) maxd = max(maxd, dist[i]);
    return maxd;
  }

  vector<long long> dist;
  void setDist(int s) {
    dist = vector<long long>(N, -1);
    dist[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (Edge e : edges[x]) {
        if (dist[e.to] != -1)
          continue;
        dist[e.to] = dist[x] + e.w;
        q.push(e.to);
      }
    }
  }
};

int main() {
  int N;
  cin >> N;
  Tree G(N);
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.addEdge(x, y);
    G.addEdge(y, x);
  }

  long long diam = G.diam();

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

struct Tree {
  int N;
  vector<vector<Edge>> edges;

  void addEdge(int a, int b) { addEdge(a, b, 1); }
  void addEdge(int a, int b, int w) { edges[a].emplace_back(b, w); }

  Tree(int N) : N(N), edges(N) {}

  long long diam() {
    setDist(0);
    int start;
    long long maxd = -1;
    rep(i, N) {
      if (dist[i] > maxd) {
        maxd = dist[i];
        start = i;
      }
    }
    setDist(start);
    rep(i, N) maxd = max(maxd, dist[i]);
    return maxd;
  }

  vector<long long> dist;
  void setDist(int s) {
    dist = vector<long long>(N, -1);
    dist[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (Edge e : edges[x]) {
        if (dist[e.to] != -1)
          continue;
        dist[e.to] = dist[x] + e.w;
        q.push(e.to);
      }
    }
  }
};

int main() {
  int N;
  cin >> N;
  Tree G(N);
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.addEdge(x, y);
    G.addEdge(y, x);
  }

  long long diam = G.diam();

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