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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b)                                                           \
  for (register int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b)                                                          \
  for (register int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clear(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch - 0x30);
    ch = getchar();
  }
  return flag * x;
}

#define Max 509
int H, W;
int blue[Max][Max], red[Max][Max];
int data[Max][Max];
int main() {
#ifndef ONLINE_JUDGE
  freopen("b.in", "r", stdin);
  freopen("b.out", "w", stdout);
#endif
  H = read(), W = read();
  rep(i, 1, H) {
    rep(j, 1, W) {
      char ch = getchar();
      if (ch == '.')
        data[i][j] = 0;
      if (ch == '#')
        data[i][j] = 1;
    }
    getchar();
  }
  rep(i, 1, W) red[1][i] = 1;
  rep(i, 1, W) blue[H][i] = 1;
  rep(i, 2, H - 1) rep(j, 1, W) if (j & 1) red[i][j] = 1;
  else blue[i][j] = 1;
  rep(i, 1, H) rep(j, 1, W) if (data[i][j]) red[i][j] = blue[i][j] = 1;
  rep(i, 1, H) {
    rep(j, 1, W) printf("%c", red[i][j] ? '#' : '.');
    puts("");
  }
  puts("");
  rep(i, 1, H) {
    rep(j, 1, W) printf("%c", blue[i][j] ? '#' : '.');
    puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b)                                                           \
  for (register int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b)                                                          \
  for (register int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clear(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch - 0x30);
    ch = getchar();
  }
  return flag * x;
}

#define Max 509
int H, W;
int blue[Max][Max], red[Max][Max];
int data[Max][Max];
int main() {
  H = read(), W = read();
  rep(i, 1, H) {
    rep(j, 1, W) {
      char ch = getchar();
      if (ch == '.')
        data[i][j] = 0;
      if (ch == '#')
        data[i][j] = 1;
    }
    getchar();
  }
  rep(i, 1, W) red[1][i] = 1;
  rep(i, 1, W) blue[H][i] = 1;
  rep(i, 2, H - 1) rep(j, 1, W) if (j & 1) red[i][j] = 1;
  else blue[i][j] = 1;
  rep(i, 1, H) rep(j, 1, W) if (data[i][j]) red[i][j] = blue[i][j] = 1;
  rep(i, 1, H) {
    rep(j, 1, W) printf("%c", red[i][j] ? '#' : '.');
    puts("");
  }
  puts("");
  rep(i, 1, H) {
    rep(j, 1, W) printf("%c", blue[i][j] ? '#' : '.');
    puts("");
  }
  return 0;
}
Problem p04007 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : Calc.cpp
// Author      : Roman
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {

  ifstream cin("/Users/shuffler/Documents/workspace/Calc/src/input.txt");
  // ofstream cout ("/Users/shuffler/Documents/workspace/Calc/src/output.txt");

  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  srand(179);
  int n, m;
  cin >> n >> m;
  vector<vector<char>> gr(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> gr[i][j];
    }
  }
  auto bl = gr;
  auto red = gr;
  for (int i = 0; i < n; ++i) {
    red[i][0] = '#';
    bl[i][m - 1] = '#';
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j + 1 < m; ++j) {
      if (i % 2 == 0) {
        red[i][j] = '#';
      } else {
        bl[i][j] = '#';
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << red[i][j];
    }
    cout << endl;
  }
  cout << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << bl[i][j];
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
//============================================================================
// Name        : Calc.cpp
// Author      : Roman
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {

  // ifstream cin ("/Users/shuffler/Documents/workspace/Calc/src/input.txt");
  // ofstream cout ("/Users/shuffler/Documents/workspace/Calc/src/output.txt");

  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  srand(179);
  int n, m;
  cin >> n >> m;
  vector<vector<char>> gr(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> gr[i][j];
    }
  }
  auto bl = gr;
  auto red = gr;
  for (int i = 0; i < n; ++i) {
    red[i][0] = '#';
    bl[i][m - 1] = '#';
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j + 1 < m; ++j) {
      if (i % 2 == 0) {
        red[i][j] = '#';
      } else {
        bl[i][j] = '#';
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << red[i][j];
    }
    cout << endl;
  }
  cout << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << bl[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p04007 (C++)

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

using namespace std;

const int N = 110;

int w, h, mp[N][N];

void Init() {
  scanf("%d%d", &w, &h);
  for (int i = 0; i < w; ++i)
    for (int j = 0; j < h; ++j) {
      char ch = getchar();
      while (ch != '#' && ch != '.')
        ch = getchar();
      if (ch == '#')
        mp[i][j] = 1;
      else
        mp[i][j] = 0;
    }
}

void Solve() {
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j)
      if (i != 0 && (mp[i][j] || j & 1 || i == w - 1))
        putchar('#');
      else
        putchar('.');
    putchar('\n');
  }
  putchar('\n');

  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j)
      if (i != w - 1 && (mp[i][j] || !(j & 1) || i == 0))
        putchar('#');
      else
        putchar('.');
    putchar('\n');
  }
}

int main() {
  Init();
  Solve();
  return 0;
}

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

using namespace std;

const int N = 510;

int w, h, mp[N][N];

void Init() {
  scanf("%d%d", &w, &h);
  for (int i = 0; i < w; ++i)
    for (int j = 0; j < h; ++j) {
      char ch = getchar();
      while (ch != '#' && ch != '.')
        ch = getchar();
      if (ch == '#')
        mp[i][j] = 1;
      else
        mp[i][j] = 0;
    }
}

void Solve() {
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j)
      if (i != 0 && (mp[i][j] || j & 1 || i == w - 1))
        putchar('#');
      else
        putchar('.');
    putchar('\n');
  }
  putchar('\n');

  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j)
      if (i != w - 1 && (mp[i][j] || !(j & 1) || i == 0))
        putchar('#');
      else
        putchar('.');
    putchar('\n');
  }
}

int main() {
  Init();
  Solve();
  return 0;
}
Problem p04007 (C++)

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

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

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

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

  vector<string> input(w);
  rep(i, h) cin >> input[i];

  vector<string> r, b;
  r = input;
  b = input;
  rep(i, h) {
    for (int j = 1; j < w - 1; j++) {
      if (i % 2 == 0) {
        r[i][j] = '#';
      } else {
        b[i][j] = '#';
      }
    }
  }

  rep(i, h - 1) {
    r[i][w - 1] = '#';
    b[i + 1][0] = '#';
  }

  rep(i, h) { cout << r[i] << endl; }
  cout << endl;
  rep(i, h) { cout << b[i] << endl; }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

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

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

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

  vector<string> input(h);
  rep(i, h) cin >> input[i];

  vector<string> r, b;
  r = input;
  b = input;
  rep(i, h) {
    for (int j = 1; j < w - 1; j++) {
      if (i % 2 == 0) {
        r[i][j] = '#';
      } else {
        b[i][j] = '#';
      }
    }
  }

  rep(i, h - 1) {
    r[i][w - 1] = '#';
    b[i + 1][0] = '#';
  }

  rep(i, h) { cout << r[i] << endl; }
  cout << endl;
  rep(i, h) { cout << b[i] << endl; }

  return 0;
}
Problem p04007 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define ll long long
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 505;
char a[N][N], b[N][N], c[N][N];

int main() {
  freopen("in.txt", "r", stdin);
  int n = read(), m = read();
  rep(i, 1, n) scanf("%s", c[i] + 1);
  rep(i, 1, n) {
    a[i][1] = '#', a[i][m] = '.';
    b[i][m] = '#', b[i][1] = '.';
    if (i & 1) {
      rep(j, 2, m - 1) a[i][j] = '#', b[i][j] = '.';
    } else {
      rep(j, 2, m - 1) a[i][j] = '.', b[i][j] = '#';
    }
  }
  rep(i, 1, n) { rep(j, 1, m) if (c[i][j] == '#') a[i][j] = b[i][j] = '#'; }
  rep(i, 1, n) { printf("%s\n", a[i] + 1); }
  puts("");
  rep(i, 1, n) { printf("%s\n", b[i] + 1); }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define ll long long
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 505;
char a[N][N], b[N][N], c[N][N];

int main() {
  //	freopen("in.txt","r",stdin);
  int n = read(), m = read();
  rep(i, 1, n) scanf("%s", c[i] + 1);
  rep(i, 1, n) {
    a[i][1] = '#', a[i][m] = '.';
    b[i][m] = '#', b[i][1] = '.';
    if (i & 1) {
      rep(j, 2, m - 1) a[i][j] = '#', b[i][j] = '.';
    } else {
      rep(j, 2, m - 1) a[i][j] = '.', b[i][j] = '#';
    }
  }
  rep(i, 1, n) { rep(j, 1, m) if (c[i][j] == '#') a[i][j] = b[i][j] = '#'; }
  rep(i, 1, n) { printf("%s\n", a[i] + 1); }
  puts("");
  rep(i, 1, n) { printf("%s\n", b[i] + 1); }
  return 0;
}
Problem p04007 (C++)

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

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

const int maxn = 505;
int n, m;
string s[maxn];
string red[maxn], blue[maxn];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    red[i] = s[i];
    blue[i] = s[i];
    for (int j = 0; j < n; j++) {
      red[i][j] = '.';
      blue[i][j] = '.';
    }
  }
  for (int i = 0; i < n; i++) {
    red[i][0] = '#';
    blue[i][m - 1] = '#';
    if (i & 1) {
      for (int j = 1; j < m - 1; j++) {
        red[i][j] = '#';
        blue[i][j] = s[i][j];
      }
    } else {
      for (int j = 1; j < m - 1; j++) {
        blue[i][j] = '#';
        red[i][j] = s[i][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << red[i] << endl;
  }
  cout << endl;
  for (int i = 0; i < n; i++) {
    cout << blue[i] << endl;
  }
  return 0;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

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

const int maxn = 505;
int n, m;
string s[maxn];
string red[maxn], blue[maxn];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    red[i] = s[i];
    blue[i] = s[i];
    for (int j = 0; j < m; j++) {
      red[i][j] = '.';
      blue[i][j] = '.';
    }
  }
  for (int i = 0; i < n; i++) {
    red[i][0] = '#';
    blue[i][m - 1] = '#';
    if (i & 1) {
      for (int j = 1; j < m - 1; j++) {
        red[i][j] = '#';
        blue[i][j] = s[i][j];
      }
    } else {
      for (int j = 1; j < m - 1; j++) {
        blue[i][j] = '#';
        red[i][j] = s[i][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << red[i] << endl;
  }
  cout << endl;
  for (int i = 0; i < n; i++) {
    cout << blue[i] << endl;
  }
  return 0;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;

vector<int> G[100000];
int depth[100000];
bool ok[100000];
P depth_pair[100000];

void dfs_depth(int v, int d) {
  depth[v] = d;
  for (int i = 0; i < G[v].size(); i++) {
    if (depth[G[v][i]] == -1)
      dfs_depth(G[v][i], d + 1);
  }
}

void dfs_ok(int v) {
  ok[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (depth[G[v][i]] > depth[v])
      dfs_ok(G[v][i]);
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    depth[i] = -1;
    int a;
    cin >> a;
    a--;
    if (i == 0) {
      if (a != 0)
        ans++;
    } else {
      G[a].push_back(i);
      G[i].push_back(a);
    }
  }
  // cout << ans << endl;
  dfs_depth(0, 0);
  for (int i = 0; i < N; i++)
    depth_pair[i] = P(depth[i], i);
  sort(depth_pair, depth_pair + N, greater<P>());
  for (int i = 0; i < N; i++) {
    if (depth_pair[i].first <= K)
      break;
    if (!ok[depth_pair[i].second]) {
      ans++;
      int cur = depth_pair[i].second;
      // cout << depth_pair[i].second << endl;
      // K-1個上まで
      for (int j = 0; j < K - 1; j++) {
        for (int k = 0; k < G[cur].size(); k++) {
          if (depth[G[cur][k]] < depth[cur]) {
            cur = G[cur][k];
            break;
          }
        }
      }
      dfs_ok(cur);
    }
  }
  cout << ans << endl;
}

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

using namespace std;

typedef pair<int, int> P;

vector<int> G[100000];
int depth[100000];
bool ok[100000];
P depth_pair[100000];

void dfs_depth(int v, int d) {
  depth[v] = d;
  for (int i = 0; i < G[v].size(); i++) {
    if (depth[G[v][i]] == -1)
      dfs_depth(G[v][i], d + 1);
  }
}

void dfs_ok(int v) {
  ok[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (depth[G[v][i]] > depth[v] && !ok[G[v][i]])
      dfs_ok(G[v][i]);
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    depth[i] = -1;
    int a;
    cin >> a;
    a--;
    if (i == 0) {
      if (a != 0)
        ans++;
    } else {
      G[a].push_back(i);
      G[i].push_back(a);
    }
  }
  // cout << ans << endl;
  dfs_depth(0, 0);
  for (int i = 0; i < N; i++)
    depth_pair[i] = P(depth[i], i);
  sort(depth_pair, depth_pair + N, greater<P>());
  for (int i = 0; i < N; i++) {
    if (depth_pair[i].first <= K)
      break;
    if (!ok[depth_pair[i].second]) {
      ans++;
      int cur = depth_pair[i].second;
      // cout << depth_pair[i].second << endl;
      // K-1個上まで
      for (int j = 0; j < K - 1; j++) {
        for (int k = 0; k < G[cur].size(); k++) {
          if (depth[G[cur][k]] < depth[cur]) {
            cur = G[cur][k];
            break;
          }
        }
      }
      dfs_ok(cur);
    }
  }
  cout << ans << endl;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <memory.h>
#include <queue>
using namespace std;

int a[100001], k;
vector<int> v[100001];
int dist[100001], cnt;

int sparse[100001][17];

bool check[100001];

void dfs(int now, int len) {
  if (check[now])
    return;
  check[now] = true;
  dist[now] = len;
  for (auto dest : v[now])
    dfs(dest, len + 1);
}

bool visited[100001];
void make_visit(int now) {
  visited[now] = true;
  for (auto dest : v[now])
    make_visit(dest);
}

priority_queue<pair<int, int>> pq;

int main() {
  int n;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (i != 1)
      v[a[i]].push_back(i);
  }
  if (a[1] != 1)
    a[1] = 1, cnt++;

  for (int i = 1; i <= n; i++)
    sparse[i][0] = a[i];
  for (int i = 1; i <= 16; i++)
    for (int j = 1; j <= n; j++)
      sparse[j][i] = sparse[sparse[j][i - 1]][i - 1];

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    if (dist[i] > k)
      pq.push(make_pair(dist[i], i));
  while (!pq.empty()) {
    int now = pq.top().second;
    pq.pop();
    if (visited[now])
      continue;

    int need = k - 1;
    for (int i = 16; i >= 0; i--)
      if (need & (1 << i))
        now = sparse[now][i];
    make_visit(now);
    cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <memory.h>
#include <queue>
using namespace std;

int a[100001], k;
vector<int> v[100001];
int dist[100001], cnt;

int sparse[100001][17];

bool check[100001];

void dfs(int now, int len) {
  if (check[now])
    return;
  check[now] = true;
  dist[now] = len;
  for (auto dest : v[now])
    dfs(dest, len + 1);
}

bool visited[100001];
void make_visit(int now) {
  if (visited[now])
    return;
  visited[now] = true;
  for (auto dest : v[now])
    make_visit(dest);
}

priority_queue<pair<int, int>> pq;

int main() {
  int n;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (i != 1)
      v[a[i]].push_back(i);
  }
  if (a[1] != 1)
    a[1] = 1, cnt++;

  for (int i = 1; i <= n; i++)
    sparse[i][0] = a[i];
  for (int i = 1; i <= 16; i++)
    for (int j = 1; j <= n; j++)
      sparse[j][i] = sparse[sparse[j][i - 1]][i - 1];

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    if (dist[i] > k)
      pq.push(make_pair(dist[i], i));
  while (!pq.empty()) {
    int now = pq.top().second;
    pq.pop();
    if (visited[now])
      continue;

    int need = k - 1;
    for (int i = 16; i >= 0; i--)
      if (need & (1 << i))
        now = sparse[now][i];
    make_visit(now);
    cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p04008 (C++)

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

const int MAX_N = 100005;

class Edge {
public:
  int nxt, to;
} e[MAX_N];

int fa[MAX_N], head[MAX_N], cnt, id[MAX_N];

void addedge(int u, int v) { e[++cnt] = (Edge){head[u], v}, head[u] = cnt; }

int N, dep[MAX_N], vis[MAX_N], K;

void dfs(int u, int v) {
  dep[u] = dep[v] + 1;
  for (int i = head[u]; i; i = e[i].nxt)
    dfs(e[i].to, u);
}

bool cmp(int x, int y) { return dep[x] > dep[y]; }

void dfs1(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = e[i].nxt)
    dfs1(e[i].to);
}

int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &fa[i]);
    if (i != 1)
      addedge(fa[i], i);
    id[i] = i;
  }

  int res = fa[1] != 1;

  dfs(1, 0);

  sort(id + 1, id + N + 1, cmp);

  for (int i = 1; i <= N; ++i)
    if (!vis[id[i]] && dep[id[i]] > K + 1) {
      int u = id[i], temp = K - 1;
      while (temp)
        u = fa[u], temp--;
      dfs1(u), res++;
    }

  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int MAX_N = 100005;

class Edge {
public:
  int nxt, to;
} e[MAX_N];

int fa[MAX_N], head[MAX_N], cnt, id[MAX_N];

void addedge(int u, int v) { e[++cnt] = (Edge){head[u], v}, head[u] = cnt; }

int N, dep[MAX_N], vis[MAX_N], K;

void dfs(int u, int v) {
  dep[u] = dep[v] + 1;
  for (int i = head[u]; i; i = e[i].nxt)
    dfs(e[i].to, u);
}

bool cmp(int x, int y) { return dep[x] > dep[y]; }

void dfs1(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = e[i].nxt)
    if (!vis[e[i].to])
      dfs1(e[i].to);
}

int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &fa[i]);
    if (i != 1)
      addedge(fa[i], i);
    id[i] = i;
  }

  int res = fa[1] != 1;

  dfs(1, 0);

  sort(id + 1, id + N + 1, cmp);

  for (int i = 1; i <= N; ++i)
    if (!vis[id[i]] && dep[id[i]] > K + 1) {
      int u = id[i], temp = K - 1;
      while (temp)
        u = fa[u], temp--;
      dfs1(u), res++;
    }

  printf("%d\n", res);
  return 0;
}
Problem p04008 (C++)

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

using namespace std;

#define foru(i, l, r) for (int i = l; i <= r; i++)
#define ford(i, r, l) for (int i = r; i >= l; i--)

typedef pair<int, int> pii;
#define mk make_pair

const int maxn = 1e5 + 5;

vector<int> G[maxn];
int N, K, Ans;
int fa[maxn], dis[maxn];

void dfs(int x) {
  for (int to : G[x])
    dfs(to), dis[x] = max(dis[x], dis[to] + 1);
  if (fa[x] != 1 && dis[x] == K - 1)
    Ans++, dis[x] = -1;
}

int main() {
  scanf("%d%d", &N, &K);
  foru(i, 1, N) scanf("%d", &fa[i]);
  Ans += fa[1] != 1, fa[1] = 1;
  foru(i, 2, N) G[fa[i]].push_back(i);
  memset(dis, 0, sizeof(dis));
  dfs(1);
  printf("%d\n", Ans);
  return 0;
}


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

using namespace std;

#define foru(i, l, r) for (int i = l; i <= r; i++)
#define ford(i, r, l) for (int i = r; i >= l; i--)

const int maxn = 1e5 + 5;

vector<int> G[maxn];
int N, K, Ans;
int fa[maxn], dis[maxn];

void dfs(int x) {
  for (int to : G[x])
    dfs(to), dis[x] = max(dis[x], dis[to] + 1);
  if (fa[x] != 1 && dis[x] == K - 1)
    Ans++, dis[x] = -1;
}

int main() {
  scanf("%d%d", &N, &K);
  foru(i, 1, N) scanf("%d", &fa[i]);
  Ans += fa[1] != 1, fa[1] = 1;
  foru(i, 2, N) G[fa[i]].push_back(i);
  memset(dis, 0, sizeof(dis));
  dfs(1);
  printf("%d\n", Ans);
  return 0;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DUMP(a, n) REP(i, n) printf("%d%c", a[i], i + 1 == n ? '\n' : ' ')

const int N_MAX = 100000;

int N, K;
int a[N_MAX];

vector<int> rG[N_MAX];
int dist[N_MAX];
pair<int, int> dv[N_MAX];
int ans;

void rdfs(int v, int d) {
  dist[v] = d;
  REP(i, rG[v].size()) { rdfs(rG[v][i], d + 1); }
}

void dfs(int v, int d) {
  if (v == 0)
    return;
  dist[v] = d;
  if (d == 1) {
    if (a[v] != 0) {
      a[v] = 0;
      ans++;
    }
    return;
  }
  dfs(a[v], d - 1);
}

void solve() {
  ans = 0;
  if (a[0] != 0) {
    a[0] = 0;
    ans++;
  }

  FOR(i, 1, N - 1) rG[a[i]].push_back(i);
  rdfs(0, 0);

  REP(i, N) dv[i] = make_pair(dist[i], i);
  sort(dv, dv + N);

  for (int i = N - 1; i >= 0; i--) {
    int v = dv[i].second;
    if (dist[v] > K)
      dfs(v, K);
  }
  printf("%d\n", ans);
}

void input() {
  scanf("%d%d", &N, &K);
  REP(i, N) {
    scanf("%d", a + i);
    a[i]--;
  }
}

int main() {
  input();
  solve();
  return 0;
}


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

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DUMP(a, n) REP(i, n) printf("%d%c", a[i], i + 1 == n ? '\n' : ' ')

const int N_MAX = 100000;

int N, K;
int a[N_MAX];

vector<int> rG[N_MAX];
int dist[N_MAX];
pair<int, int> dv[N_MAX];
int ans;

void rdfs(int v, int d) {
  dist[v] = d;
  REP(i, rG[v].size()) { rdfs(rG[v][i], d + 1); }
}

void dfs(int v, int d) {
  if (dist[v] <= d)
    return;
  dist[v] = d;
  if (d == 1) {
    if (a[v] != 0) {
      a[v] = 0;
      ans++;
    }
    return;
  }
  dfs(a[v], d - 1);
}

void solve() {
  ans = 0;
  if (a[0] != 0) {
    a[0] = 0;
    ans++;
  }

  FOR(i, 1, N - 1) rG[a[i]].push_back(i);
  rdfs(0, 0);

  REP(i, N) dv[i] = make_pair(dist[i], i);
  sort(dv, dv + N);

  for (int i = N - 1; i >= 0; i--) {
    int v = dv[i].second;
    if (dist[v] > K)
      dfs(v, K);
  }
  printf("%d\n", ans);
}

void input() {
  scanf("%d%d", &N, &K);
  REP(i, N) {
    scanf("%d", a + i);
    a[i]--;
  }
}

int main() {
  input();
  solve();
  return 0;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, K;
int A[100005];
int dp[100005];

int solve(int id) {
  if (dp[id] != -1)
    return dp[id];
  return dp[id] = solve(A[id]) + 1;
}

bool iszero() {
  int R = dp[A[0]] + 1;
  if (K == 1) {
    for (int i = 0; i < N; i++)
      if (A[i] != 0)
        return false;
  } else if (R == 1) {
    for (int i = 1; i < N; i++)
      if (A[i] > K)
        return false;
  } else if (K <= R) {
    for (int i = 1; i < N; i++)
      if (dp[i] != K)
        return false;
  } else {
    for (int i = 1; i < N; i++) {
      if (K >= dp[i] && (K - dp[i]) % R == 0)
        continue;
      return false;
    }
  }
  return true;
}
int res;
int dfs(int id, int d) {
  if (id == 0)
    return dp[id] = 0;
  if (d == K - 1 && A[id] != 0) {
    A[id] = 0;
    res++;
    return dp[id] = 1;
  }
  return dp[id] = dfs(A[id], d + 1) + 1;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    --A[i];
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    solve(i);
  if (iszero())
    cout << 0 << endl;
  else {
    res = 0;
    if (A[0])
      res++;
    A[0] = 0;
    vector<P> v;
    for (int i = 1; i < N; i++) {
      v.push_back(P(dp[i], i));
    }
    sort(v.rbegin(), v.rend());

    for (P t : v) {
      if (dp[t.second] <= K)
        continue;
      dfs(t.second, 0);
    }

    cout << res << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, K;
int A[100005];
int dp[100005];

int solve(int id) {
  if (dp[id] != -1)
    return dp[id];
  return dp[id] = solve(A[id]) + 1;
}

bool iszero() {
  int R = dp[A[0]] + 1;
  if (K == 1) {
    for (int i = 0; i < N; i++)
      if (A[i] != 0)
        return false;
  } else if (R == 1) {
    for (int i = 1; i < N; i++)
      if (A[i] > K)
        return false;
  } else if (K <= R) {
    for (int i = 1; i < N; i++)
      if (dp[i] != K)
        return false;
  } else {
    for (int i = 1; i < N; i++) {
      if (K >= dp[i] && (K - dp[i]) % R == 0)
        continue;
      return false;
    }
  }
  return true;
}
int res;
int dfs(int id, int d) {
  if (id == 0)
    return dp[id] = 0;
  if (dp[id] + d <= K)
    return dp[id];
  if (d == K - 1 && A[id] != 0) {
    A[id] = 0;
    res++;
    return dp[id] = 1;
  }
  return dp[id] = dfs(A[id], d + 1) + 1;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    --A[i];
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    solve(i);
  if (iszero())
    cout << 0 << endl;
  else {
    res = 0;
    if (A[0])
      res++;
    A[0] = 0;
    vector<P> v;
    for (int i = 1; i < N; i++) {
      v.push_back(P(dp[i], i));
    }
    sort(v.rbegin(), v.rend());

    for (P t : v) {
      if (dp[t.second] <= K)
        continue;
      dfs(t.second, 0);
    }

    cout << res << endl;
  }
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S, T;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

mat G(1e+5, vec(0)); // 子の方へ向かう有向グラフ
vec depth(1e+5, 0);
void dfs(int v, int k) {
  depth[v] = k;
  for (int to : G[v])
    dfs(to, k + 1);
}

vector<bool> used(1e+5, false);
void dfs2(int v) {
  used[v] = true;
  for (int to : G[v])
    if (!used[to])
      dfs2(to);
}

int main() {
  cin >> N >> K;
  vec a(N), ord(N);
  ll ans = 0;
  rep(i, N) {
    cin >> a[i];
    --a[i];
    if (i == 0) {
      ans += a[i] != 0;
      a[i] = 0;
    } else {
      G[a[i]].push_back(i);
    }
    ord[i] = i;
  }
  dfs(0, 0);
  sort(ALL(ord), [&](int x, int y) { return depth[x] > depth[y]; });
  rep(i, N) {
    int id = ord[i];
    // cout<<id<<endl;
    if (!used[id]) {
      rep(j, K - 1) id = a[id];
      if (a[id] == 0)
        break;
      ++ans;
      dfs2(id);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S, T;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

mat G(1e+5, vec(0)); // 子の方へ向かう有向グラフ
vec depth(1e+5, 0);
void dfs(int v, int k) {
  depth[v] = k;
  for (int to : G[v])
    dfs(to, k + 1);
}

vector<bool> used(1e+5, false);
void dfs2(int v) {
  used[v] = true;
  for (int to : G[v])
    if (!used[to])
      dfs2(to);
}

int main() {
  cin >> N >> K;
  vec a(N), ord(N);
  ll ans = 0;
  rep(i, N) {
    cin >> a[i];
    --a[i];
    if (i == 0) {
      ans += a[i] != 0;
      a[i] = 0;
    } else {
      G[a[i]].push_back(i);
    }
    ord[i] = i;
  }
  dfs(0, 0);
  sort(ALL(ord), [&](int x, int y) { return depth[x] > depth[y]; });
  rep(i, N) {
    int id = ord[i];
    // cout<<id<<endl;
    if (!used[id]) {
      bool ng = false;
      rep(j, K - 1) {
        id = a[id];
        if (a[id] == 0) {
          ng = true;
          break;
        }
      }
      if (ng || a[id] == 0)
        break;
      ++ans;
      dfs2(id);
    }
  }
  cout << ans << endl;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX = 100010, inf = 2e9;

int n, k;
vector<int> G[MX];
int A[MX];
int ans = 0;

int dep[MX];
int D[MX];

void dfs1(int v, int now = 0) {
  dep[v] = now;
  for (int x : G[v])
    dfs1(x, now + 1);
}

int up(int x) {
  int m = min(n, k - 1);
  for (int i = 1; i <= m; i++)
    if (x == 1)
      break;
    else
      x = A[x];
  return x;
}

vector<pii> V;
bool done[MX];

void erase(int v) {
  done[v] = true;
  for (int x : G[v])
    erase(x);
}

void solve() {
  dfs1(1);
  for (int i = 1; i <= n; i++) {
    V.push_back({dep[i], i});
  }
  sort(V.begin(), V.end(), greater<pii>());
  for (pii &q : V) {
    int v = q.second;
    if (done[v])
      continue;
    done[v] = true;
    int p = up(v);
    erase(p);
    if (A[p] != 1)
      ans++;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  if (A[1] != 1)
    A[1] = 1, ans++;
  for (int i = 2; i <= n; i++)
    G[A[i]].push_back(i);

  solve();

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX = 100010, inf = 2e9;

int n, k;
vector<int> G[MX];
int A[MX];
int ans = 0;

int dep[MX];
int D[MX];

void dfs1(int v, int now = 0) {
  dep[v] = now;
  for (int x : G[v])
    dfs1(x, now + 1);
}

int up(int x) {
  int m = min(n, k - 1);
  for (int i = 1; i <= m; i++)
    if (x == 1)
      break;
    else
      x = A[x];
  return x;
}

vector<pii> V;
bool done[MX];

void erase(int v) {
  done[v] = true;
  for (int x : G[v])
    if (!done[x])
      erase(x);
}

void solve() {
  dfs1(1);
  for (int i = 1; i <= n; i++) {
    V.push_back({dep[i], i});
  }
  sort(V.begin(), V.end(), greater<pii>());
  for (pii &q : V) {
    int v = q.second;
    if (done[v])
      continue;
    done[v] = true;
    int p = up(v);
    erase(p);
    if (A[p] != 1)
      ans++;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  if (A[1] != 1)
    A[1] = 1, ans++;
  for (int i = 2; i <= n; i++)
    G[A[i]].push_back(i);

  solve();

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

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

using namespace std;

const int MAXN = 100005;
const int LOG = 19;

int n, k, sol;
int a[MAXN], dub[MAXN], par[LOG][MAXN], bio[MAXN];
vector<int> v[MAXN];
set<pair<int, int>> st;

void dfs(int x) {
  dub[x] = 1 + dub[par[0][x]];
  st.insert({-dub[x], x});
  for (auto sus : v[x])
    dfs(sus);
}

void precompute() {
  for (int i = 1; i <= n; i++) {
    par[0][i] = a[i];
  }
  par[0][1] = 1;
  for (int i = 1; i < LOG; i++) {
    for (int j = 1; j <= n; j++) {
      par[i][j] = par[i - 1][par[i - 1][j]];
    }
  }
}

int kth(int x, int k) {
  int pot = 0;
  while (k > 0) {
    if (k & 1)
      x = par[pot][x];
    k /= 2;
    pot++;
  }
  return x;
}

void brisi(int x) {
  if (bio[x])
    return;
  bio[x] = 1;
  st.erase({-dub[x], x});
  for (auto sus : v[x]) {
    brisi(sus);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > 1)
      v[a[i]].push_back(i);
  }
  if (a[1] != 1)
    sol++;
  precompute();
  dfs(1);
  while (!st.empty()) {
    int x = st.begin()->second;
    if (kth(x, k) == 1)
      break;
    sol++;
    brisi(kth(x, k - 1));
  }
  cout << sol;
  return 0;
}


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

using namespace std;

const int MAXN = 100005;
const int LOG = 19;

int n, k, sol;
int a[MAXN], dub[MAXN], par[LOG][MAXN], bio[MAXN];
vector<int> v[MAXN];
set<pair<int, int>> st;

void dfs(int x) {
  dub[x] = 1 + dub[par[0][x]];
  st.insert({-dub[x], x});
  for (auto sus : v[x])
    dfs(sus);
}

void precompute() {
  for (int i = 1; i <= n; i++) {
    par[0][i] = a[i];
  }
  par[0][1] = 1;
  for (int i = 1; i < LOG; i++) {
    for (int j = 1; j <= n; j++) {
      par[i][j] = par[i - 1][par[i - 1][j]];
    }
  }
}

int kth(int x, int k) {
  if (k > n)
    return 1;
  int pot = 0;
  while (k > 0) {
    if (k & 1)
      x = par[pot][x];
    k /= 2;
    pot++;
  }
  return x;
}

void brisi(int x) {
  if (bio[x])
    return;
  bio[x] = 1;
  st.erase({-dub[x], x});
  for (auto sus : v[x]) {
    brisi(sus);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > 1)
      v[a[i]].push_back(i);
  }
  if (a[1] != 1)
    sol++;
  precompute();
  dfs(1);
  while (!st.empty()) {
    int x = st.begin()->second;
    if (kth(x, k) == 1)
      break;
    sol++;
    brisi(kth(x, k - 1));
  }
  cout << sol;
  return 0;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;

int n, k, cnt, first[maxn], ans, fa[maxn], d[maxn], g[maxn][25];
struct Edge {
  int to, next;
} e[maxn << 1];
bool vis[maxn];
struct node {
  int id, d;
} dot[maxn];

void Dfs(int u, int f) {
  fa[u] = f;
  d[u] = d[f] + 1;
  for (int i = first[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == f)
      continue;
    Dfs(v, u);
  }
}

void Dfs2(int u) {
  vis[u] = 1;
  for (int i = first[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa[u])
      continue;
    Dfs2(v);
  }
}

bool cmp(node a, node b) { return a.d > b.d; }

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cin >> n >> k;
  rep(i, 1, n) {
    int x;
    cin >> x;
    if (i == 1) {
      if (x != 1)
        ans++;
      continue;
    }
    add(i, x);
    add(x, i);
    g[i][0] = x;
  }
  g[1][0] = 1;
  d[0] = -1;
  Dfs(1, 0);
  rep(j, 1, 20) rep(i, 1, n) g[i][j] = g[g[i][j - 1]][j - 1];
  rep(i, 1, n) dot[i] = (node){i, d[i]};
  sort(dot + 1, dot + n + 1, cmp);
  rep(i, 1, n) {
    if (vis[dot[i].id])
      continue;
    if (dot[i].d <= k)
      continue;
    ans++;
    int maxd = k - 1, u = dot[i].id;
    per(i, 20, 0) if (maxd >= 1 << i) {
      maxd -= 1 << i;
      u = g[u][i];
    }
    Dfs2(u);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;

int n, k, cnt, first[maxn], ans, fa[maxn], d[maxn], g[maxn][25];
struct Edge {
  int to, next;
} e[maxn << 1];
bool vis[maxn];
struct node {
  int id, d;
} dot[maxn];

void Dfs(int u, int f) {
  fa[u] = f;
  d[u] = d[f] + 1;
  for (int i = first[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == f)
      continue;
    Dfs(v, u);
  }
}

void Dfs2(int u) {
  if (vis[u])
    return;
  vis[u] = 1;
  for (int i = first[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (v == fa[u])
      continue;
    Dfs2(v);
  }
}

bool cmp(node a, node b) { return a.d > b.d; }

void add(int u, int v) {
  e[++cnt] = (Edge){v, first[u]};
  first[u] = cnt;
}

int main() {
  cin >> n >> k;
  rep(i, 1, n) {
    int x;
    cin >> x;
    if (i == 1) {
      if (x != 1)
        ans++;
      continue;
    }
    add(i, x);
    add(x, i);
    g[i][0] = x;
  }
  g[1][0] = 1;
  d[0] = -1;
  Dfs(1, 0);
  rep(j, 1, 20) rep(i, 1, n) g[i][j] = g[g[i][j - 1]][j - 1];
  rep(i, 1, n) dot[i] = (node){i, d[i]};
  sort(dot + 1, dot + n + 1, cmp);
  rep(i, 1, n) {
    if (vis[dot[i].id])
      continue;
    if (dot[i].d <= k)
      continue;
    ans++;
    int maxd = k - 1, u = dot[i].id;
    per(i, 20, 0) if (maxd >= 1 << i) {
      maxd -= 1 << i;
      u = g[u][i];
    }
    Dfs2(u);
  }
  cout << ans << endl;
}
Problem p04008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  V<> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i], p[i]--;
  int res = !!p[0];
  p[0] = 0;
  VV<> ch(n);
  for (int v = 1; v < n; v++)
    ch[p[v]].push_back(v);
  V<pair<int, int>> d(n);
  queue<int> que;
  d[0] = {0, 0};
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int w : ch[v]) {
      d[w] = {d[v].first + 1, w};
      que.push(w);
    }
  }
  sort(d.rbegin(), d.rend());
  V<> invd(n);
  for (int i = 0; i < n; i++)
    invd[d[i].second] = i;
  for (int i = 0; i < n; i++)
    if (d[i].first > k) {
      res++;
      int s = d[i].second;
      for (int _ = 0; _ < k - 1; _++)
        s = p[s];
      d[invd[s]].first = 0;
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int w : ch[v]) {
          d[invd[w]].first = 0;
          que.push(w);
        }
      }
    }
  cout << res << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  V<> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i], p[i]--;
  int res = !!p[0];
  p[0] = 0;
  VV<> ch(n);
  for (int v = 1; v < n; v++)
    ch[p[v]].push_back(v);
  V<pair<int, int>> d(n);
  queue<int> que;
  d[0] = {0, 0};
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int w : ch[v]) {
      d[w] = {d[v].first + 1, w};
      que.push(w);
    }
  }
  sort(d.rbegin(), d.rend());
  V<> invd(n);
  for (int i = 0; i < n; i++)
    invd[d[i].second] = i;
  for (int i = 0; i < n; i++)
    if (d[i].first > k) {
      res++;
      int s = d[i].second;
      for (int _ = 0; _ < k - 1; _++)
        s = p[s];
      d[invd[s]].first = 0;
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int w : ch[v])
          if (d[invd[w]].first) {
            d[invd[w]].first = 0;
            que.push(w);
          }
      }
    }
  cout << res << '\n';
}
Problem p04011 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main() {
  int N, K, X, Y;
  long long int sum;
  while (scanf("%d"
               "%d"
               "%d"
               "%d",
               &N, &K, &X, &Y)) {
    if (N > K)
      printf("%lld\n", sum = K * X + (N - K) * Y);
    else if (N <= K)
      printf("%lld\n", sum = N * X);
  }

  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int N, K, X, Y;
  long long int sum;
  scanf("%d"
        "%d"
        "%d"
        "%d",
        &N, &K, &X, &Y);
  if (N > K)
    printf("%lld\n", sum = K * X + (N - K) * Y);
  else if (N <= K)
    printf("%lld\n", sum = N * X);
  return 0;
}
Problem p04011 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  int N, K, X, Y;
  int ans = 0;
  int i = 1;
  cin >> N >> K >> X >> Y;

  while (i <= K && i <= N) {
    ans += X;
    i++;
  }

  while (i <= N) {
    ans += Y;
  }

  cout << ans << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N, K, X, Y;
  int ans = 0;
  int i = 1;
  cin >> N >> K >> X >> Y;

  while (i <= K && i <= N) {
    ans += X;
    i++;
  }

  while (i <= N) {
    ans += Y;
    i++;
  }

  cout << ans << endl;
}
Problem p04011 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int x, y, n, k;
  int count = 0;
  scanf("%d%d%d%d", n, k, x, y);
  for (int i = 0; i < k; i++) {
    count = count + x;
  }
  for (int j = k; j <= n; j++) {
    count += y;
  }
  printf("%d\n", count);
}

Fixed submission:
#include <stdio.h>
int main() {
  int x, y, n, k;
  int count = 0;
  scanf("%d%d%d%d", &n, &k, &x, &y);
  if (n > k) {
    for (int i = 0; i < k; i++) {
      count = count + x;
    }
    for (int j = k; j < n; j++) {
      count += y;
    }
  } else {
    for (int i = 0; i < n; i++) {
      count += x;
    }
  }
  printf("%d\n", count);
}
Problem p04011 (C++)

Buggy submission (status: Runtime Error):
#include <cstdint>
#include <cstdio>

using namespace std;

int main() {
  int32_t n, k, x, y;
  int32_t payment;

  scanf("%d\n%d\n%d\n%d\n", n, k, x, y);

  payment = n >= k ? k * x : n * x;
  payment += n >= k ? (n - k) * y : 0;

  printf("%d\n", payment);
}


Fixed submission:
#include <cstdint>
#include <cstdio>

using namespace std;

int main() {
  int32_t n, k, x, y;
  int32_t payment;

  scanf("%d\n%d\n%d\n%d\n", &n, &k, &x, &y);

  payment = n >= k ? k * x : n * x;
  payment += n >= k ? (n - k) * y : 0;

  printf("%d\n", payment);
}
Problem p04011 (C++)

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

int main() {
  int N, K, X, Y;
  scanf("%d %d %d %d", N, K, X, Y);
  if (N <= K) {
    printf("%d\n", N * X);
  } else {
    printf("%d\n", K * X + (N - K) * Y);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int N, K, X, Y;
  scanf("%d%d%d%d", &N, &K, &X, &Y);
  if (N <= K) {
    printf("%d\n", N * X);
  } else {
    printf("%d\n", K * X + (N - K) * Y);
  }
  return 0;
}
Problem p04011 (Python)

Buggy submission (status: Runtime Error):
# abc044_a
# https://atcoder.jp/contests/abc044/tasks/abc044_a

# 最初のK泊までは、X円
# K＋1泊目以降は、Y円

# N泊連続で宿泊した際の宿泊費

# 入力
n, k, x, y = map(int, input().split())

# 処理

# x円とy円
if n > k:
    answer = k * x + (n - k) * y
# x円のみ
else:
    answer = n * x

print(answer)


Fixed submission:
# abc044_a
# https://atcoder.jp/contests/abc044/tasks/abc044_a

# 最初のK泊までは、X円
# K＋1泊目以降は、Y円

# N泊連続で宿泊した際の宿泊費

# 入力
n = int(input())
k = int(input())
x = int(input())
y = int(input())

# 処理

# x円とy円
if n > k:
    answer = k * x + (n - k) * y
# x円のみ
else:
    answer = n * x

print(answer)
Problem p04012 (Python)

Buggy submission (status: Runtime Error):
W = input()

r = {}
for w in W:
    r.setdefault(w, 0)
    r[w] += 1

print("Yes" if len(list(filter(lambda x: x % 2 == 1, r))) == 0 else "No")


Fixed submission:
W = input()

r = {}
for w in W:
    r.setdefault(w, 0)
    r[w] += 1

print("Yes" if len(list(filter(lambda x: x % 2 == 1, r.values()))) == 0 else "No")
Problem p04012 (Python)

Buggy submission (status: Runtime Error):
from collectiosn import Counter

c = Counter(input())
valid = True
for key, value in c.items():
    if value % 2 == 1:
        valid = False
        break
if valid:
    print("Yes")
else:
    print("No")


Fixed submission:
from collections import Counter

c = Counter(input())
valid = True
for key, value in c.items():
    if value % 2 == 1:
        valid = False
        break
if valid:
    print("Yes")
else:
    print("No")
Problem p04012 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <random>
#include <string>
#include <vector>
using namespace std;
#define MK make_pair
#define PB push_back
#define VI vector<int>
#define VII vector<int, int>
#define MSI map<string, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define W1                                                                     \
  while (1)                                                                    \
    ;
const int MOD = 1000000007;
int main() {
  string S;
  string abc;
  abc = "abcdefghijklmnopqrstuvwxyz";
  VI count;
  REP(i, S.size()) {
    count.resize(26);
    REP(j, 26) {
      if (abc.at(j) == S.at(i))
        count.at(j)++;
    }
  }
  REP(i, 26) {
    if (count.at(i) % 2 != 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <random>
#include <string>
#include <vector>
using namespace std;
#define MK make_pair
#define PB push_back
#define VI vector<int>
#define VII vector<int, int>
#define MSI map<string, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define W1                                                                     \
  while (1)                                                                    \
    ;
const int MOD = 1000000007;
int main() {
  string S;
  string abc;
  abc = "abcdefghijklmnopqrstuvwxyz";
  VI count;
  cin >> S;
  REP(i, S.size()) {
    count.resize(26);
    REP(j, 26) {
      if (abc.at(j) == S.at(i))
        count.at(j)++;
    }
  }
  REP(i, 26) {
    if (count.at(i) % 2 != 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}