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

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

const int N = 200010;

int left[N], a[N];
int vis[N];
int n, m;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < N; ++i) {
    left[i] = i;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    --left[a[i]];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = std::max(left[i] + 1, 1); j <= i; ++j) {
      ++vis[j];
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    cnt += vis[i] ? 1 : 0;
  }
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ++left[a[x]];
    if (left[a[x]]) {
      cnt -= vis[left[a[x]]] ? 1 : 0;
      --vis[left[a[x]]];
      cnt += vis[left[a[x]]] ? 1 : 0;
    }
    a[x] = y;
    --left[y];
    if (left[y] >= 0) {
      cnt -= vis[left[y] + 1] ? 1 : 0;
      ++vis[left[y] + 1];
      cnt += vis[left[y] + 1] ? 1 : 0;
    }
    printf("%d\n", n - cnt);
  }
  return 0;
}

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

const int N = 200010;

int left[N], a[N];
int vis[N];
int n, m;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < N; ++i) {
    left[i] = i;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    --left[a[i]];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = std::max(left[i] + 1, 1); j <= i; ++j) {
      ++vis[j];
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    cnt += vis[i] ? 1 : 0;
  }
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ++left[a[x]];
    if (left[a[x]] > 0) {
      cnt -= vis[left[a[x]]] ? 1 : 0;
      --vis[left[a[x]]];
      cnt += vis[left[a[x]]] ? 1 : 0;
    }
    a[x] = y;
    --left[y];
    if (left[y] >= 0) {
      cnt -= vis[left[y] + 1] ? 1 : 0;
      ++vis[left[y] + 1];
      cnt += vis[left[y] + 1] ? 1 : 0;
    }
    printf("%d\n", n - cnt);
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int C[101000], S[101000], n, m, w[101000], r;
void Add(int a) {
  int t = max(a - C[a], 1);
  r -= abs(S[t] - 1);
  S[t]++;
  r += abs(S[t] - 1);
  C[a]++;
}
void Del(int a) {
  C[a]--;
  int t = max(a - C[a], 1);
  r -= abs(S[t] - 1);
  S[t]--;
  r += abs(S[t] - 1);
}
int main() {
  int i, a, b;
  scanf("%d%d", &n, &m);
  r = n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    Add(w[i]);
  }
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    Del(w[a]);
    w[a] = b;
    Add(w[a]);
    printf("%d\n", r / 2);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int C[201000], S[201000], n, m, w[201000], r;
void Add(int a) {
  int t = max(a - C[a], 1);
  r -= abs(S[t] - 1);
  S[t]++;
  r += abs(S[t] - 1);
  C[a]++;
}
void Del(int a) {
  C[a]--;
  int t = max(a - C[a], 1);
  r -= abs(S[t] - 1);
  S[t]--;
  r += abs(S[t] - 1);
}
int main() {
  int i, a, b;
  scanf("%d%d", &n, &m);
  r = n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    Add(w[i]);
  }
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    Del(w[a]);
    w[a] = b;
    Add(w[a]);
    printf("%d\n", r / 2);
  }
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int n, m, a[maxn + 10], cnt[maxn + 10], cov[maxn + 10], ans;

void modify(int p, int v) {
  if (p >= 1) {
    ans -= !!cov[p];
    ans += !!(cov[p] += v);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    modify(a[i] - (cnt[a[i]]++), 1);
  }
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    modify(a[x] - (--cnt[a[x]]), -1);
    a[x] = y;
    modify(y - (cnt[y]++), 1);
    printf("%d\n", n - ans);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int n, m, a[maxn + 10], cnt[maxn + 10], cov[maxn + 10], ans;

void modify(int p, int v) {
  if (p >= 1) {
    ans -= !!cov[p];
    ans += !!(cov[p] += v);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    modify(a[i] - (cnt[a[i]]++), 1);
  }
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    modify(a[x] - (--cnt[a[x]]), -1);
    a[x] = y;
    modify(y - (cnt[y]++), 1);
    printf("%d\n", n - ans);
  }
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

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

const int MX = 1 << 18;

struct Tree {
  int t[MX * 2];
  void update(int x, int v) {
    x += MX;
    t[x] += v;
    while (x > 1)
      x /= 2, t[x] = max(t[x * 2], 0) + max(t[x * 2 + 1], 0);
  }
  int read() { return t[1]; }
} tree;

int cnt[MX], D[MX];

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++)
    scanf("%d", D + i), cnt[D[i]]++;
  for (int i = N + 1; i <= 2 * N; i++)
    tree.update(i, -1);
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < cnt[i]; j++)
      tree.update(i - j + N, 1);
  }
  for (int i = 1; i <= M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    int u = D[a], v = b;
    if (u != v) {
      D[a] = b;
      cnt[u]--;
      cnt[v]++;
      tree.update(u - cnt[u] + N, -1);
      tree.update(v - cnt[v] + 1 + N, 1);
    }

    printf("%d\n", tree.read());
  }
}


Fixed submission:
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

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

const int MX = 1 << 19;

struct Tree {
  int t[MX * 2];
  void update(int x, int v) {
    x += MX;
    t[x] += v;
    while (x > 1)
      x /= 2, t[x] = max(t[x * 2], 0) + max(t[x * 2 + 1], 0);
  }
  int read() { return t[1]; }
} tree;

int cnt[MX], D[MX];

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++)
    scanf("%d", D + i), cnt[D[i]]++;
  for (int i = N + 1; i <= 2 * N; i++)
    tree.update(i, -1);
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < cnt[i]; j++)
      tree.update(i - j + N, 1);
  }
  for (int i = 1; i <= M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    int u = D[a], v = b;
    if (u != v) {
      D[a] = b;
      cnt[u]--;
      cnt[v]++;
      tree.update(u - cnt[u] + N, -1);
      tree.update(v - cnt[v] + 1 + N, 1);
    }

    printf("%d\n", tree.read());
  }
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 100005;

int n, T, ans, x, w, y;
int cnt[N], a[N], b[N];

int main() {
  int i, j;
  scanf("%d%d", &n, &T);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), cnt[a[i]]++;
  for (i = 1; i <= n; i++) {
    for (j = max(0, i - cnt[i]); j < i; j++)
      b[j]++;
  }
  for (i = 0; i < n; i++)
    if (!b[i])
      ans++;
  while (T--) {
    scanf("%d%d", &x, &w);
    y = a[x] - cnt[a[x]];
    if (y >= 0) {
      b[y]--;
      if (!b[y])
        ans++;
    }
    cnt[a[x]]--;
    a[x] = w;
    cnt[a[x]]++;
    y = a[x] - cnt[a[x]];
    if (y >= 0) {
      if (!b[y])
        ans--;
      b[y]++;
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 200005;

int n, T, ans, x, w, y;
int cnt[N], a[N], b[N];

int main() {
  int i, j;
  scanf("%d%d", &n, &T);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), cnt[a[i]]++;
  for (i = 1; i <= n; i++) {
    for (j = max(0, i - cnt[i]); j < i; j++)
      b[j]++;
  }
  for (i = 0; i < n; i++)
    if (!b[i])
      ans++;
  while (T--) {
    scanf("%d%d", &x, &w);
    y = a[x] - cnt[a[x]];
    if (y >= 0) {
      b[y]--;
      if (!b[y])
        ans++;
    }
    cnt[a[x]]--;
    a[x] = w;
    cnt[a[x]]++;
    y = a[x] - cnt[a[x]];
    if (y >= 0) {
      if (!b[y])
        ans--;
      b[y]++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03667 (C++)

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

const int N = 2e5 + 7;

int a[N];
int cnt[N];
int c[N];
int ans;
int n, m;

void relax(int x, int val) {
  if (x >= 0)
    if (c[x] == 0)
      ans--;
  c[x] += val;
  if (c[x] == 0)
    ans++;
}
void modif(int x, int val) {
  if (x > n)
    return;
  if (val == -1)
    relax(x - (cnt[x]--), val);
  else
    relax(x - (++cnt[x]), val);
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> m;
  ans = n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    modif(x, 1);
    a[i] = x;
  }
  for (int i = 0; i < m; i++) {
    int ind, val;
    cin >> ind >> val;
    ind--;
    modif(a[ind], -1);
    a[ind] = val;
    modif(a[ind], 1);
    cout << ans << "\n";
  }

  return 0;
}


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

const int N = 2e5 + 7;

int a[N];
int cnt[N];
int c[N];
int ans;
int n, m;

void relax(int x, int val) {
  if (x < 0)
    return;
  if (c[x] == 0)
    ans--;
  c[x] += val;
  if (c[x] == 0)
    ans++;
}
void modif(int x, int val) {
  if (x > n)
    return;
  if (val == -1)
    relax(x - (cnt[x]--), val);
  else
    relax(x - (++cnt[x]), val);
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> m;
  ans = n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    modif(x, 1);
    a[i] = x;
  }
  for (int i = 0; i < m; i++) {
    int ind, val;
    cin >> ind >> val;
    ind--;
    modif(a[ind], -1);
    a[ind] = val;
    modif(a[ind], 1);
    cout << ans << "\n";
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gec getchar
#define puc putchar
#define File(S) (freopen(S ".in", "r", stdin), freopen(S ".out", "w", stdout))
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &a) {
  static char ch;
  static bool sign;
  for (ch = gec(), sign = 0; ch < '0' || ch > '9'; ch = gec())
    sign ^= (ch == '-');
  for (a = 0; ch >= '0' && ch <= '9'; ch = gec())
    a = ((a + (a << 2)) << 1) + (ch ^ '0');
  if (sign)
    a = -a;
}
template <typename T> inline void print(T x) {
  static int Buf[30], PS = 0, BUF;
  if (x < 0)
    puc('-'), x = -x;
  for (; x >= 10; x = BUF) {
    BUF = (x / 5) >> 1;
    Buf[++PS] = x - ((BUF + (BUF << 2)) << 1);
  }
  Buf[++PS] = x;
  while (PS)
    puc('0' ^ Buf[PS--]);
}
const int N = 1e5 + 10;
int cnt[N], COUNT[2 * N], a[N];
int *Count = COUNT + N;
int n, Sum, x, y, m;
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(a[i]), cnt[a[i]]++;
  for (int i = 1; i <= n; i++)
    for (int j = cnt[i]; j; j--)
      Count[i - j + 1]++;
  for (int i = 1; i <= n; i++)
    Sum += (bool)Count[i];
  while (m--) {
    read(x);
    read(y);
    Sum -= (--Count[a[x] - cnt[a[x]] + 1] || (a[x] - cnt[a[x]] < 0)) ? 0 : 1;
    cnt[a[x]]--;
    a[x] = y;
    cnt[a[x]]++;
    Sum += (Count[a[x] - cnt[a[x]] + 1]++ || (a[x] - cnt[a[x]] < 0)) ? 0 : 1;
    printf("%d\n", n - Sum);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gec getchar
#define puc putchar
#define File(S) (freopen(S ".in", "r", stdin), freopen(S ".out", "w", stdout))
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &a) {
  static char ch;
  static bool sign;
  for (ch = gec(), sign = 0; ch < '0' || ch > '9'; ch = gec())
    sign ^= (ch == '-');
  for (a = 0; ch >= '0' && ch <= '9'; ch = gec())
    a = ((a + (a << 2)) << 1) + (ch ^ '0');
  if (sign)
    a = -a;
}
template <typename T> inline void print(T x) {
  static int Buf[30], PS = 0, BUF;
  if (x < 0)
    puc('-'), x = -x;
  for (; x >= 10; x = BUF) {
    BUF = (x / 5) >> 1;
    Buf[++PS] = x - ((BUF + (BUF << 2)) << 1);
  }
  Buf[++PS] = x;
  while (PS)
    puc('0' ^ Buf[PS--]);
}
const int N = 2e5 + 10;
int cnt[N], COUNT[2 * N], a[N];
int *Count = COUNT + N;
int n, Sum, x, y, m;
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++)
    read(a[i]), cnt[a[i]]++;
  for (int i = 1; i <= n; i++)
    for (int j = cnt[i]; j; j--)
      Count[i - j + 1]++;
  for (int i = 1; i <= n; i++)
    Sum += (bool)Count[i];
  while (m--) {
    read(x);
    read(y);
    Sum -= (--Count[a[x] - cnt[a[x]] + 1] || (a[x] - cnt[a[x]] < 0)) ? 0 : 1;
    cnt[a[x]]--;
    a[x] = y;
    cnt[a[x]]++;
    Sum += (Count[a[x] - cnt[a[x]] + 1]++ || (a[x] - cnt[a[x]] < 0)) ? 0 : 1;
    printf("%d\n", n - Sum);
  }
  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#define re(i, a, b) for (int i = a; i <= b; i++)
#define _re(i, a, b) for (int i = a; i >= b; i--)
#define for_edge(x) for (int i = head[x]; i; i = e[i].nxt)
#define ll long long
#define maxn 100005
#define inf 1e9
using namespace std;
inline int read() {
  char c = getchar();
  int ans = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
//____________________________________________________________________________________________________________________________________________________________________
int a[maxn], sum[maxn], cnt[maxn], n, m;
int main() {
  n = read();
  m = read();
  re(i, 1, n) cnt[a[i] = read()]++;
  int ans = 0;
  re(i, 1, n) {
    sum[max(i - cnt[i] + 1, 1)]++;
    sum[i + 1]--;
  }
  re(i, 1, n) {
    sum[i] += sum[i - 1];
    if (sum[i])
      ans++;
  }
  re(i, 1, m) {
    int pos = read(), x = a[pos], y = read();
    a[pos] = y;
    if (x - cnt[x] + 1 > 0) {
      sum[x - cnt[x] + 1]--;
      if (!sum[x - cnt[x] + 1])
        ans--;
    }
    cnt[x]--;

    cnt[y]++;
    if (y - cnt[y] + 1 > 0) {
      if (!sum[y - cnt[y] + 1])
        ans++;
      sum[y - cnt[y] + 1]++;
    }
    // re(i,1,n)printf("%d ",sum[i]);putchar('\n');
    printf("%d\n", n - ans);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#define re(i, a, b) for (int i = a; i <= b; i++)
#define _re(i, a, b) for (int i = a; i >= b; i--)
#define for_edge(x) for (int i = head[x]; i; i = e[i].nxt)
#define ll long long
#define maxn 200005
#define inf 1e9
using namespace std;
inline int read() {
  char c = getchar();
  int ans = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
//____________________________________________________________________________________________________________________________________________________________________
int a[maxn], sum[maxn], cnt[maxn], n, m;
int main() {
  n = read();
  m = read();
  re(i, 1, n) cnt[a[i] = read()]++;
  int ans = 0;
  re(i, 1, n) {
    sum[max(i - cnt[i] + 1, 1)]++;
    sum[i + 1]--;
  }
  re(i, 1, n) {
    sum[i] += sum[i - 1];
    if (sum[i])
      ans++;
  }
  re(i, 1, m) {
    int pos = read(), x = a[pos], y = read();
    a[pos] = y;
    if (x - cnt[x] + 1 > 0) {
      sum[x - cnt[x] + 1]--;
      if (!sum[x - cnt[x] + 1])
        ans--;
    }
    cnt[x]--;

    cnt[y]++;
    if (y - cnt[y] + 1 > 0) {
      if (!sum[y - cnt[y] + 1])
        ans++;
      sum[y - cnt[y] + 1]++;
    }
    // re(i,1,n)printf("%d ",sum[i]);putchar('\n');
    printf("%d\n", n - ans);
  }

  return 0;
}
Problem p03667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ll, ii>
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

int n, k;
int arr[100005];
int cnt[100005];
int pos[100005];
int off;

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

  cin >> n >> k;

  off = n;

  for (int x = 1; x <= n; x++) {
    cin >> arr[x];
    cnt[arr[x]]++;
    if (arr[x] - cnt[arr[x]] >= 0) {
      pos[arr[x] - cnt[arr[x]]]++;
      if (pos[arr[x] - cnt[arr[x]]] == 1)
        off--;
    }
  }

  int a, b;
  while (k--) {
    cin >> a >> b;
    if (arr[a] - cnt[arr[a]] >= 0) {
      pos[arr[a] - cnt[arr[a]]]--;
      if (pos[arr[a] - cnt[arr[a]]] == 0)
        off++;
    }
    cnt[arr[a]]--;

    arr[a] = b;

    cnt[arr[a]]++;
    if (arr[a] - cnt[arr[a]] >= 0) {
      pos[arr[a] - cnt[arr[a]]]++;
      if (pos[arr[a] - cnt[arr[a]]] == 1)
        off--;
    }

    cout << off << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ll, ii>
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

int n, k;
int arr[200005];
int cnt[200005];
int pos[200005];
int off;

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

  cin >> n >> k;

  off = n;

  for (int x = 1; x <= n; x++) {
    cin >> arr[x];
    cnt[arr[x]]++;
    if (arr[x] - cnt[arr[x]] >= 0) {
      pos[arr[x] - cnt[arr[x]]]++;
      if (pos[arr[x] - cnt[arr[x]]] == 1)
        off--;
    }
  }

  int a, b;
  while (k--) {
    cin >> a >> b;
    if (arr[a] - cnt[arr[a]] >= 0) {
      pos[arr[a] - cnt[arr[a]]]--;
      if (pos[arr[a] - cnt[arr[a]]] == 0)
        off++;
    }
    cnt[arr[a]]--;

    arr[a] = b;

    cnt[arr[a]]++;
    if (arr[a] - cnt[arr[a]] >= 0) {
      pos[arr[a] - cnt[arr[a]]]++;
      if (pos[arr[a] - cnt[arr[a]]] == 1)
        off--;
    }

    cout << off << endl;
  }
}
Problem p03668 (C++)

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

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;
const int N = 100010;
struct Edge {
  int to, next;
} edge[N];
int head[N], num;
void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; }
int dfs(int x, int f) {
  int ret = 0;
  RepG(i, x) if (v != f) ret ^= (dfs(v, x) + 1);
  return ret;
}

int main() {
  int n;
  scanf("%d", &n);
  Rep(i, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    add_edge(x, y);
    add_edge(y, x);
  }

  if (dfs(1, 0))
    printf("Alice\n");
  else
    printf("Bob\n");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <vector>

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;
const int N = 100010;
struct Edge {
  int to, next;
} edge[N * 2];
int head[N], num;
void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; }
int dfs(int x, int f) {
  int ret = 0;
  RepG(i, x) if (v != f) ret ^= (dfs(v, x) + 1);
  return ret;
}

int main() {
  int n;
  scanf("%d", &n);
  Rep(i, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    add_edge(x, y);
    add_edge(y, x);
  }

  if (dfs(1, 0))
    printf("Alice\n");
  else
    printf("Bob\n");

  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define inf 0x3f3f3f3f
#define esp 1e-7
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

struct Tree {
  int sg;
  int visited;
} t[100050];
int n;
vector<int> to[10050];

void dfs(int index) {
  for (unsigned int i = 0; i < to[index].size(); i++) {
    if (t[to[index][i]].visited == 0) {
      t[to[index][i]].visited = 1;
      dfs(to[index][i]);
      t[index].sg ^= (t[to[index][i]].sg + 1);
    }
  }
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    to[i].clear();
  for (int i = 1, tmp1, tmp2; i < n; i++) {
    tmp1 = read();
    tmp2 = read();
    to[tmp1].push_back(tmp2);
    to[tmp2].push_back(tmp1);
  }
  t[1].visited = 1;
  dfs(1);
  if (t[1].sg == 0)
    cout << "Bob" << endl;
  else
    cout << "Alice" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define inf 0x3f3f3f3f
#define esp 1e-7
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

struct Tree {
  int sg;
  int visited;
} t[100050];
int n;
vector<int> to[100050];

void dfs(int index) {
  for (unsigned int i = 0; i < to[index].size(); i++) {
    if (t[to[index][i]].visited == 0) {
      t[to[index][i]].visited = 1;
      dfs(to[index][i]);
      t[index].sg ^= (t[to[index][i]].sg + 1);
    }
  }
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    to[i].clear();
  for (int i = 1, tmp1, tmp2; i < n; i++) {
    tmp1 = read();
    tmp2 = read();
    to[tmp1].push_back(tmp2);
    to[tmp2].push_back(tmp1);
  }
  t[1].visited = 1;
  dfs(1);
  if (t[1].sg == 0)
    cout << "Bob" << endl;
  else
    cout << "Alice" << endl;
  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100000;

struct EDGE {
  int from, to, next;
} e[N * 2];

int n, cnt;
int head[N], f[N];

void addedge(int u, int v) {
  e[++cnt].from = u;
  e[cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}

void dfs(int x, int fa) {
  f[x] = 0;
  for (int i = head[x]; i; i = e[i].next) {
    if (e[i].to == fa)
      continue;
    dfs(e[i].to, x);
    f[x] ^= f[e[i].to] + 1;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  if (f[1] == 0)
    puts("Bob");
  else
    puts("Alice");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100005;

struct EDGE {
  int from, to, next;
} e[N * 2];

int n, cnt;
int head[N], f[N];

void addedge(int u, int v) {
  e[++cnt].from = u;
  e[cnt].to = v;
  e[cnt].next = head[u];
  head[u] = cnt;
}

void dfs(int x, int fa) {
  f[x] = 0;
  for (int i = head[x]; i; i = e[i].next) {
    if (e[i].to == fa)
      continue;
    dfs(e[i].to, x);
    f[x] ^= f[e[i].to] + 1;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  if (f[1] == 0)
    puts("Bob");
  else
    puts("Alice");
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
// code for hackerrank problem: deforestation
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define N 1010
int n;

vector<int> v[N];
int f[N];

void dfs(int r, int p) {
  f[r] = 0;
  for (int i = 0; i < (int)v[r].size(); i++) {
    if (p == v[r][i])
      continue;
    dfs(v[r][i], r);
    f[r] ^= f[v[r][i]] + 1;
  }
}

int main() {
  int T = 1;
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      v[i].clear();
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      v[x].push_back(y);
      v[y].push_back(x);
    }
    dfs(1, 0);
    if (f[1])
      puts("Alice");
    else
      puts("Bob");
  }
  return 0;
}


Fixed submission:
// code for hackerrank problem: deforestation
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define N 101010
int n;

vector<int> v[N];
int f[N];

void dfs(int r, int p) {
  f[r] = 0;
  for (int i = 0; i < (int)v[r].size(); i++) {
    if (p == v[r][i])
      continue;
    dfs(v[r][i], r);
    f[r] ^= f[v[r][i]] + 1;
  }
}

int main() {
  int T = 1;
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      v[i].clear();
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      v[x].push_back(y);
      v[y].push_back(x);
    }
    dfs(1, 0);
    if (f[1])
      puts("Alice");
    else
      puts("Bob");
  }
  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
const int MaxN = 1e5;
using namespace std;
int pre[2 * MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int all, n, res[MaxN + 5];
void Build(int u, int v) {
  pre[++all] = last[u];
  last[u] = all;
  other[all] = v;
}
void dfs(int x, int fa) {
  int ed = last[x];
  while (ed != -1) {
    int dr = other[ed];
    if (dr != fa) {
      dfs(dr, x);
      res[x] ^= res[dr] + 1;
    }
    ed = pre[ed];
  }
}
int main() {
  while (~scanf("%d", &n)) {
    all = -1;
    memset(last, -1, sizeof(last));
    memset(res, 0, sizeof(res));
    for (int i = 1; i <= n - 1; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      Build(u, v);
      Build(v, u);
    }
    dfs(1, 0);
    if (res[1])
      printf("Alice\n");
    else
      printf("Bob\n");
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
const int MaxN = 1e6;
using namespace std;
int pre[2 * MaxN + 5], last[MaxN + 5], other[MaxN + 5];
int all, n, res[MaxN + 5];
void Build(int u, int v) {
  pre[++all] = last[u];
  last[u] = all;
  other[all] = v;
}
void dfs(int x, int fa) {
  int ed = last[x];
  while (ed != -1) {
    int dr = other[ed];
    if (dr != fa) {
      dfs(dr, x);
      res[x] ^= res[dr] + 1;
    }
    ed = pre[ed];
  }
}
int main() {
  while (~scanf("%d", &n)) {
    all = -1;
    memset(last, -1, sizeof(last));
    memset(res, 0, sizeof(res));
    for (int i = 1; i <= n - 1; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      Build(u, v);
      Build(v, u);
    }
    dfs(1, 0);
    if (res[1])
      printf("Alice\n");
    else
      printf("Bob\n");
  }
}
Problem p03668 (C++)

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

// I am fucking stupid
// Stupidity level  -> 1/0

const int MAXN = 1010;
vector<int> v1[MAXN];
int gr[MAXN];

void DFS(int x, int par) {
  for (auto itr : v1[x]) {
    if (itr != par) {
      DFS(itr, x);
      gr[x] ^= (gr[itr] + 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < MAXN; i += 1) {
    v1[i].clear();
    gr[i] = 0;
  }
  for (int i = 1; i <= n - 1; i += 1) {
    int x, y;
    cin >> x >> y;
    v1[x].push_back(y);
    v1[y].push_back(x);
  }
  DFS(1, -1);
  if (gr[1] == 0) {
    cout << "Bob" << endl;
  } else {
    cout << "Alice" << endl;
  }

  return 0;
}


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

// I am fucking stupid
// Stupidity level  -> 1/0

const int MAXN = 101010;
vector<int> v1[MAXN];
int gr[MAXN];

void DFS(int x, int par) {
  for (auto itr : v1[x]) {
    if (itr != par) {
      DFS(itr, x);
      gr[x] ^= (gr[itr] + 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < MAXN; i += 1) {
    v1[i].clear();
    gr[i] = 0;
  }
  for (int i = 1; i <= n - 1; i += 1) {
    int x, y;
    cin >> x >> y;
    v1[x].push_back(y);
    v1[y].push_back(x);
  }
  DFS(1, -1);
  if (gr[1] == 0) {
    cout << "Bob" << endl;
  } else {
    cout << "Alice" << endl;
  }

  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define M 55
#define N 1005
#define ll long long
#define oo 1000000000
#define mod 5557
using namespace std;
int n, m, res[M];
vector<int> vec[M];
void dfs(int now, int f) {
  for (int i = 0; i < vec[now].size(); i++) {
    int y = vec[now][i];
    if (y == f)
      continue;
    dfs(y, now);
    res[now] = res[now] ^ (1 + res[y]);
  }
}
int main() {
  int i, j, a, b;
  cin >> n;
  for (i = 1; i < n; i++) {
    scanf("%d %d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  dfs(1, 0);
  if (res[1])
    puts("Alice");
  else
    puts("Bob");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define M 100005
#define N 1005
#define ll long long
#define oo 1000000000
#define mod 5557
using namespace std;
int n, m, res[M];
vector<int> vec[M];
void dfs(int now, int f) {
  for (int i = 0; i < vec[now].size(); i++) {
    int y = vec[now][i];
    if (y == f)
      continue;
    dfs(y, now);
    res[now] = res[now] ^ (1 + res[y]);
  }
}
int main() {
  int i, j, a, b;
  cin >> n;
  for (i = 1; i < n; i++) {
    scanf("%d %d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  dfs(1, 0);
  if (res[1])
    puts("Alice");
  else
    puts("Bob");
  return 0;
}
Problem p03668 (C++)

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

using namespace std;

#define N 1000000000;
#define ll long long
vector<ll> adj[100005];
std::vector<ll> vis;
ll dfs(ll node) {
  if ((node != 1 && adj[node].size() - 1 == 0) ||
      (node == 1 && adj[node].size() == 0))
    return 0;
  if ((node != 1 && adj[node].size() - 1 == 1) ||
      (node == 1 && adj[node].size() == 1)) {
    for (auto j : adj[node]) {
      if (vis[j] == 0) {
        vis[j] + 1;
        return 1 + dfs(j);
      }
    }
  }
  ll ans = 0;
  for (auto j : adj[node]) {
    if (vis[j] == 0) {
      vis[j] = 1;
      ans ^= (1 + dfs(j));
    }
  }
  return ans;
}
int main()

{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    ll n;
    cin >> n;
    for (ll i = 0; i < n - 1; i++) {
      ll a, b;
      cin >> a >> b;
      adj[a].push_back(b);
      adj[b].push_back(a);
    }
    vis = vector<ll>(n + 2, 0);
    vis[1] = 1;
    if (dfs(1))
      cout << "Alice" << endl;
    else
      cout << "Bob" << endl;
  }
  return 0;
}

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

using namespace std;

#define N 1000000000;
#define ll long long
vector<ll> adj[100005];
std::vector<ll> vis;
ll dfs(ll node) {

  ll ans = 0;
  for (auto j : adj[node]) {
    if (vis[j] == 0) {
      vis[j] = 1;
      ans ^= (1 + dfs(j));
    }
  }
  return ans;
}
int main()

{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    ll n;
    cin >> n;
    for (ll i = 0; i < n - 1; i++) {
      ll a, b;
      cin >> a >> b;
      adj[a].push_back(b);
      adj[b].push_back(a);
    }
    vis = vector<ll>(n + 2, 0);
    vis[1] = 1;
    if (dfs(1))
      cout << "Alice" << endl;
    else
      cout << "Bob" << endl;
  }
  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
namespace io {
const int L = (1 << 19) + 1;
char ibuf[L], *iS, *iT, c;
inline char Gc() {
  if (iS == iT) {
    iT = (iS = ibuf) + fread(ibuf, 1, L, stdin);
    return iS == iT ? EOF : *iS++;
  }
  return *iS++;
}
template <class I> void Gi(I &x) {
  for (c = Gc(); c < '0' || c > '9'; c = Gc())
    ;
  for (x = 0; c <= '9' && c >= '0'; c = Gc())
    x = x * 10 + (c & 15);
}
}; // namespace io
using io::Gi;
int N, T;
struct edge {
  int adj, nex;
  edge(int a_ = 0, int n_ = 0) {
    adj = a_;
    nex = n_;
  }
} E[222222];
void add(int u, int v) {
  E[++T] = edge(v, E[u].nex);
  E[u].nex = T;
  E[++T] = edge(u, E[v].nex);
  E[v].nex = T;
}
int dfs(int x, int f) {
  int s = 0;
  for (int i = E[x].nex; i; i = E[i].nex) {
    if (E[i].adj != f) {
      s ^= (dfs(E[i].adj, x) + 1);
    }
  }
  return s;
}
int main() {
  int u, v;
  Gi(N);
  T = N;
  for (int i = 1; i != N; i++) {
    Gi(u);
    Gi(v);
    add(u, v);
  }
  printf(dfs(1, 0) ? "Alice" : "Bob");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
namespace io {
const int L = (1 << 19) + 1;
char ibuf[L], *iS, *iT, c;
inline char Gc() {
  if (iS == iT) {
    iT = (iS = ibuf) + fread(ibuf, 1, L, stdin);
    return iS == iT ? EOF : *iS++;
  }
  return *iS++;
}
template <class I> void Gi(I &x) {
  for (c = Gc(); c < '0' || c > '9'; c = Gc())
    ;
  for (x = 0; c <= '9' && c >= '0'; c = Gc())
    x = x * 10 + (c & 15);
}
}; // namespace io
using io::Gi;
int N, T;
struct edge {
  int adj, nex;
  edge(int a_ = 0, int n_ = 0) {
    adj = a_;
    nex = n_;
  }
} E[333333];
void add(int u, int v) {
  E[++T] = edge(v, E[u].nex);
  E[u].nex = T;
  E[++T] = edge(u, E[v].nex);
  E[v].nex = T;
}
int dfs(int x, int f) {
  int s = 0;
  for (int i = E[x].nex; i; i = E[i].nex) {
    if (E[i].adj != f) {
      s ^= (dfs(E[i].adj, x) + 1);
    }
  }
  return s;
}
int main() {
  int u, v;
  Gi(N);
  T = N;
  for (int i = 1; i != N; i++) {
    Gi(u);
    Gi(v);
    add(u, v);
  }
  printf(dfs(1, 0) ? "Alice" : "Bob");
  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
/***********Template Starts Here***********/
// copied from
// https://www.hackerrank.com/contests/5-days-of-game-theory/challenges/deforestation/editorial

#include <bits/stdc++.h>

#define pb push_back
#define FOR(i, x, y) for (vlong i = (x); i <= (y); ++i)
#define ROF(i, x, y) for (vlong i = (y); i >= (x); --i)
#define CLR(x, y) memset(x, y, sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(), (V).end()), (V).end())
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SZ(x) ((vlong)(x).size())

using namespace std;

typedef long long vlong;

const vlong inf = 2147383647;

/***********Template Ends Here***********/
#define NODE 502
vector<int> adj[NODE + 10];

int hackenbush(int s, int p) {

  int res = 0;
  FOR(i, 0, SZ(adj[s]) - 1) {
    int t = adj[s][i];

    if (t == p)
      continue;

    res ^= hackenbush(t, s) + 1;
  };

  return res;
}

void solution() {
  int kase;
  // scanf ( "%d", &kase );

  // assert ( kase >= 1 && kase <= 100 );
  kase = 1;
  while (kase--) {
    int n;
    scanf("%d", &n);

    //  assert ( n >= 1 && n <= 500 );

    FOR(i, 1, n) adj[i].clear();

    FOR(i, 1, n - 1) {
      int u, v;
      scanf("%d %d", &u, &v);
      adj[u].pb(v);
      adj[v].pb(u);
    };

    int res = hackenbush(1, 0);

    if (res)
      printf("Alice\n");
    else
      printf("Bob\n");
  }
}

int main() {

  solution();

  return 0;
}

Fixed submission:
/***********Template Starts Here***********/
// copied from
// https://www.hackerrank.com/contests/5-days-of-game-theory/challenges/deforestation/editorial

#include <bits/stdc++.h>

#define pb push_back
#define FOR(i, x, y) for (vlong i = (x); i <= (y); ++i)
#define ROF(i, x, y) for (vlong i = (y); i >= (x); --i)
#define CLR(x, y) memset(x, y, sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(), (V).end()), (V).end())
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SZ(x) ((vlong)(x).size())

using namespace std;

typedef long long vlong;

const vlong inf = 2147383647;

/***********Template Ends Here***********/
#define NODE 100005
vector<int> adj[NODE + 10];

int hackenbush(int s, int p) {

  int res = 0;
  FOR(i, 0, SZ(adj[s]) - 1) {
    int t = adj[s][i];

    if (t == p)
      continue;

    res ^= hackenbush(t, s) + 1;
  };

  return res;
}

void solution() {
  int kase;
  // scanf ( "%d", &kase );

  // assert ( kase >= 1 && kase <= 100 );
  kase = 1;
  while (kase--) {
    int n;
    scanf("%d", &n);

    //  assert ( n >= 1 && n <= 500 );

    FOR(i, 1, n) adj[i].clear();

    FOR(i, 1, n - 1) {
      int u, v;
      scanf("%d %d", &u, &v);
      adj[u].pb(v);
      adj[v].pb(u);
    };

    int res = hackenbush(1, 0);

    if (res)
      printf("Alice\n");
    else
      printf("Bob\n");
  }
}

int main() {

  solution();

  return 0;
}
Problem p03668 (C++)

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

using namespace std;
int N;
vector<int> G[1005];

int dfs(int v, int p) {
  int res = 0;
  for (int u : G[v]) {
    if (u == p)
      continue;
    res ^= dfs(u, v) + 1;
  }
  return res;
}

int main() {
  cin >> N;
  REP(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  cout << (dfs(0, -1) ? "Alice" : "Bob") << endl;
  return 0;
}


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

using namespace std;
int N;
vector<int> G[100005];

int dfs(int v, int p) {
  int res = 0;
  for (int u : G[v]) {
    if (u == p)
      continue;
    res ^= dfs(u, v) + 1;
  }
  return res;
}

int main() {
  cin >> N;
  REP(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  cout << (dfs(0, -1) ? "Alice" : "Bob") << endl;
  return 0;
}
Problem p03668 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int n;
#define MAX 100002

vector<int> v[MAX];
int gr[MAX];

inline void dfs(int b, int pr = -1) {
  if (v[b].size() == 1 && pr != -1) {
    gr[b] = 0;
    return;
  }
  for (int i = 0; i < v[b].size(); i++) {
    int go = v[b][i];
    if (go != pr) {
      dfs(go);
      gr[b] ^= (gr[go] + 1);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  if (gr[0]) {
    puts("Alice");
  } else {
    puts("Bob");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int n;
#define MAX 100002

vector<int> v[MAX];
int gr[MAX];

inline void dfs(int b, int pr = -1) {
  if (v[b].size() == 1 && pr != -1) {
    gr[b] = 0;
    return;
  }
  for (int i = 0; i < v[b].size(); i++) {
    int go = v[b][i];
    if (go != pr) {
      dfs(go, b);
      gr[b] ^= (gr[go] + 1);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  if (gr[0]) {
    puts("Alice");
  } else {
    puts("Bob");
  }
  return 0;
}
Problem p03669 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 405;
int fa[N], vis[N], in[N], out[N], n, h;
inline int find(R int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d%d", &n, &h);
  fp(i, 0, h << 1) fa[i] = i;
  for (R int i = 1, a, b, c, d, l, r; i <= n; ++i) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    l = (c ? -c : a) + h, r = (d ? d : -b) + h, fa[find(l)] = find(r);
    ++out[l], ++in[r];
  }
  fp(i, -h, -1) if (out[i + h] > in[i + h]) return puts("NO"), 0;
  fp(i, 1, h) if (in[i + h] > out[i + h]) return puts("NO"), 0;
  fp(i, 0, h << 1) if (in[i] != out[i]) vis[find(i)] = 1;
  fp(i, 1, n) if (in[i] && out[i] && !vis[find(i)]) return puts("NO"), 0;
  puts("YES");
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int N = 405;
int fa[N], vis[N], in[N], out[N], n, h;
inline int find(R int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d%d", &n, &h);
  fp(i, 0, h << 1) fa[i] = i;
  for (R int i = 1, a, b, c, d, l, r; i <= n; ++i) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    l = (c ? -c : a) + h, r = (d ? d : -b) + h, fa[find(l)] = find(r);
    ++out[l], ++in[r];
  }
  fp(i, -h, -1) if (out[i + h] > in[i + h]) return puts("NO"), 0;
  fp(i, 1, h) if (in[i + h] > out[i + h]) return puts("NO"), 0;
  fp(i, 0, h << 1) if (in[i] != out[i]) vis[find(i)] = 1;
  fp(i, 0, h << 1) if (in[i] && out[i] && !vis[find(i)]) return puts("NO"), 0;
  puts("YES");
  return 0;
}
Problem p03669 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>
int F() {
  register int a, b, c;
  while (c = getchar(), c < '-')
    ;
  b = (c == '-');
  for (a = b ? 0 : c - '0'; c = getchar(), c > '-'; a = a * 10 + c - '0')
    ;
  return b ? -a : a;
}
const int N = 2e5 + 10;
int fa[N];
int find(register int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int deg[N];
bool mark[N], appear[N];
bool work() {
  register int n = F(), h = F(), i, a, b, c, d, cnt = 0, ans = 0;
  for (i = 1; i <= 2 * h; ++i)
    fa[i] = i, deg[i] = 0, appear[i] = mark[i] = 0;
  for (i = 1; i <= n; ++i) {
    a = F(), b = F(), c = F(), d = F();
    ++deg[c = c ? c : a + h];
    --deg[d = d ? d + h : b];
    fa[find(c)] = find(d);
    appear[c] = appear[d] = 1;
  }
  for (i = 1; i <= 2 * h; ++i)
    if (appear[i]) {
      if (i <= h && deg[i] > 0 || i > h && deg[i] < 0)
        return 0;
      mark[i] = deg[i] != 0;
    }
  for (i = 1; i <= 2 * h; ++i)
    mark[find(i)] |= mark[i];
  for (i = 1; i <= 2 * h; ++i)
    if (appear[i] && find(i) == i && !mark[i])
      return 0;
  return 1;
}
int main() {
  register int t = F();
  while (t--)
    puts(work() ? "YES" : "NO");
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
int F() {
  register int a, b, c;
  while (c = getchar(), c < '-')
    ;
  b = (c == '-');
  for (a = b ? 0 : c - '0'; c = getchar(), c > '-'; a = a * 10 + c - '0')
    ;
  return b ? -a : a;
}
const int N = 2e5 + 10;
int fa[N];
int find(register int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int deg[N];
bool mark[N], appear[N];
bool work() {
  register int n = F(), h = F(), i, a, b, c, d, cnt = 0, ans = 0;
  for (i = 1; i <= 2 * h; ++i)
    fa[i] = i, deg[i] = 0, appear[i] = mark[i] = 0;
  for (i = 1; i <= n; ++i) {
    a = F(), b = F(), c = F(), d = F();
    ++deg[c = c ? c : a + h];
    --deg[d = d ? d + h : b];
    fa[find(c)] = find(d);
    appear[c] = appear[d] = 1;
  }
  for (i = 1; i <= 2 * h; ++i)
    if (appear[i]) {
      if (i <= h && deg[i] > 0 || i > h && deg[i] < 0)
        return 0;
      mark[i] = deg[i] != 0;
    }
  for (i = 1; i <= 2 * h; ++i)
    mark[find(i)] |= mark[i];
  for (i = 1; i <= 2 * h; ++i)
    if (appear[i] && find(i) == i && !mark[i])
      return 0;
  return 1;
}
int main() {
  register int t = 1;
  while (t--)
    puts(work() ? "YES" : "NO");
  return 0;
}
Problem p03669 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

void fail(int i) {
  printf("NO\n", i);
  exit(0);
}

int n, h;
struct block {
  int l, r;
  void scan() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == 0)
      l = a << 1;
    else
      l = c << 1 | 1;
    if (d == 0)
      r = b << 1 | 1;
    else
      r = d << 1;
  }
} bs[100000];

int par[402];
int find(int x) {
  if (par[x] != x)
    return par[x] = find(par[x]);
  return x;
}

int cnt[402];
int ex[402];
int zr[402];
int main() {
  freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 402; ++i)
    par[i] = i;
  cin >> n >> h;
  for (int i = 0; i < n; ++i) {
    bs[i].scan();
    ++cnt[bs[i].l];
    --cnt[bs[i].r];
    ex[bs[i].l] = 1;
    ex[bs[i].r] = 1;
    par[find(bs[i].l)] = find(bs[i].r);
  }
  for (int i = 0; i < 402; ++i) {
    if (cnt[i] < 0 && ((i ^ 1) & 1))
      fail(0);
    if (cnt[i] > 0 && ((i ^ 0) & 1))
      fail(1);
    if (cnt[i] != 0)
      zr[find(i)] = 1;
  }
  for (int i = 0; i < 402; ++i) {
    if (ex[i] == 0)
      continue;
    if (find(i) != i)
      continue;
    if (zr[i] == 0)
      fail(2);
  }
  printf("YES\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

void fail(int i) {
  printf("NO\n", i);
  exit(0);
}

int n, h;
struct block {
  int l, r;
  void scan() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == 0)
      l = a << 1;
    else
      l = c << 1 | 1;
    if (d == 0)
      r = b << 1 | 1;
    else
      r = d << 1;
  }
} bs[100000];

int par[402];
int find(int x) {
  if (par[x] != x)
    return par[x] = find(par[x]);
  return x;
}

int cnt[402];
int ex[402];
int zr[402];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 402; ++i)
    par[i] = i;
  cin >> n >> h;
  for (int i = 0; i < n; ++i) {
    bs[i].scan();
    ++cnt[bs[i].l];
    --cnt[bs[i].r];
    ex[bs[i].l] = 1;
    ex[bs[i].r] = 1;
    par[find(bs[i].l)] = find(bs[i].r);
  }
  for (int i = 0; i < 402; ++i) {
    if (cnt[i] < 0 && ((i ^ 1) & 1))
      fail(0);
    if (cnt[i] > 0 && ((i ^ 0) & 1))
      fail(1);
    if (cnt[i] != 0)
      zr[find(i)] = 1;
  }
  for (int i = 0; i < 402; ++i) {
    if (ex[i] == 0)
      continue;
    if (find(i) != i)
      continue;
    if (zr[i] == 0)
      fail(2);
  }
  printf("YES\n");
  return 0;
}