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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
ll mod = 1000000007;

int main() {
  int h, w;
  cin >> h >> w;
  string grid[w][h];
  queue<pair<int, int>> q;
  int cnt_b = 0;
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j) {
      auto sub = s.substr(j, 1);
      grid[j][i] = sub;
      if (sub == "#") {
        ++cnt_b;
        q.push(make_pair(i, j));
      }
    }
  }
  int out = 0;
  while (cnt_b < h * w) {
    vector<pair<int, int>> vp;
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first;
      int y = p.second;
      if (x - 1 >= 0) {
        if (grid[x - 1][y] == ".") {
          grid[x - 1][y] = "#";
          ++cnt_b;
          vp.push_back(make_pair(x - 1, y));
        }
      }
      if (x + 1 < w) {
        if (grid[x + 1][y] == ".") {
          ++cnt_b;
          grid[x + 1][y] = "#";
          vp.push_back(make_pair(x + 1, y));
        }
      }
      if (y - 1 >= 0) {
        if (grid[x][y - 1] == ".") {
          ++cnt_b;
          grid[x][y - 1] = "#";
          vp.push_back(make_pair(x, y - 1));
        }
      }
      if (y + 1 < h) {
        if (grid[x][y + 1] == ".") {
          ++cnt_b;
          grid[x][y + 1] = "#";
          vp.push_back(make_pair(x, y + 1));
        }
      }
    }
    for (auto &&e : vp)
      q.push(e);
    ++out;
  }
  cout << out << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
ll mod = 1000000007;

int main() {
  int h, w;
  cin >> h >> w;
  string grid[w][h];
  queue<pair<int, int>> q;
  int cnt_b = 0;
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j) {
      auto sub = s.substr(j, 1);
      grid[j][i] = sub;
      if (sub == "#") {
        ++cnt_b;
        q.push(make_pair(j, i));
      }
    }
  }
  int out = 0;
  while (cnt_b < h * w) {
    vector<pair<int, int>> vp;
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first;
      int y = p.second;
      if (x - 1 >= 0) {
        if (grid[x - 1][y] == ".") {
          grid[x - 1][y] = "#";
          ++cnt_b;
          vp.push_back(make_pair(x - 1, y));
        }
      }
      if (x + 1 < w) {
        if (grid[x + 1][y] == ".") {
          ++cnt_b;
          grid[x + 1][y] = "#";
          vp.push_back(make_pair(x + 1, y));
        }
      }
      if (y - 1 >= 0) {
        if (grid[x][y - 1] == ".") {
          ++cnt_b;
          grid[x][y - 1] = "#";
          vp.push_back(make_pair(x, y - 1));
        }
      }
      if (y + 1 < h) {
        if (grid[x][y + 1] == ".") {
          ++cnt_b;
          grid[x][y + 1] = "#";
          vp.push_back(make_pair(x, y + 1));
        }
      }
    }
    for (auto &&e : vp)
      q.push(e);
    ++out;
  }
  cout << out << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int inf = 2147483647, dx[] = {-1, 0, 1, 0},
          dy[] = {0, -1, 0, 1}; // 上 左 下 右
const int N = 100005, M = 1000005, mod = 1000000007;
const long long llinf = 9223372036854775807ll;
int n, m, t;
char a[1000][1000];
queue<pair<int, int>> q;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        q.push(make_pair(i, j));
    }
  while (!q.empty()) {
    int len = q.size();
    t++;
    for (int i = 1; i <= len; i++) {
      pair<int, int> p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      for (int d = 0; d < 4; d++) {
        int xx = x + dx[d], yy = y + dy[d];
        if (a[xx][yy] == '.')
          q.push(make_pair(xx, yy));
        a[xx][yy] = '#';
      }
    }
  }
  cout << t - 1;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int inf = 2147483647, dx[] = {-1, 0, 1, 0},
          dy[] = {0, -1, 0, 1}; // 上 左 下 右
const int N = 100005, M = 1000005, mod = 1000000007;
const long long llinf = 9223372036854775807ll;
int n, m, t;
char a[1005][1005];
queue<pair<int, int>> q;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        q.push(make_pair(i, j));
    }
  while (!q.empty()) {
    int len = q.size();
    t++;
    for (int i = 1; i <= len; i++) {
      pair<int, int> p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      for (int d = 0; d < 4; d++) {
        int xx = x + dx[d], yy = y + dy[d];
        if (a[xx][yy] == '.')
          q.push(make_pair(xx, yy));
        a[xx][yy] = '#';
      }
    }
  }
  cout << t - 1;
  return 0;
}
Problem p03053 (Python)

Buggy submission (status: Runtime Error):
h, w = map(int, input().split())
rb = ["#"] * (w + 2)
a = [rb + [["#"] + list(input()) + ["#"] for _ in range(h)] + rb]
p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"]
d = (-1, 0), (1, 0), (0, -1), (0, 1)
r = -1
while p:
    r += 1
    s = []
    for i, j in p:
        for di, dj in d:
            di += i
            dj += j
            if a[di][dj] == ".":
                a[di][dj] = "#"
                s.append((di, dj))
    p = s
print(r)


Fixed submission:
h, w = map(int, input().split())
rb = [["#"] * (w + 2)]
a = rb + [list("#" + input() + "#") for _ in range(h)] + rb
p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"]
d = (-1, 0), (1, 0), (0, -1), (0, 1)
r = -1
while p:
    r += 1
    s = []
    for i, j in p:
        for di, dj in d:
            di += i
            dj += j
            if a[di][dj] == ".":
                a[di][dj] = "#"
                s.append((di, dj))
    p = s
print(r)
Problem p03053 (Python)

Buggy submission (status: Time Limit Exceeded):
h, w = map(int, input().split())
rb = [["#"] * (w + 2)]
a = rb + [list("#" + input() + "#") for _ in range(h)] + rb
p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"]
d = (-1, 0), (1, 0), (0, -1), (0, 1)
r = -1
while p:
    r += 1
    s = []
    for i, j in p:
        for di, dj in d:
            di += i
            dj += j
            if a[di][dj] == ".":
                a[di][dj] = "#"
                s.append((di, dj))
    p = s
print(r)


Fixed submission:
h, w = map(int, input().split())
a = [["#"] * (w + 2)] * 2
a[1:1] = (list("#" + input() + "#") for _ in range(h))
p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"]
d = (-1, 0), (1, 0), (0, -1), (0, 1)
r = -1
while p:
    r += 1
    s = []
    for i, j in p:
        for di, dj in d:
            di += i
            dj += j
            if a[di][dj] == ".":
                a[di][dj] = "#"
                s.append((di, dj))
    p = s
print(r)
Problem p03053 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

h, w = map(int, input().split())
inf = 10**6
a = [[0] * (w + 2), *[[0] + [inf] * w + [0] for _ in range(h)], [0] * (w + 2)]
d = deque()

for i in range(h):
    for j, k in enumerate(input()):
        if k == "#":
            a[i + 1][j + 1] = 0
            d.append((i + 1, j + 1))

stp = ((0, 1), (0, -1), (1, 0), (-1, 0))
res = 0

while d:
    x, y = d.popleft()
    for k, l in stp:
        nx, ny = x + k, y + l
        if a[x][y] + 1 < a[nx][ny]:
            a[nx][ny] = a[x][y] + 1
            res = max(res, a[nx][ny])
            d.append((nx, ny))
print(res)


Fixed submission:
from collections import deque

h, w = map(int, input().split())
inf = 10**6
a = [[0] * (w + 2)] + [[0] + [inf] * w + [0] for _ in range(h)] + [[0] * (w + 2)]
d = deque()

for i in range(h):
    for j, k in enumerate(input()):
        if k == "#":
            a[i + 1][j + 1] = 0
            d.append((i + 1, j + 1))

stp = ((0, 1), (0, -1), (1, 0), (-1, 0))
res = 0

while d:
    x, y = d.popleft()
    for k, l in stp:
        nx, ny = x + k, y + l
        if a[x][y] + 1 < a[nx][ny]:
            a[nx][ny] = a[x][y] + 1
            res = max(res, a[nx][ny])
            d.append((nx, ny))
print(res)
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int MAXN = 1005;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m;
bool a[MAXN][MAXN];
int vis[MAXN][MAXN];

int main()

{
  memset(vis, -1, sizeof(vis));
  queue<pair<int, int>> q;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = c == '#';
      if (a[i][j]) {
        q.push(make_pair(i, j));
        vis[i][j] = 0;
      }
    }
  }

  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int gx = x + dx[i], gy = y + dy[i];
      if (vis[gx][gy] == -1) {
        vis[gx][gy] = vis[x][y] + 1;
        q.push(make_pair(gx, gy));
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ans = max(ans, vis[i][j]);

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int MAXN = 1005;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m;
bool a[MAXN][MAXN];
int vis[MAXN][MAXN];

int main()

{
  memset(vis, -1, sizeof(vis));
  queue<pair<int, int>> q;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = c == '#';
      if (a[i][j]) {
        q.push(make_pair(i, j));
        vis[i][j] = 0;
      }
    }
  }

  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int gx = x + dx[i], gy = y + dy[i];
      if (gx <= 0 || gy <= 0 || gx > n || gy > m)
        continue;
      if (vis[gx][gy] == -1) {
        vis[gx][gy] = vis[x][y] + 1;
        q.push(make_pair(gx, gy));
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      ans = max(ans, vis[i][j]);

  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

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

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

  int h, w;
  cin >> h >> w;
  vector<vector<char>> a(h);
  set<pair<int, int>> p;
  for (int i = 0; i < h; i++) {
    a[i].resize(w);
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        p.insert({i, j});
    }
  }
  int d[][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
  int ans = 0;
  while (!p.empty()) {
    set<pair<int, int>> s;
    for (auto const &x : p) {
      for (auto const y : d) {
        if (a[x.first + y[0]][x.second + y[1]] == '.')
          s.insert({x.first + y[0], x.second + y[1]});
      }
    }
    if (!s.empty()) {
      ans++;
      for (auto const &x : s)
        a[x.first][x.second] = '#';
    }
    p = move(s);
  }
  cout << ans << '\n';

  return 0;
}


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

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

  int h, w;
  cin >> h >> w;
  vector<vector<char>> a(h);
  set<pair<int, int>> p;
  for (int i = 0; i < h; i++) {
    a[i].resize(w);
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        p.insert({i, j});
    }
  }
  int d[][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
  int ans = 0;
  while (!p.empty()) {
    set<pair<int, int>> s;
    for (auto const &x : p) {
      for (auto const y : d) {
        int i = x.first + y[0], j = x.second + y[1];
        if (i >= 0 && i < h && j >= 0 && j < w && a[i][j] == '.')
          s.insert({i, j});
      }
    }
    if (!s.empty()) {
      ans++;
      for (auto const &x : s)
        a[x.first][x.second] = '#';
    }
    p = move(s);
  }
  cout << ans << '\n';

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int dx[4] = {-1, 0, 0, 1};
  int dy[4] = {0, 1, -1, 0};
  char buf[55][55];
  int field[55][55];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> buf[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      field[i][j] = 1e5;
    }
  }
  queue<int> v;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (buf[i][j] == '#') {
        field[i][j] = 0;
        v.push(j);
        v.push(i);
      }
    }
  }

  while (v.size()) {
    int a = v.front();
    v.pop();
    int b = v.front();
    v.pop();
    for (int k = 0; k < 4; k++) {
      int x = dx[k] + a;
      int y = dy[k] + b;
      if (x >= 0 && x < w && y >= 0 && y < h && buf[y][x] == '.' &&
          field[y][x] == 1e5) {
        field[y][x] = field[b][a] + 1;
        buf[y][x] = '#';
        v.push(x);
        v.push(y);
      }
    }
  }

  int max = -1e6;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (max < field[i][j]) {
        max = field[i][j];
      }
    }
  }

  cout << max << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int dx[4] = {-1, 0, 0, 1};
  int dy[4] = {0, 1, -1, 0};
  char buf[1005][1005];
  int field[1005][1005];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> buf[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      field[i][j] = 1e5;
    }
  }
  queue<int> v;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (buf[i][j] == '#') {
        field[i][j] = 0;
        v.push(j);
        v.push(i);
      }
    }
  }

  while (v.size()) {
    int a = v.front();
    v.pop();
    int b = v.front();
    v.pop();
    for (int k = 0; k < 4; k++) {
      int x = dx[k] + a;
      int y = dy[k] + b;
      if (x >= 0 && x < w && y >= 0 && y < h && buf[y][x] == '.' &&
          field[y][x] == 1e5) {
        field[y][x] = field[b][a] + 1;
        buf[y][x] = '#';
        v.push(x);
        v.push(y);
      }
    }
  }

  int max = -1e6;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (max < field[i][j]) {
        max = field[i][j];
      }
    }
  }

  cout << max << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < int(a); ++i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int H, W;
  cin >> H >> W;
  string A[H];
  rep(i, H) cin >> A[i];

  vector<vector<int>> dist(H, vector<int>(W, -1));
  queue<pair<int, int>> que;
  rep(y, H) {
    rep(x, W) {
      if (A[x][y] == '#') {
        dist.at(y).at(x) = 0;
        que.push(mp(y, x));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> n = que.front();
    que.pop();
    rep(i, 4) {
      int nx = n.S + dx[i], ny = n.F + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (dist.at(ny).at(nx) >= 0)
        continue;
      if (A[ny][nx] == '#')
        continue;

      dist.at(ny).at(nx) = dist.at(n.F).at(n.S) + 1;
      que.push(mp(ny, nx));
    }
  }
  int ans = 0;
  rep(i, H) {
    rep(j, W) { ans = max(ans, dist.at(i).at(j)); }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < int(a); ++i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int H, W;
  cin >> H >> W;
  string A[H];
  rep(i, H) cin >> A[i];

  vector<vector<int>> dist(H, vector<int>(W, -1));
  queue<pair<int, int>> que;
  rep(y, H) {
    rep(x, W) {
      if (A[y][x] == '#') {
        dist.at(y).at(x) = 0;
        que.push(mp(y, x));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> n = que.front();
    que.pop();
    rep(i, 4) {
      int nx = n.S + dx[i], ny = n.F + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (dist.at(ny).at(nx) >= 0)
        continue;
      if (A[ny][nx] == '#')
        continue;

      dist.at(ny).at(nx) = dist.at(n.F).at(n.S) + 1;
      que.push(mp(ny, nx));
    }
  }
  int ans = 0;
  rep(i, H) {
    rep(j, W) { ans = max(ans, dist.at(i).at(j)); }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
queue<P> q;
char c[1010][1010];
int d[1010][1010];
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> c[i];
    for (int j = 0; j < W; j++) {
      d[i][j] = 10000000;
      if (c[i][j] == '#') {
        q.push(P(i, j));
        d[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nh = now.first;
    int nw = now.second;
    int dh[4] = {0, 0, 1, -1};
    int dw[4] = {1, -1, 0, 0};
    for (int i = 0; i < 4; i++) {
      int h = nh + dh[i];
      int w = nw + dw[i];
      if (d[h][w] > d[nh][nw] + 1) {
        d[h][w] = d[nh][nw] + 1;
        ans = max(ans, d[h][w]);
        q.push(P(h, w));
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
queue<P> q;
char c[1010][1010];
int d[1010][1010];
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> c[i];
    for (int j = 0; j < W; j++) {
      d[i][j] = 10000000;
      if (c[i][j] == '#') {
        q.push(P(i, j));
        d[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nh = now.first;
    int nw = now.second;
    int dh[4] = {0, 0, 1, -1};
    int dw[4] = {1, -1, 0, 0};
    for (int i = 0; i < 4; i++) {
      int h = nh + dh[i];
      int w = nw + dw[i];
      if (0 <= h && h < H && 0 <= w && w < W) {
        if (d[h][w] > d[nh][nw] + 1) {
          d[h][w] = d[nh][nw] + 1;
          ans = max(ans, d[h][w]);
          q.push(P(h, w));
        }
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll H, W;
  cin >> H >> W;
  vector<char> vec(H * W); // vecは各頂点の白か黒かを保持
  vector<ll> answer(H * W + 1,
                    H + W); // 各黒点からの距離のうち小さいものを保持(随時更新)
  for (ll i = 0; i < H * W; i++) {
    cin >> vec.at(i);
  }
  using Graph = vector<vector<char>>;
  Graph G(H * W + 1); // グラフのサイズはマス目のサイズ+最初の仮想の点分の1
  for (ll i = 0; i < H * W; ++i) { // グラフの作成
    if (vec.at(i) == '#') {        // 仮想の点からの辺を定義
      G[H * W].push_back(i);
    }
    if (i % W != W - 1) { // 左右方向の辺の定義
      G[i].push_back(i + 1);
      G[i + 1].push_back(i);
    }
    if (i < W * (H - 1)) { // 上下方向の辺の定義
      G[i].push_back(i + W);
      G[i + W].push_back(i);
    }
  }
  vector<ll> dist(H * W + 1,
                  -2); // 全頂点を「未訪問」に初期化(-2だったら未訪問と判断)
  queue<ll> que;
  dist[H * W] = -1; // 仮想の点をスタートにする
  que.push(H * W);
  while (!que.empty()) {
    ll v = que.front(); // キューから先頭頂点を取り出す
    que.pop();
    for (ll nv : G[v]) { // vから辿れる頂点をすべて調べる
      if (dist[nv] != -2) {
        continue;
      } // すでに発見済みの頂点は探索しない
      dist[nv] =
          dist[v] +
          1; // 新たな白色頂点nv について距離情報distを更新してキューに追加する
      answer.at(nv) =
          min(answer.at(nv),
              dist[nv]); // 各黒点からの距離の最小値を必要に応じて更新
      que.push(nv);
    }
  }
  ll rock =
      0; // rockは各白点の(各黒点からの距離の最小値の)最大値を保持(随時更新)
  for (ll i = 0; i < H * W; i++) {
    if (vec.at(i) == '.') {
      rock = max(answer.at(i), rock);
    }
  }
  cout << rock << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll H, W;
  cin >> H >> W;
  vector<char> vec(H * W); // vecは各頂点の白か黒かを保持
  vector<ll> answer(H * W + 1,
                    H + W); // 各黒点からの距離のうち小さいものを保持(随時更新)
  for (ll i = 0; i < H * W; i++) {
    cin >> vec.at(i);
  }
  using Graph = vector<vector<ll>>;
  Graph G(H * W + 1); // グラフのサイズはマス目のサイズ+最初の仮想の点分の1
  for (ll i = 0; i < H * W; ++i) { // グラフの作成
    if (vec.at(i) == '#') {        // 仮想の点からの辺を定義
      G[H * W].push_back(i);
    }
    if (i % W != W - 1) { // 左右方向の辺の定義
      G[i].push_back(i + 1);
      G[i + 1].push_back(i);
    }
    if (i < W * (H - 1)) { // 上下方向の辺の定義
      G[i].push_back(i + W);
      G[i + W].push_back(i);
    }
  }
  vector<ll> dist(H * W + 1,
                  -2); // 全頂点を「未訪問」に初期化(-2だったら未訪問と判断)
  queue<ll> que;
  dist[H * W] = -1; // 仮想の点をスタートにする
  que.push(H * W);
  while (!que.empty()) {
    ll v = que.front(); // キューから先頭頂点を取り出す
    que.pop();
    for (ll nv : G[v]) { // vから辿れる頂点をすべて調べる
      if (dist[nv] != -2) {
        continue;
      } // すでに発見済みの頂点は探索しない
      dist[nv] =
          dist[v] +
          1; // 新たな白色頂点nv について距離情報distを更新してキューに追加する
      answer.at(nv) =
          min(answer.at(nv),
              dist[nv]); // 各黒点からの距離の最小値を必要に応じて更新
      que.push(nv);
    }
  }
  ll rock =
      0; // rockは各白点の(各黒点からの距離の最小値の)最大値を保持(随時更新)
  for (ll i = 0; i < H * W; i++) {
    if (vec.at(i) == '.') {
      rock = max(answer.at(i), rock);
    }
  }
  cout << rock << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, ans;
char s[1111][1111];
int vis[1111][1111];
struct node {
  int x, y, step;
} nex, now;
queue<node> que;
void bfs() {
  while (!que.empty()) {
    now = que.front();
    que.pop();
    ans = max(ans, now.step);
    for (int i = 0; i < 4; i++) {
      nex.x = now.x + dx[i];
      nex.y = now.y + dy[i];
      nex.step = now.step + 1;

      if (!vis[nex.x][nex.y]) {
        s[nex.x][nex.y] == '#';
        vis[nex.x][nex.y] = 1;
        que.push(nex);
      }
    }
  }
}

int main() {

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> s[i][j];
      now.x = i, now.y = j, now.step = 0;
      if (s[i][j] == '#')
        vis[i][j] = 1, que.push(now);
    }
  bfs();
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, ans;
char s[1111][1111];
int vis[1111][1111];
struct node {
  int x, y, step;
} nex, now;
queue<node> que;
void bfs() {
  while (!que.empty()) {
    now = que.front();
    que.pop();
    ans = max(ans, now.step);
    for (int i = 0; i < 4; i++) {
      nex.x = now.x + dx[i];
      nex.y = now.y + dy[i];
      nex.step = now.step + 1;

      if (s[nex.x][nex.y] == '.' && !vis[nex.x][nex.y]) {
        s[nex.x][nex.y] == '#';
        vis[nex.x][nex.y] = 1;
        que.push(nex);
      }
    }
  }
}

int main() {

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> s[i][j];
      now.x = i, now.y = j, now.step = 0;
      if (s[i][j] == '#')
        vis[i][j] = 1, que.push(now);
    }
  bfs();
  printf("%d\n", ans);
  return 0;
}
Problem p03054 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int h, w, n, sr, sc;
char s1[N], s2[N];
bool judge(char opt) {
  int x = sr, y = sc;
  for (int i = 1; i <= n; i++) {
    if (s1[i] == opt) {
      if (opt == 'U')
        x--;
      else if (opt == 'D')
        x++;
      else if (opt == 'L')
        y--;
      else
        y++;
    }
    if (x < 1 || x > h || y < 1 || y > w)
      return true;
    if (opt == 'U') {
      if (s2[i] == 'D' && x + 1 <= h)
        x++;
    } else if (opt == 'D') {
      if (s2[i] == 'U' && x - 1 >= 1)
        x--;
    } else if (opt == 'L') {
      if (s2[i] == 'R' && y + 1 <= w)
        y++;
    } else {
      if (s2[i] == 'L' && y - 1 >= 1)
        y--;
    }
  }
  return false;
}
int main() {
  scanf("%d%d%d", &h, &w, &n);
  scanf("%d%d", &sr, &sc);
  scanf("%s%s", s1 + 1, s2 + 1);
  if (judge('U') || judge('D') || judge('L') || judge('R'))
    printf("NO\n");
  else
    printf("YES\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int h, w, n, sr, sc;
char s1[N], s2[N];
bool judge(char opt) {
  int x = sr, y = sc;
  for (int i = 1; i <= n; i++) {
    if (s1[i] == opt) {
      if (opt == 'U')
        x--;
      else if (opt == 'D')
        x++;
      else if (opt == 'L')
        y--;
      else
        y++;
    }
    if (x < 1 || x > h || y < 1 || y > w)
      return true;
    if (opt == 'U') {
      if (s2[i] == 'D' && x + 1 <= h)
        x++;
    } else if (opt == 'D') {
      if (s2[i] == 'U' && x - 1 >= 1)
        x--;
    } else if (opt == 'L') {
      if (s2[i] == 'R' && y + 1 <= w)
        y++;
    } else {
      if (s2[i] == 'L' && y - 1 >= 1)
        y--;
    }
  }
  return false;
}
int main() {
  scanf("%d%d%d", &h, &w, &n);
  scanf("%d%d", &sr, &sc);
  scanf("%s%s", s1 + 1, s2 + 1);
  if (judge('U') || judge('D') || judge('L') || judge('R'))
    printf("NO\n");
  else
    printf("YES\n");
}
Problem p03054 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mx = 200005;
int h, w, n, x, y, r, l, u, d;
char s[mx], t[mx];

int main() {
  scanf("%d%d%d%d%d%s%s", &h, &w, &n, &x, &y, s, t);
  l = 1;
  r = w;
  u = 1;
  d = h;
  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == 'L' && r != w)
      r++;
    else if (t[i] == 'R' && l != 1)
      l--;
    else if (t[i] == 'U' && d != h)
      d++;
    else if (t[i] == 'D' && u != 1)
      u--;
    if (s[i] == 'L')
      l++;
    else if (s[i] == 'R')
      r--;
    else if (s[i] == 'U')
      u++;
    else
      d--;
    if (l > r || u > d)
      return puts("NO");
  }
  puts(l <= y && y <= r && u <= x && x <= d ? "YES" : "NO");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mx = 200005;
int h, w, n, x, y, r, l, u, d;
char s[mx], t[mx];

int main() {
  scanf("%d%d%d%d%d%s%s", &h, &w, &n, &x, &y, s, t);
  l = 1;
  r = w;
  u = 1;
  d = h;
  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == 'L' && r != w)
      r++;
    else if (t[i] == 'R' && l != 1)
      l--;
    else if (t[i] == 'U' && d != h)
      d++;
    else if (t[i] == 'D' && u != 1)
      u--;
    if (s[i] == 'L')
      l++;
    else if (s[i] == 'R')
      r--;
    else if (s[i] == 'U')
      u++;
    else
      d--;
    if (l > r || u > d)
      return puts("NO") * 0;
  }
  puts(l <= y && y <= r && u <= x && x <= d ? "YES" : "NO");
}
Problem p03054 (C++)

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

using namespace std;
string dirs = "LRUD";
int pre[2][4][200005];
int di[] = {0, 0, -1, 1};
int dj[] = {-1, 1, 0, 0};
int n, m, l;

bool valid(int i, int j) { return i > 0 && j > 0 && i <= n && j <= m; }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> n >> m >> l;
  int x, y;
  cin >> x >> y;
  string s[2];
  cin >> s[0] >> s[1];

  bool found = false;
  for (int d = 0; d < 4; d++) {
    int nx = x, ny = y;
    for (int i = 0; i < l; i++) {
      if (dirs.find(s[0][i]) == d) {
        nx += di[d], ny += dj[d];
      }
      if (!valid(nx, ny)) {
        found = true;
        break;
      }
      if (dirs.find(s[1][i]) == (d ^ 1)) {
        if (valid(nx + di[d ^ 1], ny + dj[d ^ 1])) {
          nx += di[d ^ 1], ny += dj[d ^ 1];
        }
      }
    }
  }
  cout << (found ? "NO\n" : "YES\n");
  return 0;
}


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

using namespace std;
string dirs = "LRUD";
int pre[2][4][200005];
int di[] = {0, 0, -1, 1};
int dj[] = {-1, 1, 0, 0};
int n, m, l;

bool valid(int i, int j) { return i > 0 && j > 0 && i <= n && j <= m; }

int main() {
#ifndef ONLINE_JUDGE
  // freopen("input.in", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> n >> m >> l;
  int x, y;
  cin >> x >> y;
  string s[2];
  cin >> s[0] >> s[1];

  bool found = false;
  for (int d = 0; d < 4; d++) {
    int nx = x, ny = y;
    for (int i = 0; i < l; i++) {
      if (dirs.find(s[0][i]) == d) {
        nx += di[d], ny += dj[d];
      }
      if (!valid(nx, ny)) {
        found = true;
        break;
      }
      if (dirs.find(s[1][i]) == (d ^ 1)) {
        if (valid(nx + di[d ^ 1], ny + dj[d ^ 1])) {
          nx += di[d ^ 1], ny += dj[d ^ 1];
        }
      }
    }
  }
  cout << (found ? "NO\n" : "YES\n");
  return 0;
}
Problem p03054 (C++)

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

const int N = 100010;

char str1[N], str2[N];
int n;

int main() {

  int n1, n2, x1, x2;
  scanf("%d%d%d", &n1, &n2, &n);
  scanf("%d%d", &x1, &x2);
  scanf("%s%s", str1 + 1, str2 + 1);

  bool f = 1;
  int L = 0, R = n1 + 1;
  for (int i = n; i >= 1; i--) {

    if (str2[i] == 'L' || str2[i] == 'R') {

    } else if (str2[i] == 'D') {
      L = std::max(L - 1, 0);
    } else {
      R = std::min(R + 1, n1 + 1);
    }

    if (str1[i] == 'L' || str1[i] == 'R') {

    } else if (str1[i] == 'D') {
      R = R - 1;
    } else {
      L = L + 1;
    }
    if (L + 1 >= R) {
      f = 0;
      break;
    }
  }
  if (x1 <= L || R <= x1) {
    f = 0;
  }
  L = 0, R = n2 + 1;
  for (int i = n; i >= 1 && f; i--) {

    if (str2[i] == 'U' || str2[i] == 'D') {

    } else if (str2[i] == 'L') {
      R = std::min(R + 1, n2 + 1);
    } else {
      L = std::max(L - 1, 0);
    }

    if (str1[i] == 'U' || str1[i] == 'D') {

    } else if (str1[i] == 'L') {
      L = L + 1;
    } else {
      R = R - 1;
    }
    if (L + 1 >= R) {
      f = 0;
      break;
    }
  }
  if (x2 <= L || R <= x2) {
    f = 0;
  }
  if (f) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
  return 0;
}


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

const int N = 200010;

char str1[N], str2[N];
int n;

int main() {

  int n1, n2, x1, x2;
  scanf("%d%d%d", &n1, &n2, &n);
  scanf("%d%d", &x1, &x2);
  scanf("%s%s", str1 + 1, str2 + 1);

  bool f = 1;
  int L = 0, R = n1 + 1;
  for (int i = n; i >= 1; i--) {

    if (str2[i] == 'L' || str2[i] == 'R') {

    } else if (str2[i] == 'D') {
      L = std::max(L - 1, 0);
    } else {
      R = std::min(R + 1, n1 + 1);
    }

    if (str1[i] == 'L' || str1[i] == 'R') {

    } else if (str1[i] == 'D') {
      R = R - 1;
    } else {
      L = L + 1;
    }
    if (L + 1 >= R) {
      f = 0;
      break;
    }
  }
  if (x1 <= L || R <= x1) {
    f = 0;
  }
  L = 0, R = n2 + 1;
  for (int i = n; i >= 1 && f; i--) {

    if (str2[i] == 'U' || str2[i] == 'D') {

    } else if (str2[i] == 'L') {
      R = std::min(R + 1, n2 + 1);
    } else {
      L = std::max(L - 1, 0);
    }

    if (str1[i] == 'U' || str1[i] == 'D') {

    } else if (str1[i] == 'L') {
      L = L + 1;
    } else {
      R = R - 1;
    }
    if (L + 1 >= R) {
      f = 0;
      break;
    }
  }
  if (x2 <= L || R <= x2) {
    f = 0;
  }
  if (f) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
  return 0;
}
Problem p03054 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")

#include <bitset>
#include <iostream>
using namespace std;

int h, w, n;
int sr, sc;

string s, t;

bitset<200003> b;

template <char c1, char c2> bool solve(int m, int pos) {
  b.reset();
  b[0] = true;
  b[m + 1] = true;

  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == c1)
      b &= (b << 1);
    else if (t[i] == c2)
      b &= (b >> 1);

    b[0] = true;
    b[m + 1] = true;

    if (s[i] == c1)
      b |= (b << 1);
    else if (s[i] == c2)
      b |= (b >> 1);

    b[0] = true;
    b[m + 1] = true;
  }

  return b[pos + 1];
}

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

  cin >> h >> w >> n;
  cin >> sr >> sc;
  sr--;
  sc--;
  cin >> s >> t;

  bool ans = false;
  if (solve<'L', 'R'>(w, sc) || solve<'U', 'D'>(h, sr)) {
    ans = true;
  }

  cout << (ans ? "NO" : "YES") << endl;

  return 0;
}

Fixed submission:
#pragma GCC optimize("O3", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC optimize("tree-vectorize")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")

#include <bitset>
#include <iostream>
using namespace std;

int h, w, n;
int sr, sc;

string s, t;

bitset<200003> b;

template <char c1, char c2> bool solve(int m, int pos) {
  b.reset();
  b[0] = true;
  b[m + 1] = true;

  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == c1)
      b &= (b << 1);
    else if (t[i] == c2)
      b &= (b >> 1);

    b[0] = true;
    b[m + 1] = true;

    if (s[i] == c1)
      b |= (b << 1);
    else if (s[i] == c2)
      b |= (b >> 1);

    b[0] = true;
    b[m + 1] = true;
  }

  return b[pos + 1];
}

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

  cin >> h >> w >> n;
  cin >> sr >> sc;
  sr--;
  sc--;
  cin >> s >> t;

  bool ans = false;
  if (solve<'L', 'R'>(w, sc) || solve<'U', 'D'>(h, sr)) {
    ans = true;
  }

  cout << (ans ? "NO" : "YES") << endl;

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
#ifdef DEBUG
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL << 60;

int main(void) {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<ll> dist(n, INF);
  auto dfs = [&](auto &&self, ll v, ll p, ll d) -> void {
    dist[v] = d;
    for (auto to : g[v])
      if (to != p) {
        self(self, to, v, d + 1);
      }
  };
  dfs(dfs, 0, -1, 0);
  ll ma = 0, idx = -1;
  REP(i, n) if (ma < dist[i]) ma = dist[i], idx = i;

  dist.assign(n, INF);
  dfs(dfs, idx, -1, 0);
  ma = 0;
  REP(i, n) chmax(ma, dist[i]);

  if (ma == 0)
    cout << "First\n";
  else if (ma == 1)
    cout << "Second\n";
  else {
    ma -= 2;
    ma %= 3;
    if (ma <= 1)
      cout << "First\n";
    else
      cout << "Second\n";
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
#ifdef DEBUG
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL << 60;

int main(void) {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<ll> dist(n, INF);
  auto dfs = [&](auto &&self, ll v, ll p, ll d) -> void {
    dist[v] = d;
    for (auto to : g[v])
      if (to != p) {
        self(self, to, v, d + 1);
      }
  };
  dfs(dfs, 0, -1, 0);
  ll ma = -1, idx = -1;
  REP(i, n) if (ma < dist[i]) ma = dist[i], idx = i;

  dist.assign(n, INF);
  dfs(dfs, idx, -1, 0);
  ma = 0;
  REP(i, n) chmax(ma, dist[i]);

  if (ma == 0)
    cout << "First\n";
  else if (ma == 1)
    cout << "Second\n";
  else {
    ma -= 2;
    ma %= 3;
    if (ma <= 1)
      cout << "First\n";
    else
      cout << "Second\n";
  }

  return 0;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int fir[N], snd[N], d[N];
int hed[N], to[N << 1], nxt[N << 1], cnt;
int L = 0;
inline void dfs(int x, int pre) {
  d[x] = d[pre] + 1;
  fir[x] = snd[x] = d[x];
  for (int i = hed[x]; i; i = nxt[i]) {
    int v = to[i];
    if (v == pre)
      continue;
    dfs(v, x);
    if (fir[v] >= fir[x])
      snd[x] = fir[x], fir[x] = fir[v];
    else if (fir[v] >= snd[x])
      snd[x] = fir[v];
  }
  L = max(L, fir[x] + snd[x] - 2 * d[x]);
}
int n;
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ++cnt;
    to[cnt] = v, nxt[cnt] = hed[u];
    hed[u] = cnt;
    ++cnt;
    to[cnt] = u, nxt[cnt] = hed[v];
    hed[v] = cnt;
  }
  dfs(1, 0);
  cout << (L % 3 == 1 ? "Second" : "First") << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int fir[N], snd[N], d[N];
int hed[N], to[N << 1], nxt[N << 1], cnt;
int L = 0;
inline void dfs(int x, int pre) {
  d[x] = d[pre] + 1;
  fir[x] = snd[x] = d[x];
  for (int i = hed[x]; i; i = nxt[i]) {
    int v = to[i];
    if (v == pre)
      continue;
    dfs(v, x);
    if (fir[v] >= fir[x])
      snd[x] = fir[x], fir[x] = fir[v];
    else if (fir[v] >= snd[x])
      snd[x] = fir[v];
  }
  L = max(L, fir[x] + snd[x] - 2 * d[x]);
}
int n;
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ++cnt;
    to[cnt] = v, nxt[cnt] = hed[u];
    hed[u] = cnt;
    ++cnt;
    to[cnt] = u, nxt[cnt] = hed[v];
    hed[v] = cnt;
  }
  dfs(1, 0);
  cout << (L % 3 == 1 ? "Second" : "First") << endl;
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back

vector<vector<int>> E;
// Eは頂点数＋１をとっておく

int diameter(int x) { // 頂点数を入れる
  int ans = 0, en;
  queue<int> q;
  vector<int> dis(x + 1, 0); // 何マスで訪れたか
  dis.at(x) = -1;
  q.push(x);

  while (q.size() != 0) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < E.at(s).size(); i++) {
      if (dis.at(E.at(s).at(i)) == 0) {
        dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1);
        q.push(E.at(s).at(i));
        if (ans < dis.at(E.at(s).at(i))) {
          en = E.at(s).at(i);
        }
        ans = max(ans, dis.at(E.at(s).at(i)));
      }
    }
  }

  dis.assign(x + 1, 0);
  dis.at(en) = -1;
  q.push(en);
  while (q.size() != 0) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < E.at(s).size(); i++) {
      if (dis.at(E.at(s).at(i)) == 0) {
        dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1);
        q.push(E.at(s).at(i));
        ans = max(ans, dis.at(E.at(s).at(i)));
      }
    }
  }
  return ans;
}

int main() {
  int n, d;
  cin >> n;
  E.assign(n + 1, vector<int>());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    E.at(a).pb(b);
    E.at(b).pb(a); // Edge
  }
  d = diameter(n);
  d++;
  if (d % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back

vector<vector<int>> E;
// Eは頂点数＋１をとっておく

int diameter(int x) { // 頂点数を入れる
  int ans = 0, en = x;
  queue<int> q;
  vector<int> dis(x + 1, 0); // 何マスで訪れたか
  dis.at(x) = -1;
  q.push(x);

  while (q.size() != 0) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < E.at(s).size(); i++) {
      if (dis.at(E.at(s).at(i)) == 0) {
        dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1);
        q.push(E.at(s).at(i));
        if (ans < dis.at(E.at(s).at(i))) {
          en = E.at(s).at(i);
        }
        ans = max(ans, dis.at(E.at(s).at(i)));
      }
    }
  }

  dis.assign(x + 1, 0);
  dis.at(en) = -1;
  q.push(en);
  while (q.size() != 0) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < E.at(s).size(); i++) {
      if (dis.at(E.at(s).at(i)) == 0) {
        dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1);
        q.push(E.at(s).at(i));
        ans = max(ans, dis.at(E.at(s).at(i)));
      }
    }
  }
  return ans;
}

int main() {
  int n, d;
  cin >> n;
  E.assign(n + 1, vector<int>());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    E.at(a).pb(b);
    E.at(b).pb(a); // Edge
  }
  d = diameter(n);
  d++;
  if (d % 3 == 2) {
    cout << "Second" << endl;
  } else {
    cout << "First" << endl;
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e5 + 7;
int dp[N];

void init() {
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    int some_lose = 0;
    for (int j = 1; j <= 2; j++) {
      if (i - j >= 0 && !dp[i - j]) {
        some_lose = 1;
      }
    }
    if (some_lose)
      dp[i] = 1;
  }
}

int used[N];
vector<int> g[N];

void dfs(int cur, int ch, pair<int, int> &mx) {
  used[cur] = 1;
  mx = max(mx, {ch, cur});
  for (auto t : g[cur]) {
    if (!used[t]) {
      dfs(t, ch + 1, mx);
    }
  }
}

int solve() {
  pair<int, int> mx = {-1, -1};
  dfs(0, 0, mx);
  int v = mx.second;
  fill(used, used + N, 0);
  mx = {-1, -1};
  dfs(v, 0, mx);
  return mx.first;
}

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

  init();
  // for (int i = 0; i < 10; i++) {
  //     cerr << dp[i] << ' ';
  // }
  // cerr << endl;
  int n;
  cin >> n;
  for (int i = 0; i + 1 < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int d = solve();
  if (dp[d]) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 2e5 + 7;
int dp[N];

void init() {
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    int some_lose = 0;
    for (int j = 1; j <= 2; j++) {
      if (i - j >= 0 && !dp[i - j]) {
        some_lose = 1;
      }
    }
    if (some_lose)
      dp[i] = 1;
  }
}

int used[N];
vector<int> g[N];

void dfs(int cur, int ch, pair<int, int> &mx) {
  used[cur] = 1;
  mx = max(mx, {ch, cur});
  for (auto t : g[cur]) {
    if (!used[t]) {
      dfs(t, ch + 1, mx);
    }
  }
}

int solve() {
  pair<int, int> mx = {-1, -1};
  dfs(0, 0, mx);
  int v = mx.second;
  fill(used, used + N, 0);
  mx = {-1, -1};
  dfs(v, 0, mx);
  return mx.first;
}

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

  init();
  // for (int i = 0; i < 10; i++) {
  //     cerr << dp[i] << ' ';
  // }
  // cerr << endl;
  int n;
  cin >> n;
  for (int i = 0; i + 1 < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int d = solve();
  if (dp[d]) {
    cout << "First\n";
  } else {
    cout << "Second\n";
  }
}
Problem p03055 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int d[101];
vector<int> gr[101];
void dfs(int cur, int parent) {
  for (int adj : gr[cur]) {
    if (adj != parent) {
      d[adj] = d[cur] + 1;
      dfs(adj, cur);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }

  dfs(0, -1);
  int edge = max_element(d, d + n) - d;
  d[edge] = 0;
  dfs(edge, -1);
  int ans = *max_element(d, d + n);
  if (ans % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int d[N];
vector<int> gr[N];
void dfs(int cur, int parent) {
  for (int adj : gr[cur]) {
    if (adj != parent) {
      d[adj] = d[cur] + 1;
      dfs(adj, cur);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }

  dfs(0, -1);
  int edge = max_element(d, d + n) - d;
  d[edge] = 0;
  dfs(edge, -1);
  int ans = *max_element(d, d + n);
  if (ans % 3 == 1)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}
Problem p03055 (C++)

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

const int MAXN = 100010;
int N;
bool dp[MAXN];
vector<int> adj[MAXN];
int s, md;

void dfs(int cur, int prv, int d) {
  if (d > md) {
    s = cur;
    md = d;
  }
  for (int nxt : adj[cur]) {
    if (nxt == prv)
      continue;
    dfs(nxt, cur, d + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, -1, 1);
  dfs(s, -1, 1);

  dp[0] = false;
  dp[1] = true;
  dp[2] = false;
  for (int i = 3; i <= md; i++) {
    dp[i] = (!dp[i - 1] || !dp[i - 2]);
  }
  if (dp[md]) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }

  return 0;
}


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

const int MAXN = 200010;
int N;
bool dp[MAXN];
vector<int> adj[MAXN];
int s, md;

void dfs(int cur, int prv, int d) {
  if (d > md) {
    s = cur;
    md = d;
  }
  for (int nxt : adj[cur]) {
    if (nxt == prv)
      continue;
    dfs(nxt, cur, d + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, -1, 1);
  dfs(s, -1, 1);

  dp[0] = false;
  dp[1] = true;
  dp[2] = false;
  for (int i = 3; i <= md; i++) {
    dp[i] = (!dp[i - 1] || !dp[i - 2]);
  }
  if (dp[md]) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }

  return 0;
}