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

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

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework() {}

void read() {}

int deg[MAXN];

void solve(int casi) {
  //	cout << "Case #" << casi << ": ";
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    deg[x]++;
    deg[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (deg[i] & 1) {
      puts("NO");
      return;
    }
  puts("YES");
}

void printans() {}

int main() {
  std::ios::sync_with_stdio(false);
  prework();
  int T = 1;
  //	cin>>T;
  for (int i = 1; i <= T; i++) {
    read();
    solve(i);
    printans();
  }
  return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework() {}

void read() {}

int deg[MAXN];

void solve(int casi) {
  //	cout << "Case #" << casi << ": ";
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    deg[x]++;
    deg[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (deg[i] & 1) {
      puts("NO");
      return;
    }
  puts("YES");
}

void printans() {}

int main() {
  //	std::ios::sync_with_stdio(false);
  prework();
  int T = 1;
  //	cin>>T;
  for (int i = 1; i <= T; i++) {
    read();
    solve(i);
    printans();
  }
  return 0;
}
Problem p03724 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int count[100001] = {0};
  int i = 0;
  int tmp;
  for (i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b) {
      tmp = a;
      a = b;
      b = tmp;
    }
    int k;
    for (k = a; k < b; k++) {
      count[k]++;
    }
  }
  for (i = 0; i < N; i++) {
    if (count[i] % 2) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int count[100001] = {0};
  int i = 0;
  int tmp;
  for (i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b) {
      tmp = a;
      a = b;
      b = tmp;
    }
    count[a]++;
    count[b]++;
    // int k;
    // for(k=a;k<b;k++){
    //   count[k]++;
    // }
  }
  for (i = 0; i < N; i++) {
    if (count[i] % 2) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03724 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, m;
  cin >> n >> m;
  vl v(n, 0);
  ll a, b;
  rep(i, n) {
    cin >> a >> b;
    a--;
    b--;
    v[a]++;
    v[b]++;
  }
  rep(i, n) {
    if (v[i] % 2 == 1) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, m;
  cin >> n >> m;
  vl v(n, 0);
  ll a, b;
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    v[a]++;
    v[b]++;
  }
  rep(i, n) {
    if (v[i] % 2 == 1) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03724 (C++)

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)500 + 100;
const int M = (int)1e5 + 100;
const int K = (int)200;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n, m;
int a[N];

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    a[x] ^= 1;
    a[y] ^= 1;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
  return 0;
}

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>

using namespace std;

const int N = (int)1e5 + 100;
const int M = (int)1e5 + 100;
const int K = (int)200;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const ld EPS = (ld)1e-9;
const ll LINF = (ll)1e18;

int n, m;
int a[N];

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    a[x] ^= 1;
    a[y] ^= 1;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
  return 0;
}
Problem p03724 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

const ll INF = 1e9;

template <typename F> double bsearch(double ok, double ng, ll loop, F f) {
  REP(i, loop) {
    double middle = (ok + ng) / 2;
    (f(middle) ? ok : ng) = middle;
  }
  return ok;
}

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m;
  cin >> n >> m;
  vvl path(m);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  REP(i, n) {
    if (path[i].size() % 2 == 1) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define MAXC(c, x) (c = max(c, x))
#define MINC(c, x) (c = min(c, x))

#define REP(i, n) for (auto i = 0 * (n), i##_len = (n); i < i##_len; ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, n) for (ll i = s, i##_len = (ll)(n); i < i##_len; ++i)
#define TEN(x) ((ll)1e##x)
const ll mod = TEN(9) + 7;

const ll INF = 1e9;

template <typename F> double bsearch(double ok, double ng, ll loop, F f) {
  REP(i, loop) {
    double middle = (ok + ng) / 2;
    (f(middle) ? ok : ng) = middle;
  }
  return ok;
}

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m;
  cin >> n >> m;
  vvl path(n);
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  REP(i, n) {
    if (path[i].size() % 2 == 1) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03724 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define inf 0x7fffffff
using namespace std;
// 1232
int n, m, k, i, a, c, o[34], d, b, l[2000004], j[3333],
    dx[10] = {0, 1, -1, 0, 0, -1, -1, 1, 1},
    dy[10] = {0, 0, 0, -1, 1, 1, -1, 1, -1};
long long x, y, z;
stack<int> s;
struct P {
  int x, y;
  long long z;
  bool operator<(const P &a) const { return y > a.y; }
};
// map<int, int> p;
queue<P> q;
string r;
vector<P> v[1000100];
// vector<long long> ;
bool sdf(long long a, long long b) { return a > b; }

int main() {
  scanf("%d %d", &a, &b);
  for (int t = 1; t <= b; t++) {
    scanf("%d %d", &c, &d);
    o[c]++;
    o[d]++;
  }
  for (int t = 1; t <= a; t++)
    if (o[t] % 2) {
      puts("NO");
      return 0;
    }
  puts("YES");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define inf 0x7fffffff
using namespace std;
// 1232
int n, m, k, i, a, c, o[3000004], d, b, l[2000004], j[3333],
    dx[10] = {0, 1, -1, 0, 0, -1, -1, 1, 1},
    dy[10] = {0, 0, 0, -1, 1, 1, -1, 1, -1};
long long x, y, z;
stack<int> s;
struct P {
  int x, y;
  long long z;
  bool operator<(const P &a) const { return y > a.y; }
};
// map<int, int> p;
queue<P> q;
string r;
vector<P> v[1000100];
// vector<long long> ;
bool sdf(long long a, long long b) { return a > b; }

int main() {
  scanf("%d %d", &a, &b);
  for (int t = 1; t <= b; t++) {
    scanf("%d %d", &c, &d);
    o[c]++;
    o[d]++;
  }
  for (int t = 1; t <= a; t++)
    if (o[t] % 2) {
      puts("NO");
      return 0;
    }
  puts("YES");
  return 0;
}
Problem p03724 (C++)

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

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> a(M), b(M);
  for (int i = 0; i < N; i++) {
    scanf(" %d %d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
  }

  std::vector<int> deg(N);
  for (int i = 0; i < M; i++) {
    deg[a[i]]++;
    deg[b[i]]++;
  }

  bool flag = true;
  for (int &c : deg) {
    if (c % 2 != 0)
      flag = false;
  }
  if (flag == true)
    printf("YES\n");
  else
    printf("NO\n");
}

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

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> a(M), b(M);
  for (int i = 0; i < M; i++) {
    scanf(" %d %d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
  }

  std::vector<int> deg(N);
  for (int i = 0; i < M; i++) {
    deg[a[i]]++;
    deg[b[i]]++;
  }

  bool flag = true;
  for (int &c : deg) {
    if (c % 2 != 0)
      flag = false;
  }
  if (flag == true)
    printf("YES\n");
  else
    printf("NO\n");
}
Problem p03724 (C++)

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

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x, y) scanf("%llu%llu", &x, &y)
#define Scll3(x, y, z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x, y) for (int x = 0; x < y; x++)
#define Repe(x, y, z) for (int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n");
// }}} 2

// }}} 1

int main() {

  int N, M;
  Scd2(N, M);

  int a, b;
  vector<int> imo(N, 0);
  Rep(i, N) {
    Scd2(a, b);
    a--, b--;
    imo[a]++;
    imo[b]--;
  }

  bool ans = true;
  Rep(i, N) {
    imo[i + 1] += imo[i];
    if (imo[i] & 1)
      ans = false;
  }

  printf("%s\n", ans ? "YES" : "NO");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long unsigned int ll;

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x, y) scanf("%llu%llu", &x, &y)
#define Scll3(x, y, z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x, y) for (int x = 0; x < y; x++)
#define Repe(x, y, z) for (int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\n") : printf("No\n");
// }}} 2

// }}} 1

int main() {

  int N, M;
  Scd2(N, M);

  int a, b;
  vector<int> imo(N, 0);
  Rep(i, M) {
    Scd2(a, b);
    a--, b--;
    imo[a]++;
    imo[b]--;
  }

  bool ans = true;
  Rep(i, N) {
    imo[i + 1] += imo[i];
    if (imo[i] & 1)
      ans = false;
  }

  printf("%s\n", ans ? "YES" : "NO");

  return 0;
}
Problem p03724 (C++)

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

long long node[10100];
long long n, m, a, b;

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    node[a]++;
    node[b]--;
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += node[i];
    if (sum %= 2) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";

  return 0;
}


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

long long node[101000];
long long n, m, a, b;

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    node[a]++;
    node[b]--;
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += node[i];
    if (sum %= 2) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";

  return 0;
}
Problem p03725 (C++)

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

int main() {
  long long H, W, K, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  string S[800] = {};
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  queue<pair<pair<long long, long long>, long long>> Q;
  long long SX, SY;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (S[i][j] == 'S')
        SX = i, SY = j;
  Q.push({{SX, SY}, K});
  while (!Q.empty()) {
    pair<pair<long long, long long>, long long> PP = Q.front();
    Q.pop();
    pair<long long, long long> P = PP.first;
    long long X = PP.second;
    S[P.first][P.second] = 'X';
    if (P.first == 0 || P.first == H - 1 || P.second == 0 ||
        P.second == W - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (X == 0)
      continue;
    for (int i = 0; i < 4; i++) {
      long long NX = P.first + dx[i], NY = P.second + dy[i];
      if (NX < 0 || H <= NX || NY < 0 || W <= NY) {
      }
      if (S[NX][NY] == '.')
        Q.push({{NX, NY}, X - 1});
    }
  }
  long long Dist = 1000;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      if (S[i][j] == 'X') {
        Dist = min({Dist, i, H - i - 1, j, W - j - 1});
      }
    }
  }
  cout << (Dist + K - 1) / K + 1 << endl;
}

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

int main() {
  long long H, W, K, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  string S[800] = {};
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  queue<pair<pair<long long, long long>, long long>> Q;
  long long SX, SY;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (S[i][j] == 'S')
        SX = i, SY = j;
  Q.push({{SX, SY}, K});
  while (!Q.empty()) {
    pair<pair<long long, long long>, long long> PP = Q.front();
    Q.pop();
    pair<long long, long long> P = PP.first;
    long long X = PP.second;
    if (S[P.first][P.second] == 'X')
      continue;
    S[P.first][P.second] = 'X';
    if (P.first == 0 || P.first == H - 1 || P.second == 0 ||
        P.second == W - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (X == 0)
      continue;
    for (int i = 0; i < 4; i++) {
      long long NX = P.first + dx[i], NY = P.second + dy[i];
      if (NX < 0 || H <= NX || NY < 0 || W <= NY) {
      }
      if (S[NX][NY] == '.')
        Q.push({{NX, NY}, X - 1});
    }
  }
  long long Dist = 1000;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      if (S[i][j] == 'X') {
        Dist = min({Dist, i, H - i - 1, j, W - j - 1});
      }
    }
  }
  cout << (Dist + K - 1) / K + 1 << endl;
}
Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  rep(i, H) cin >> A[i];
  int sh, sw;
  rep(i, H) rep(j, W) if (A[i][j] == 'S') sh = i, sw = j;
  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};
  queue<Pii> que;
  que.push(Pii(sh, sw));
  vector<vector<int>> d(H, vector<int>(W, -1));
  d[sh][sw] = 0;
  int ans = INF;
  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    int h = p.first, w = p.second;
    int x = (d[h][w] + min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    if (d[h][w] < K)
      x = 1 + (min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    chmin(ans, x);
    rep(j, 4) {
      int nh = h + dh[j], nw = w + dw[j];
      if (nh < 0 || nh >= W || nw < 0 || nw >= W)
        continue;
      if (A[nh][nw] == '#')
        continue;
      if (d[nh][nw] != -1)
        continue;
      d[nh][nw] = d[h][w] + 1;
      que.push(Pii(nh, nw));
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  rep(i, H) cin >> A[i];
  int sh, sw;
  rep(i, H) rep(j, W) if (A[i][j] == 'S') sh = i, sw = j;
  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};
  queue<Pii> que;
  que.push(Pii(sh, sw));
  vector<vector<int>> d(H, vector<int>(W, -1));
  d[sh][sw] = 0;
  int ans = INF;
  while (!que.empty()) {
    Pii p = que.front();
    que.pop();
    int h = p.first, w = p.second;
    int x = (d[h][w] + min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    if (d[h][w] < K)
      x = 1 + (min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    chmin(ans, x);
    rep(j, 4) {
      int nh = h + dh[j], nw = w + dw[j];
      if (nh < 0 || nh >= H || nw < 0 || nw >= W)
        continue;
      if (A[nh][nw] == '#')
        continue;
      if (d[nh][nw] != -1)
        continue;
      d[nh][nw] = d[h][w] + 1;
      que.push(Pii(nh, nw));
    }
  }
  cout << ans << endl;
}
Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int H, W, K, S_h, S_w;
string A[800];
int is_first[800][800] = {};

int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};

void go(int h, int w, int k) {
  queue<pair<pair<int, int>, int>> Q;
  Q.push(make_pair(make_pair(h, w), k));
  while (!Q.empty()) {
    pair<pair<int, int>, int> now = Q.front();
    Q.pop();
    int h = now.first.first;
    int w = now.first.second;
    int k = now.second;
    is_first[h][w] = k;
    if (k > 1) {
      for (int i = 0; i < 4; i++) {
        int h_ = h + dh[i];
        int w_ = w + dw[i];
        if ((0 <= h_) && (h_ < H) && (0 <= w_) && (w_ < W) &&
            (A[h_][w_] == '.') && (is_first[h_][w_] == 0)) {
          Q.push(make_pair(make_pair(h_, w_), k - 1));
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> K;
  for (int h = 0; h < H; h++) {
    cin >> A[h];
    for (int w = 0; w < W; w++) {
      if (A[h][w] == 'S') {
        S_h = h;
        S_w = w;
      }
    }
  }
  go(S_h, S_w, K + 1);

  int ans = 1000;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (is_first[h][w]) {
        int dist = min(h, min(H - 1 - h, min(w, W - 1 - w)));
        ans = min(ans, 1 + (dist + K - 1) / K);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int H, W, K, S_h, S_w;
string A[800];
int is_first[800][800] = {};

int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};

void go(int h, int w, int k) {
  queue<pair<pair<int, int>, int>> Q;
  Q.push(make_pair(make_pair(h, w), k));
  while (!Q.empty()) {
    pair<pair<int, int>, int> now = Q.front();
    Q.pop();
    int h = now.first.first;
    int w = now.first.second;
    int k = now.second;
    is_first[h][w] = k;
    if (k > 1) {
      for (int i = 0; i < 4; i++) {
        int h_ = h + dh[i];
        int w_ = w + dw[i];
        if ((0 <= h_) && (h_ < H) && (0 <= w_) && (w_ < W) &&
            (A[h_][w_] == '.') && (is_first[h_][w_] == 0)) {
          is_first[h_][w_] = k - 1;
          Q.push(make_pair(make_pair(h_, w_), k - 1));
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> K;
  for (int h = 0; h < H; h++) {
    cin >> A[h];
    for (int w = 0; w < W; w++) {
      if (A[h][w] == 'S') {
        S_h = h;
        S_w = w;
      }
    }
  }
  go(S_h, S_w, K + 1);

  int ans = 1000;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (is_first[h][w]) {
        int dist = min(h, min(H - 1 - h, min(w, W - 1 - w)));
        ans = min(ans, 1 + (dist + K - 1) / K);
      }
    }
  }
  cout << ans << endl;
}
Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char a[801][801];
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  vector<vector<int>> tesu(h, vector<int>(w, -1));
  queue<pair<int, int>> qu;
  qu.push(make_pair(sx, sy));
  tesu[sx][sy] = 0;
  int ans = (min(min(sx, h - 1 - sx), min(sy, w - 1 - sy)) - 1) / k + 2;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!qu.empty()) {
    int x = (qu.front()).first;
    int y = (qu.front()).second;
    qu.pop();
    if (tesu[x][y] >= k)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (tesu[nx][ny] >= 0 || a[nx][ny] == '#')
        continue;
      tesu[nx][ny] = tesu[x][y] + 1;
      if (nx == 0 || nx == h - 1 || ny == 0 || ny == w - 1)
        ans = 1;
      else
        ans = min(ans,
                  (min(min(nx, h - 1 - nx), min(ny, w - 1 - ny)) - 1) / k + 2);
      qu.push(make_pair(nx, ny));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char a[801][801];
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  vector<vector<int>> tesu(h, vector<int>(w, -1));
  queue<pair<int, int>> qu;
  qu.push(make_pair(sx, sy));
  tesu[sx][sy] = 0;
  int ans = (min(min(sx, h - 1 - sx), min(sy, w - 1 - sy)) - 1) / k + 2;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!qu.empty()) {
    int x = (qu.front()).first;
    int y = (qu.front()).second;
    qu.pop();
    if (tesu[x][y] >= k)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w || tesu[nx][ny] >= 0 ||
          a[nx][ny] == '#')
        continue;
      tesu[nx][ny] = tesu[x][y] + 1;
      if (nx == 0 || nx == h - 1 || ny == 0 || ny == w - 1)
        ans = 1;
      else
        ans = min(ans,
                  (min(min(nx, h - 1 - nx), min(ny, w - 1 - ny)) - 1) / k + 2);
      qu.push(make_pair(nx, ny));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int h, w, k, cx, cy, u, d, l, r;
char map[876][876];
bool bm[876][876];
queue<pair<pair<int, int>, int>> q;
signed main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        cx = i;
        cy = j;
      }
    }
  }
  u = h;
  d = h;
  l = w;
  r = w;
  q.push(make_pair(make_pair(cx, cy), 0));
  while (q.size()) {
    int x = q.front().first.first, y = q.front().first.second,
        p = q.front().second;
    bm[x][y] = 1;
    u = min(u, (x - 1 + k - 1) / k);
    d = min(d, (h - x + k - 1) / k);
    l = min(l, (y - 1 + k - 1) / k);
    r = min(r, (w - y + k - 1) / k);
    if (p < k) {
      if (map[x - 1][y] == '.' && !bm[x - 1][y])
        q.push(make_pair(make_pair(x - 1, y), p + 1));
      if (map[x + 1][y] == '.' && !bm[x + 1][y])
        q.push(make_pair(make_pair(x + 1, y), p + 1));
      if (map[x][y - 1] == '.' && !bm[x][y - 1])
        q.push(make_pair(make_pair(x, y - 1), p + 1));
      if (map[x][y + 1] == '.' && !bm[x][y + 1])
        q.push(make_pair(make_pair(x, y + 1), p + 1));
    }
    q.pop();
  }
  cout << min(min(u, d), min(l, r)) + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int h, w, k, cx, cy, u, d, l, r;
char map[876][876];
bool bm[876][876];
queue<pair<pair<int, int>, int>> q;
signed main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        cx = i;
        cy = j;
      }
    }
  }
  u = h;
  d = h;
  l = w;
  r = w;
  q.push(make_pair(make_pair(cx, cy), 0));
  while (q.size()) {
    int x = q.front().first.first, y = q.front().first.second,
        p = q.front().second;
    if (!bm[x][y]) {
      bm[x][y] = 1;
      u = min(u, (x - 1 + k - 1) / k);
      d = min(d, (h - x + k - 1) / k);
      l = min(l, (y - 1 + k - 1) / k);
      r = min(r, (w - y + k - 1) / k);
      if (p < k) {
        if (map[x - 1][y] == '.' && !bm[x - 1][y])
          q.push(make_pair(make_pair(x - 1, y), p + 1));
        if (map[x + 1][y] == '.' && !bm[x + 1][y])
          q.push(make_pair(make_pair(x + 1, y), p + 1));
        if (map[x][y - 1] == '.' && !bm[x][y - 1])
          q.push(make_pair(make_pair(x, y - 1), p + 1));
        if (map[x][y + 1] == '.' && !bm[x][y + 1])
          q.push(make_pair(make_pair(x, y + 1), p + 1));
      }
    }
    q.pop();
  }
  cout << min(min(u, d), min(l, r)) + 1 << endl;
}
Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 808
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
int ans, n, m, K, sx, sy;
char g[MAX][MAX];
int Calc(int x, int y) { return min(min(x - 1, n - x), min(y - 1, m - y)); }
int dis[MAX][MAX];
int d[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void BFS(int sx, int sy) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      dis[i][j] = 1e9;
  dis[sx][sy] = 0;
  ans = 1e9;
  queue<int> Qx, Qy;
  Qx.push(sx), Qy.push(sy);
  while (!Qx.empty()) {
    int x = Qx.front(), y = Qy.front();
    Qx.pop();
    Qy.pop();
    ans = min(ans, (Calc(x, y) + K - 1) / K);
    if (dis[x][y] >= K)
      continue;
    for (int i = 0; i < 4; ++i) {
      int xx = x + d[i][0], yy = y + d[i][1];
      if (xx < 1 || xx > n || yy < 1 || yy > m)
        continue;
      if (g[xx][yy] == '#')
        continue;
      dis[xx][yy] = dis[x][y] + 1;
      Qx.push(xx);
      Qy.push(yy);
    }
  }
}
int main() {
  n = read();
  m = read();
  K = read();
  for (int i = 1; i <= n; ++i)
    scanf("%s", g[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (g[i][j] == 'S')
        sx = i, sy = j;
  BFS(sx, sy);
  printf("%d\n", ans + 1);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 808
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
int ans, n, m, K, sx, sy;
char g[MAX][MAX];
int Calc(int x, int y) { return min(min(x - 1, n - x), min(y - 1, m - y)); }
int dis[MAX][MAX];
int d[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void BFS(int sx, int sy) {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      dis[i][j] = 1e9;
  dis[sx][sy] = 0;
  ans = 1e9;
  queue<int> Qx, Qy;
  Qx.push(sx), Qy.push(sy);
  while (!Qx.empty()) {
    int x = Qx.front(), y = Qy.front();
    Qx.pop();
    Qy.pop();
    ans = min(ans, (Calc(x, y) + K - 1) / K);
    if (dis[x][y] >= K)
      continue;
    for (int i = 0; i < 4; ++i) {
      int xx = x + d[i][0], yy = y + d[i][1];
      if (xx < 1 || xx > n || yy < 1 || yy > m)
        continue;
      if (g[xx][yy] == '#')
        continue;
      if (dis[xx][yy] <= dis[x][y] + 1)
        continue;
      dis[xx][yy] = dis[x][y] + 1;
      Qx.push(xx);
      Qy.push(yy);
    }
  }
}
int main() {
  n = read();
  m = read();
  K = read();
  for (int i = 1; i <= n; ++i)
    scanf("%s", g[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (g[i][j] == 'S')
        sx = i, sy = j;
  BFS(sx, sy);
  printf("%d\n", ans + 1);
  return 0;
}
Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>
using namespace std;
typedef pair<int, int> P;

const int INF = 10000000;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int d[803][803];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string a[803];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  }
  queue<P> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S')
        que.push(P(i, j));
      else
        d[i][j] = -1;
    }
  }
  int ans = INF;
  while (que.size()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int u = x + dx[i], v = y + dy[i];
      if (u >= 0 && u < h && v >= 0 && v < w && a[u][v] != '#' &&
          d[u][v] == -1) {
        d[u][v] = d[x][y] + 1;
        que.push(P(u, v));
      }
    }
    if (d[x][y] <= k) {
      ans = min(ans, min(min(x + k - 1, y + k - 1),
                         min(h - x + k - 2, w - y + k - 2)) /
                             k +
                         1);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <string>
using namespace std;
typedef pair<int, int> P;

const int INF = 10000000;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int d[803][803];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string a[803];
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }
  queue<P> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S')
        que.push(P(i, j));
      else
        d[i][j] = -1;
    }
  }
  int ans = INF;
  while (que.size()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    for (int i = 0; i < 4; i++) {
      int u = x + dx[i], v = y + dy[i];
      if (u >= 0 && u < h && v >= 0 && v < w && a[u][v] != '#' &&
          d[u][v] == -1) {
        d[u][v] = d[x][y] + 1;
        que.push(P(u, v));
      }
    }
    if (d[x][y] <= k) {
      ans = min(ans, min(min(x + k - 1, y + k - 1),
                         min(h - x + k - 2, w - y + k - 2)) /
                             k +
                         1);
    }
  }
  cout << ans << endl;
}
Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int d[803][803];
char a[803][803];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S')
        sx = i, sy = j;
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      d[i][j] = -1;
  queue<P> que;
  d[sx][sy] = 0;
  que.push(P(sx, sy));
  while (que.size()) {
    P now = que.front();
    int x = now.first, y = now.second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 && y + dy[y] < w &&
          d[x + dx[i]][y + dy[i]] == -1 && a[x + dx[i]][y + dy[i]] == '.' &&
          d[x][y] < k) {
        d[x + dx[i]][y + dy[i]] = d[x][y] + 1;
        que.push(P(x + dx[i], y + dy[i]));
      }
    }
  }
  int les = 1000000;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (d[i][j] != -1)
        les = min(les, min(min(i, j), min(h - 1 - i, w - 1 - j)));
  cout << (les + k - 1) / k + 1 << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int d[803][803];
char a[803][803];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S')
        sx = i, sy = j;
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      d[i][j] = -1;
  queue<P> que;
  d[sx][sy] = 0;
  que.push(P(sx, sy));
  while (que.size()) {
    P now = que.front();
    int x = now.first, y = now.second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 && y + dy[i] < w &&
          d[x + dx[i]][y + dy[i]] == -1 && a[x + dx[i]][y + dy[i]] == '.' &&
          d[x][y] < k) {
        d[x + dx[i]][y + dy[i]] = d[x][y] + 1;
        que.push(P(x + dx[i], y + dy[i]));
      }
    }
  }
  int les = 1000000;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (d[i][j] != -1)
        les = min(les, min(min(i, j), min(h - 1 - i, w - 1 - j)));
  cout << (les + k - 1) / k + 1 << endl;
}
Problem p03725 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
const long double pi = 3.14159265358979323846264338327950288;
char a[800][800];
const ll inf = 99999999999999;
int b[800][800];
struct Q {
  ll x, y;
};
int u1[4] = {0, 0, -1, 1}, u2[4] = {-1, 1, 0, 0};
signed main() {
  ll h, w, x, y, k;
  cin >> h >> w >> k;
  for (ll i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (ll j = 0; j < w; j++) {
      a[i][j] = s[j];
      b[i][j] = inf;
      if (s[j] == 'S')
        x = j, y = i;
    }
  }
  queue<Q> q;
  q.push(Q{x, y});
  b[y][x] = 0;
  ll ans = inf;
  while (!q.empty()) {
    Q t = q.front();
    q.pop();
    if (t.y == 0 || t.y == h - 1 || t.x == 0 || t.x == w - 1)
      ans = 1;
    ans = min(ans, (ll)ceil(min(t.x, min(w - 1 - t.x, min(h - 1 - t.y, t.y))) *
                            1.0 / k) +
                       1);
    for (int i = 0; i < 4; i++) {
      ll x = t.x + u1[i];
      ll y = t.y + u2[i];
      if (x < w && x >= 0 && y < h && w >= 0 && a[y][x] == '.' &&
          b[t.y][t.x] + 1 < b[y][x] && b[t.y][t.x] + 1 <= k) {
        q.push(Q{x, y});
        b[y][x] = b[t.y][t.x] + 1;
      }
    }
  }
  cout << ans << endl;
  cin >> h;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
const long double pi = 3.14159265358979323846264338327950288;
char a[800][800];
const ll inf = 99999999999999;
int b[800][800];
struct Q {
  ll x, y;
};
int u1[4] = {0, 0, -1, 1}, u2[4] = {-1, 1, 0, 0};
signed main() {
  ll h, w, x, y, k;
  cin >> h >> w >> k;
  for (ll i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (ll j = 0; j < w; j++) {
      a[i][j] = s[j];
      b[i][j] = inf;
      if (s[j] == 'S')
        x = j, y = i;
    }
  }
  queue<Q> q;
  q.push(Q{x, y});
  b[y][x] = 0;
  ll ans = inf;
  while (!q.empty()) {
    Q t = q.front();
    q.pop();
    if (t.y == 0 || t.y == h - 1 || t.x == 0 || t.x == w - 1)
      ans = 1;
    ans = min(ans, (ll)ceil(min(t.x, min(w - 1 - t.x, min(h - 1 - t.y, t.y))) *
                            1.0 / k) +
                       1);
    for (int i = 0; i < 4; i++) {
      ll x = t.x + u1[i];
      ll y = t.y + u2[i];
      if (x < w && x >= 0 && y < h && y >= 0 && a[y][x] == '.' &&
          b[t.y][t.x] + 1 < b[y][x] && b[t.y][t.x] + 1 <= k) {
        q.push(Q{x, y});
        b[y][x] = b[t.y][t.x] + 1;
      }
    }
  }
  cout << ans << endl;
  cin >> h;
}
Problem p03725 (C++)

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  int sx, sy;
  vector<vector<char>> a(h, vector<char>(w));
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];

      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }

  queue<pair<pair<int, int>, int>> q;
  q.push({{sx, sy}, k});
  visited[sx][sy] = true;

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  int ans = 10000;
  while (q.empty() == false) {
    int x = q.front().first.first;
    int y = q.front().first.second;
    int t = q.front().second;
    q.pop();

    ans = min(ans, (min({x, y, (h - 1 - x), (w - 1 - y)}) + (k - 1)) / k);

    for (int i = 0; i < 4; i++) {
      if (0 <= x + dx[i] && x + dx[i] < h && 0 <= y + dy[i] && y + dy[i] < w &&
          visited[x + dx[i]][y + dy[i]] == false &&
          a[x + dx[i]][y + dy[i]] != '#' && t - 1 >= 0) {
        visited[x + dx[i]][y + dy[i]] == true;
        q.push({{x + dx[i], y + dy[i]}, t - 1});
      }
    }
  }
  cout << ans + 1 << endl;
  return 0;
}

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  int sx, sy;
  vector<vector<char>> a(h, vector<char>(w));
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];

      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }

  queue<pair<pair<int, int>, int>> q;
  q.push({{sx, sy}, k});
  visited[sx][sy] = true;

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  int ans = 10000;
  while (q.empty() == false) {
    int x = q.front().first.first;
    int y = q.front().first.second;
    int t = q.front().second;
    q.pop();

    ans = min(ans, (min({x, y, (h - 1 - x), (w - 1 - y)}) + (k - 1)) / k);

    for (int i = 0; i < 4; i++) {
      if (0 <= x + dx[i] && x + dx[i] < h && 0 <= y + dy[i] && y + dy[i] < w &&
          visited[x + dx[i]][y + dy[i]] == false &&
          a[x + dx[i]][y + dy[i]] != '#' && t - 1 >= 0) {
        visited[x + dx[i]][y + dy[i]] = true;
        q.push({{x + dx[i], y + dy[i]}, t - 1});
      }
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p03726 (C++)

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

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

const int N = 800;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);

vector<int> g[N];

int dfs(int u, int par) {
  int cnt = 0;
  for (int v : g[u])
    if (v != par)
      if (dfs(v, u) == 1)
        cnt++;
  if (cnt >= 2 || cnt == 0 && par == -1) {
    cout << "First\n";
    exit(0);
  }
  if (cnt == 0)
    return 1;
  else
    return 0;
}

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  // ll TL = 0.95 * CLOCKS_PER_SEC;
  // clock_t time = clock();
  int n;
  cin >> n;
  rep(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  cout << "Second\n";
  return 0;
}


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

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)

using namespace std;

const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);

vector<int> g[N];

int dfs(int u, int par) {
  int cnt = 0;
  for (int v : g[u])
    if (v != par)
      if (dfs(v, u) == 1)
        cnt++;
  if (cnt >= 2 || cnt == 0 && par == -1) {
    cout << "First\n";
    exit(0);
  }
  if (cnt == 0)
    return 1;
  else
    return 0;
}

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  // ll TL = 0.95 * CLOCKS_PER_SEC;
  // clock_t time = clock();
  int n;
  cin >> n;
  rep(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  cout << "Second\n";
  return 0;
}
Problem p03726 (C++)

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

using namespace std;

typedef long long ll;

const int maxn = 800 + 50;
const int mod = 1e9 + 7;

vector<int> t[maxn];
int maxmatch;

bool dfs(int v, int par = -1) {
  bool flag = 0;
  for (auto u : t[v]) {
    if (u == par)
      continue;
    bool me = dfs(u, v);
    if (me == 1)
      continue;
    if (flag == 0) {
      flag = 1;
      maxmatch += 2;
    }
  }
  return flag;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    t[v].push_back(u);
    t[u].push_back(v);
  }
  dfs(0);
  if (maxmatch == n)
    cout << "Second\n";
  else
    cout << "First\n";
}


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

using namespace std;

typedef long long ll;

const int maxn = 100000 + 50;
const int mod = 1e9 + 7;

vector<int> t[maxn];
int maxmatch;

bool dfs(int v, int par = -1) {
  bool flag = 0;
  for (auto u : t[v]) {
    if (u == par)
      continue;
    bool me = dfs(u, v);
    if (me == 1)
      continue;
    if (flag == 0) {
      flag = 1;
      maxmatch += 2;
    }
  }
  return flag;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    t[v].push_back(u);
    t[u].push_back(v);
  }
  dfs(0);
  if (maxmatch == n)
    cout << "Second\n";
  else
    cout << "First\n";
}