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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

struct dish {
  ll x, y;

  dish(ll tx = 0, ll ty = 0) {
    x = tx;
    y = ty;
  }

  void read() { cin >> x >> y; }

  bool operator<(const dish &d) const { return (x + y >= d.x + d.y); }
};

#define MAXN (1 << 17)

dish a[MAXN];
int n;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    a[i].read();
  }
  sort(a, a + n);
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    if (i & 1) {
      ret -= a[i].y;
    } else {
      ret += a[i].x;
    }
  }
  cout << ret << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

struct dish {
  ll x, y;

  dish(ll tx = 0, ll ty = 0) {
    x = tx;
    y = ty;
  }

  void read() { cin >> x >> y; }

  bool operator<(const dish &d) const { return (x + y > d.x + d.y); }
};

#define MAXN (1 << 17)

dish a[MAXN];
int n;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    a[i].read();
  }
  sort(a, a + n);
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    if (i & 1) {
      ret -= a[i].y;
    } else {
      ret += a[i].x;
    }
  }
  cout << ret << endl;

  return 0;
}
Problem p03141 (C++)

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

#define ll long long

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define INF 1000000000000000000
ll ar[100007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double pi = 3.14159265358979323846;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll ans = 0;
  vector<ll> v1, v2, v3;

  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    v1.pb(x + y);
    ans -= (y);
  }
  // debug(ans);
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  rep(i, n) {
    if (i % 2 == 0)
      ans += v1[i];
  }
  cout << ans;
  return 0;
}

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

#define ll long long

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define INF 1000000000000000000
ll ar[100007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double pi = 3.14159265358979323846;
  ll n;
  cin >> n;
  ll ans = 0;
  vector<ll> v1, v2, v3;

  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    v1.pb(x + y);
    ans -= (y);
  }
  // debug(ans);
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  rep(i, n) {
    if (i % 2 == 0)
      ans += v1[i];
  }
  cout << ans;
  return 0;
}
Problem p03141 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  // ll a[n],b[n];
  ll ans = 0;
  pair<ll, int> s[n];
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    // a--;b--;
    s[i] = {a + b, i};
    ans -= b;
  }
  sort(s, s + n);
  reverse(s, s + n);
  for (int i = 0; i < n; i += 2) {
    ans += s[i].first;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
  /*
  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif */
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  // ll a[n],b[n];
  ll ans = 0;
  pair<ll, int> s[n];
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    // a--;b--;
    s[i] = {a + b, i};
    ans -= b;
  }
  sort(s, s + n);
  reverse(s, s + n);
  for (int i = 0; i < n; i += 2) {
    ans += s[i].first;
  }
  cout << ans;
}
Problem p03141 (C++)

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

using namespace std;

int n;
int a[10010], b[10010];
long long s[10010];
vector<pair<int, int>> V;
long long answer = 0;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    s[i] = a[i] + b[i];
    V.push_back(make_pair(s[i], i));
  }
  sort(V.begin(), V.end());
  while (V.size() != 0) {
    answer += a[V.back().second];
    V.pop_back();
    if (V.size() != 0) {
      answer -= b[V.back().second];
      V.pop_back();
    }
  }
  cout << answer << endl;
}

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

using namespace std;

int n;
long long a[100100], b[100100];
long long s[100100];
vector<pair<int, int>> V;
long long answer = 0;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    s[i] = a[i] + b[i];
    V.push_back(make_pair(s[i], i));
  }
  sort(V.begin(), V.end());
  while (V.size() != 0) {
    answer += a[V.back().second];
    V.pop_back();
    if (V.size() != 0) {
      answer -= b[V.back().second];
      V.pop_back();
    }
  }
  cout << answer << endl;
}
Problem p03142 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = (int(x) - 1 for x in input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs(s):
    global ts
    global used
    used[s] = True
    for t in g[s]:
        if not used[t]:
            dfs(t)
    ts.append(s)


def tsort():
    global ts
    for i in range(N):
        dfs(i)
    ts = ts[::-1]


used = [False] * N
ts = []
tsort()

mp = [None] * N
for i, x in enumerate(ts):
    mp[x] = i

ans = [0] * N
for t in ts[1:]:
    if rg[t]:
        ans[t] = ts[max(mp[s] for s in rg[t])] + 1

for x in ans:
    print(x)


Fixed submission:
#!/usr/bin/env python3
import sys

sys.setrecursionlimit(100000)
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = (int(x) - 1 for x in input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs(s):
    global ts
    global used
    used[s] = True
    for t in g[s]:
        if not used[t]:
            dfs(t)
    ts.append(s)


def tsort():
    global ts
    for i in range(N):
        dfs(i)
    ts = ts[::-1]


used = [False] * N
ts = []
tsort()

mp = [None] * N
for i, x in enumerate(ts):
    mp[x] = i

ans = [0] * N
for t in ts[1:]:
    if rg[t]:
        ans[t] = ts[max(mp[s] for s in rg[t])] + 1

for x in ans:
    print(x)
Problem p03142 (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 v, e;
const int MAX_N = 10010;
// 隣接リスト
vector<int> to[MAX_N];
vector<int> from[MAX_N];
bool used[MAX_N];
// トポロジカルソートされた数列
vector<int> tps;

void dfs(int u) {
  if (used[u])
    return;
  used[u] = true;
  for (int i : to[u])
    dfs(i);
  // 帰りがけ順で追加
  tps.push_back(u);
}

void tsort() {
  for (int i = 0; i < v; ++i)
    dfs(i);
  reverse(tps.begin(), tps.end());
}

int main(void) {
  int n, m;
  cin >> n >> m;
  v = n;
  e = n + m - 1;
  for (int i = 0; i < e; ++i) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    from[t].push_back(s);
  }

  tsort();
  int rvs[v];
  // rep(i,n)cout << tps[i] << endl;
  rep(i, v) {
    int t = tps[i];
    rvs[t] = i;
  }
  // rep(i,n)cout << rvs[i] << endl;
  int ans[v];
  rep(i, v) {
    if (i == tps[0]) {
      cout << 0 << endl;
      continue;
    }
    int mx = 0;
    for (int j : from[i]) {
      mx = max(rvs[j], mx);
      // cout << rvs[j] << endl;
    }
    cout << tps[mx] + 1 << endl;
  }
  return 0;
}


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

// 頂点数、変数
int v, e;
const int MAX_N = 100100;
// 隣接リスト
vector<int> to[MAX_N];
vector<int> from[MAX_N];
bool used[MAX_N];
// トポロジカルソートされた数列
vector<int> tps;

void dfs(int u) {
  if (used[u])
    return;
  used[u] = true;
  for (int i : to[u])
    dfs(i);
  // 帰りがけ順で追加
  tps.push_back(u);
}

void tsort() {
  for (int i = 0; i < v; ++i)
    dfs(i);
  reverse(tps.begin(), tps.end());
}

int main(void) {
  int n, m;
  cin >> n >> m;
  v = n;
  e = n + m - 1;
  for (int i = 0; i < e; ++i) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    from[t].push_back(s);
  }

  tsort();
  int rvs[v];
  // rep(i,n)cout << tps[i] << endl;
  rep(i, v) {
    int t = tps[i];
    rvs[t] = i;
  }
  // rep(i,n)cout << rvs[i] << endl;
  int ans[v];
  rep(i, v) {
    if (i == tps[0]) {
      cout << 0 << endl;
      continue;
    }
    int mx = 0;
    for (int j : from[i]) {
      mx = max(rvs[j], mx);
      // cout << rvs[j] << endl;
    }
    cout << tps[mx] + 1 << endl;
  }
  return 0;
}
Problem p03142 (C++)

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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))

int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  //    vector<int> a(n),b(n),parent(n),isroot(n),pcnt(n);
  vector<int> a(n + 5), b(n + 5), parent(n + 5), isroot(n + 5), pcnt(n + 5);
  vector<vector<int>> a2b(n);

  for (int i = 0; i < n - 1 + m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    a2b[a[i]].push_back(b[i]);
    isroot[b[i]] = -1;
    pcnt[b[i]]++; // oya nannnin iruka
  }
  int root;

  for (int i = 0; i < n; i++) {
    if (isroot[i] == 0) {
      root = i;
      parent[i] = 0;
    } else {
      parent[i] = -1;
    }
  }

  queue<int> qa;
  qa.push(root);
  while (qa.size() > 0) {
    int nowpar = qa.front();
    qa.pop();

    for (int i = 0; i < a2b[nowpar].size(); i++) {
      int nowchi = a2b[nowpar][i];
      pcnt[nowchi]--;

      if (pcnt[nowchi] == 0) {
        parent[nowchi] = nowpar + 1;
        qa.push(nowchi);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << parent[i] << endl;
  }

  return 0;
}

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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))

int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  //    vector<int> a(n),b(n),parent(n),isroot(n),pcnt(n);
  vector<int> a(100100), b(100100), parent(100100), isroot(100100),
      pcnt(100100);
  vector<vector<int>> a2b(100100);

  for (int i = 0; i < n - 1 + m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    a2b[a[i]].push_back(b[i]);
    isroot[b[i]] = -1;
    pcnt[b[i]]++; // oya nannnin iruka
  }
  int root;

  for (int i = 0; i < n; i++) {
    if (isroot[i] == 0) {
      root = i;
      parent[i] = 0;
    } else {
      parent[i] = -1;
    }
  }

  queue<int> qa;
  qa.push(root);
  while (qa.size() > 0) {
    int nowpar = qa.front();
    qa.pop();

    for (int i = 0; i < a2b[nowpar].size(); i++) {
      int nowchi = a2b[nowpar][i];
      pcnt[nowchi]--;

      if (pcnt[nowchi] == 0) {
        parent[nowchi] = nowpar + 1;
        qa.push(nowchi);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << parent[i] << endl;
  }

  return 0;
}
Problem p03142 (C++)

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N + 1);
  vector<int> PC(N + 1, 0);
  for (int i = 0; i < N + M - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a].emplace_back(b);
    PC[b]++;
  }
  vector<int> P(N);
  queue<int> que;
  for (int i = 1; i < N + 1; i++) {
    if (PC[i] == 0) {
      que.push(i);
      P[i] = 0;
      break;
    }
  }
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int v : G[u]) {
      if (PC[v] > 1) {
        PC[v]--;
      } else {
        P[v] = u;
        que.push(v);
      }
    }
  }
  for (int i = 1; i < N + 1; i++) {
    cout << P[i] << '\n';
  }
  return 0;
}


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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N + 1);
  vector<int> PC(N + 1, 0);
  for (int i = 0; i < N + M - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a].emplace_back(b);
    PC[b]++;
  }
  vector<int> P(N + 1);
  queue<int> que;
  for (int i = 1; i < N + 1; i++) {
    if (PC[i] == 0) {
      que.push(i);
      P[i] = 0;
      break;
    }
  }
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int v : G[u]) {
      if (PC[v] > 1) {
        PC[v]--;
      } else {
        P[v] = u;
        que.push(v);
      }
    }
  }
  for (int i = 1; i < N + 1; i++) {
    cout << P[i] << '\n';
  }
  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

void dfs(const vector<vecint> &g, int s, vector<int> &nodes) {
  for (int v : g[s]) {
    dfs(g, v, nodes);
  }
  for (int v : g[s]) {
    if (nodes[v] == -1) {
      nodes[v] = s;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vecint> g(n);
  vecint d(n);
  REP(i, n + m - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    ++d[b];
  }
  int root = 0;
  REP(i, n) {
    if (d[i] == 0)
      root = i;
  }
  vecint nodes(n, -1);
  dfs(g, root, nodes);
  REP(i, n) { cout << nodes[i] + 1 << endl; }
  return 0;
}


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

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

void dfs(const vector<vecint> &g, int s, vector<int> &nodes) {
  for (int v : g[s]) {
    if (nodes[v] == -1) {
      dfs(g, v, nodes);
    }
  }
  for (int v : g[s]) {
    if (nodes[v] == -1) {
      nodes[v] = s;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vecint> g(n);
  vecint d(n);
  REP(i, n + m - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    ++d[b];
  }
  int root = 0;
  REP(i, n) {
    if (d[i] == 0)
      root = i;
  }
  vecint nodes(n, -1);
  dfs(g, root, nodes);
  REP(i, n) { cout << nodes[i] + 1 << endl; }
  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

// 頂点数、変数
int v, e;
const int MAX_N = 10010;
// 隣接リスト
vector<int> g[MAX_N];
bool used[MAX_N];
// トポロジカルソートされた数列
vector<int> ans;

void dfs(int u) {
  if (used[u])
    return;
  used[u] = true;
  for (auto &i : g[u])
    dfs(i);
  // 帰りがけ順で追加
  ans.push_back(u);
}

void tsort() {
  for (int i = 0; i < v; ++i)
    dfs(i);
  reverse(ans.begin(), ans.end());
}

int ans2[100100];
vector<int> G2[100100];
int order[100100];

int ansi(int i) {
  int maxorder = -1;
  for (auto p : G2[i]) {
    maxorder = max(maxorder, order[p]);
  }
  if (maxorder == -1)
    return 0;
  return ans[maxorder] + 1;
}

signed main(void) {
  cin >> v >> e;
  e += v - 1;
  for (int i = 0; i < e; ++i) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    g[s].push_back(t);
    G2[t].push_back(s);
  }

  tsort();

  for (int i = 0; i < v; ++i) {
    order[ans[i]] = i;
  }
  for (int i = 0; i < v; ++i) {
    cout << ansi(i) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

// 頂点数、変数
int v, e;
const int MAX_N = 100010;
// 隣接リスト
vector<int> g[MAX_N];
bool used[MAX_N];
// トポロジカルソートされた数列
vector<int> ans;

void dfs(int u) {
  if (used[u])
    return;
  used[u] = true;
  for (auto &i : g[u])
    dfs(i);
  // 帰りがけ順で追加
  ans.push_back(u);
}

void tsort() {
  for (int i = 0; i < v; ++i)
    dfs(i);
  reverse(ans.begin(), ans.end());
}

int ans2[100100];
vector<int> G2[100100];
int order[100100];

int ansi(int i) {
  int maxorder = -1;
  for (auto p : G2[i]) {
    maxorder = max(maxorder, order[p]);
  }
  if (maxorder == -1)
    return 0;
  return ans[maxorder] + 1;
}

signed main(void) {
  cin >> v >> e;
  e += v - 1;
  for (int i = 0; i < e; ++i) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    g[s].push_back(t);
    G2[t].push_back(s);
  }

  tsort();

  for (int i = 0; i < v; ++i) {
    order[ans[i]] = i;
  }
  for (int i = 0; i < v; ++i) {
    cout << ansi(i) << endl;
  }

  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline ll min(ll x, ll y) { return x < y ? x : y; }
inline ll max(ll x, ll y) { return x > y ? x : y; }
const int Maxn = 1e5 + 5;
int n, m, cnt, fir[Maxn], ind[Maxn], f[Maxn];
struct {
  int to, last;
} e[Maxn];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
  ind[v]++;
}
void BFS(int x) {
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      f[nx] = x;
      q.push(nx);
    }
  }
}
int main() {
  int a, b;
  cin >> n >> m;
  for (int i = 1; i < n + m; i++) {
    scanf("%d%d", &a, &b);
    con(a, b);
  }
  for (int i = 1; i <= n; i++)
    if (!ind[i]) {
      BFS(i);
      break;
    }
  for (int i = 1; i <= n; i++)
    printf("%d\n", f[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline ll min(ll x, ll y) { return x < y ? x : y; }
inline ll max(ll x, ll y) { return x > y ? x : y; }
const int Maxn = 1e5 + 5;
int n, m, cnt, fir[Maxn], ind[Maxn], f[Maxn];
struct {
  int to, last;
} e[Maxn];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
  ind[v]++;
}
void BFS(int x) {
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      f[nx] = x;
      ind[nx]--;
      if (!ind[nx])
        q.push(nx);
    }
  }
}
int main() {
  int a, b;
  cin >> n >> m;
  for (int i = 1; i < n + m; i++) {
    scanf("%d%d", &a, &b);
    con(a, b);
  }
  for (int i = 1; i <= n; i++)
    if (!ind[i]) {
      BFS(i);
      break;
    }
  for (int i = 1; i <= n; i++)
    printf("%d\n", f[i]);
  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M;
  cin >> N >> M;
  vvll G(N, vll(0));
  vll incount(N, 0);
  for (int i = 0; i < N - 1 + M; i++) {
    ll A, B;
    cin >> A >> B;
    A--;
    B--;
    G.at(A).push_back(B);
    incount.at(B)++;
  }
  int root;
  for (int i = 0; i < N; i++) {
    if (incount.at(i) == 0) {
      root = i;
      break;
    }
  }
  queue<int> que;
  que.push(root);
  vll num(N, -1);
  num.at(root) = 0;
  vll par(N, -1);
  par.at(root) = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < sz(G.at(v)); i++) {
      int next = G.at(v).at(i);
      if (num.at(v) + 1 > num.at(next)) {
        num.at(next) = num.at(v) + 1;
        par.at(next) = v + 1;
        que.push(next);
      }
    }
  }
  for (auto x : par)
    cout << x << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M;
  cin >> N >> M;
  vvll G(N, vll(0));
  vll incount(N, 0);
  for (int i = 0; i < N - 1 + M; i++) {
    ll A, B;
    cin >> A >> B;
    A--;
    B--;
    G.at(A).push_back(B);
    incount.at(B)++;
  }
  int root;
  for (int i = 0; i < N; i++) {
    if (incount.at(i) == 0) {
      root = i;
      break;
    }
  }
  queue<int> que;
  que.push(root);
  vll num(N, -1);
  num.at(root) = 0;
  vll par(N, -1);
  par.at(root) = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < sz(G.at(v)); i++) {
      int next = G.at(v).at(i);
      incount.at(next)--;
      if (incount.at(next) == 0) {
        num.at(next) = num.at(v) + 1;
        par.at(next) = v + 1;
        que.push(next);
      }
    }
  }
  for (auto x : par)
    cout << x << endl;
}
Problem p03142 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF 1e9
#define MOD 1000000007
#define ll long long
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
typedef unsigned long long ull;
typedef pair<ll, int> pr;

vector<int> to[11111], from[11111];
bool used[11111];
int takasa[11111];
deque<int> tpsort;
void dfs(int i) {
  if (used[i]) {
    return;
  }
  used[i] = true;
  for (auto &itr : to[i]) {
    dfs(itr);
  }
  tpsort.push_front(i);
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int nm = n + m - 1;
  int a, b;
  for (int i = 0; i < nm; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    from[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    dfs(i);
  }
  int t = 0;
  for (auto &itr : tpsort) {
    takasa[itr] = t;
    t++;
  }
  // for(int i=0;i<n;i++){
  //     cout<<takasa[i]<<" ";
  // }
  //    cout<<endl;
  for (int i = 0; i < n; i++) {
    if (from[i].empty()) {
      cout << 0 << endl;
      //        cout<<"emp"<<endl;
      continue;
    }
    int ans = from[i][0];
    for (int j = 1; j < from[i].size(); j++) {
      if (takasa[from[i][j]] > takasa[ans]) {
        ans = from[i][j];
      }
    }
    cout << ans + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF 1e9
#define MOD 1000000007
#define ll long long
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
typedef unsigned long long ull;
typedef pair<ll, int> pr;

vector<int> to[111111], from[111111];
bool used[111111];
int takasa[111111];
deque<int> tpsort;
void dfs(int i) {
  if (used[i]) {
    return;
  }
  used[i] = true;
  for (auto &itr : to[i]) {
    dfs(itr);
  }
  tpsort.push_front(i);
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int nm = n + m - 1;
  int a, b;
  for (int i = 0; i < nm; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    from[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    dfs(i);
  }
  int t = 0;
  for (auto &itr : tpsort) {
    takasa[itr] = t;
    t++;
  }
  // for(int i=0;i<n;i++){
  //     cout<<takasa[i]<<" ";
  // }
  //    cout<<endl;
  for (int i = 0; i < n; i++) {
    if (from[i].empty()) {
      cout << 0 << endl;
      //        cout<<"emp"<<endl;
      continue;
    }
    int ans = from[i][0];
    for (int j = 1; j < from[i].size(); j++) {
      if (takasa[from[i][j]] > takasa[ans]) {
        ans = from[i][j];
      }
    }
    cout << ans + 1 << endl;
  }

  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <list>
#include <stdio.h>
using namespace std;

class Node {
public:
  int parentsCount = 0;
  list<int> children;

  int setParents = 0;
  int trueParent = 0;
  int trueDepth = 0;

  void setDepth(int parent, int depth) {
    if (trueDepth < depth) {
      trueDepth = depth;
      trueParent = parent;
    }
    setParents++;
  }

  int getDepth() {
    if (parentsCount > setParents) {
      return -1;
    }
    return trueDepth;
  }
};

int getRoot(Node *nodes, int size) {
  for (int i = 1; i < size; i++) {
    if (nodes[i].parentsCount == 0) {
      return i;
    }
  }
  return -1;
}

int main(void) {
  Node *nodes = NULL;
  int N, M, a, b;
  scanf("%d%d", &N, &M);
  nodes = new Node[N + 1];

  for (int i = 0; i < N + M - 1; i++) {
    scanf("%d%d", &a, &b);
    nodes[b].parentsCount++;
    nodes[a].children.push_front(b);
  }

  list<int> list;
  list.push_front(getRoot(nodes, N + 1));

  while (list.size() > 0) {
    int target = list.front();
    list.pop_front();
    int depth = nodes[target].getDepth();

    for (const int x : nodes[target].children) {
      nodes[x].setDepth(target, depth + 1);
      if (nodes[x].getDepth() != -1) {
        list.push_front(x);
      }
    }
  }

  for (int i = 1; i < N + 1; i++) {
    printf("%d\n", nodes[i].trueParent);
  }
}


Fixed submission:
#include <list>
#include <stdio.h>
using namespace std;

class Node {
public:
  int parentsCount = 0;
  list<int> children;

  int setParents = 0;
  int trueParent = 0;
  int trueDepth = 0;

  void setDepth(int parent, int depth) {
    if (trueDepth < depth) {
      trueDepth = depth;
      trueParent = parent;
    }
    setParents++;
  }

  int getDepth() {
    if (parentsCount > setParents) {
      return -1;
    }
    return trueDepth;
  }
};

int getRoot(Node *nodes, int size) {
  for (int i = 1; i < size; i++) {
    if (nodes[i].parentsCount == 0) {
      return i;
    }
  }
  return -1;
}

int main(void) {
  Node *nodes = NULL;
  int N, M, a, b;
  scanf("%d%d", &N, &M);
  nodes = new Node[N + 1];

  for (int i = 0; i < N + M - 1; i++) {
    scanf("%d%d", &a, &b);
    nodes[b].parentsCount++;
    nodes[a].children.push_front(b);
  }

  list<int> list;
  list.push_front(getRoot(nodes, N + 1));

  while (list.size() > 0) {
    int target = list.front();
    list.pop_front();
    int depth = nodes[target].getDepth();

    for (const int x : nodes[target].children) {
      nodes[x].setDepth(target, depth + 1);
      if (nodes[x].getDepth() != -1 && nodes[x].children.size() > 0) {
        list.push_front(x);
      }
    }
  }

  for (int i = 1; i < N + 1; i++) {
    printf("%d\n", nodes[i].trueParent);
  }
}
Problem p03142 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

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

const ll INF = 1e9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<set<ll>> from(n);
  vector<set<ll>> to(n);
  rep(i, n + m - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    from[b].insert(a);
    to[a].insert(b);
  }
  priority_queue<pll> PQ;
  vector<ll> ans(n);
  rep(i, n) { PQ.push({n - from[i].size(), i}); }
  ll p = 0;
  pll st = PQ.top();
  ans[st.fi] = 0;
  ll cnt = 1;
  while (cnt < n) {
    pll x = PQ.top();
    PQ.pop();
    p = x.fi;
    for (auto c : to[x.se]) {
      if (from[c].size() == 1) {
        ans[c] = x.se + 1;
        cnt++;
        PQ.push({n, c});
      }
      from[c].erase(x.se);
    }
  }
  rep(i, n) { cout << ans[i] << endl; }
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

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

const ll INF = 1e9 + 7;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<set<ll>> from(n);
  vector<set<ll>> to(n);
  rep(i, n + m - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    from[b].insert(a);
    to[a].insert(b);
  }
  priority_queue<pll> PQ;
  vector<ll> ans(n);
  rep(i, n) { PQ.push({n - from[i].size(), i}); }
  ll p = 0;
  pll st = PQ.top();
  ans[st.se] = 0;
  ll cnt = 1;
  while (cnt < n) {
    pll x = PQ.top();
    PQ.pop();
    p = x.fi;
    for (auto c : to[x.se]) {
      if (from[c].size() == 1) {
        ans[c] = x.se + 1;
        cnt++;
        PQ.push({n, c});
      }
      from[c].erase(x.se);
    }
  }
  rep(i, n) { cout << ans[i] << endl; }
}
Problem p03142 (C++)

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, M;
vector<int> pathes[100500];
vector<int> inv[100050];
int parent[100050];
int Rank[100050];

void bfs(int from) {
  if (inv[from].empty()) {
    Rank[from] = 1;
    parent[from] = 0;
    return;
  }
  int par = 0;
  int now = 0;
  for (int i = 0; i < inv[from].size(); i++) {
    int from_from = inv[from][i];
    bfs(from_from);
    if (now < Rank[from_from]) {
      now = Rank[from_from];
      par = from_from;
    }
  }
  parent[from] = par;
  Rank[from] = now + 1;
}

int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 1; i <= N - 1 + M; i++) {
    int a, b;
    cin >> a >> b;
    pathes[a].push_back(b);
    inv[b].push_back(a);
  }
  for (int i = 1; i <= N; i++) {
    bfs(i);
  }
  for (int i = 1; i <= N; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
int N, M;
vector<int> pathes[100500];
vector<int> inv[100050];
int parent[100050];
int Rank[100050];

void bfs(int from) {
  if (inv[from].empty()) {
    Rank[from] = 1;
    parent[from] = 0;
    return;
  }
  if (parent[from] != 0)
    return;
  int par = 0;
  int now = 0;
  for (int i = 0; i < inv[from].size(); i++) {
    int from_from = inv[from][i];
    bfs(from_from);
    if (now < Rank[from_from]) {
      now = Rank[from_from];
      par = from_from;
    }
  }
  parent[from] = par;
  Rank[from] = now + 1;
}

int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 1; i <= N - 1 + M; i++) {
    int a, b;
    cin >> a >> b;
    pathes[a].push_back(b);
    inv[b].push_back(a);
  }
  for (int i = 1; i <= N; i++) {
    bfs(i);
  }
  for (int i = 1; i <= N; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}
Problem p03142 (C++)

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

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  vector<int> cnt(N);
  rep(i, N - 1 + M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    ++cnt[b];
  }
  int root;
  rep(i, N) if (cnt[i] == 0) root = i;
  vector<int> par(N, -1);
  queue<int> que;
  que.push(root);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      que.push(nv);
      par[nv] = v;
    }
  }
  rep(i, N) cout << par[i] + 1 << endl;
}

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

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  vector<int> cnt(N);
  rep(i, N - 1 + M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(b);
    ++cnt[b];
  }
  int root;
  rep(i, N) if (cnt[i] == 0) root = i;
  vector<int> par(N, -1);
  queue<int> que;
  que.push(root);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : G[v]) {
      --cnt[nv];
      if (cnt[nv] > 0)
        continue;
      que.push(nv);
      par[nv] = v;
    }
  }
  rep(i, N) cout << par[i] + 1 << endl;
}
Problem p03142 (Python)

Buggy submission (status: Time Limit Exceeded):
n, m = map(int, input().split())
g = [[] for _ in range(n)]
inv = [0] * n
for _ in range(n - 1 + m):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    inv[b - 1] += 1
for i in range(n):
    if inv[i] == 0:
        root = i
        break
d = [0] * n
s = [root]
while s:
    p = s.pop()
    for node in g[p]:
        d[node] = max(d[node], d[p] + 1)
        s.append(node)
ans = [0] * n
for i in range(n):
    for node in g[i]:
        if d[node] == d[i] + 1:
            ans[node] = i + 1
for x in ans:
    print(x)


Fixed submission:
n, m = map(int, input().split())
g = [[] for _ in range(n)]
inv = [0] * n
for _ in range(n - 1 + m):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    inv[b - 1] += 1
for i in range(n):
    if inv[i] == 0:
        root = i
        break
d = [0] * n
s = [root]
while s:
    p = s.pop()
    for node in g[p]:
        inv[node] -= 1
        if inv[node] == 0:
            d[node] = max(d[node], d[p] + 1)
            s.append(node)
ans = [0] * n
for i in range(n):
    for node in g[i]:
        if d[node] == d[i] + 1:
            ans[node] = i + 1
for x in ans:
    print(x)
Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> deg(n, 0);
  rep(i, n + m - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    deg[b]++;
  }
  int root = -1;
  for (int i = 0; i < n; i++)
    if (!deg[i])
      root = i;
  vector<int> par(n, 0);
  queue<int> que;
  que.push(root);
  par[root] = -1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (auto v : g[u]) {
      par[v] = u;
      que.push(v);
    }
  }
  for (int i = 0; i < n; i++)
    cout << par[i] + 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> deg(n, 0);
  rep(i, n + m - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    deg[b]++;
  }
  int root = -1;
  for (int i = 0; i < n; i++)
    if (!deg[i])
      root = i;
  vector<int> par(n, 0);
  queue<int> que;
  que.push(root);
  par[root] = -1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (auto v : g[u]) {
      deg[v]--;
      if (deg[v] == 0) {
        que.push(v);
        par[v] = u;
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << par[i] + 1 << endl;
  return 0;
}
Problem p03142 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define INF 1000000000
#define eps 1e-7
#define mod 1000000007
#define int ll
using namespace std;

int P[100000 + 10];
vi adj[10000 + 10];
int in_deg[10000 + 10];

signed main(void) {
  int N, M;
  cin >> N >> M;

  REP(i, 1, N) P[i] = i;

  rep(i, N - 1 + M) {
    int A, B;
    cin >> A >> B;
    in_deg[B]++;
    adj[A].push_back(B);
  }

  /* breadth first search */
  queue<int> Q;
  REP(i, 1, N) if (in_deg[i] == 0) Q.push(i);
  while (!Q.empty()) {
    int top = Q.front();
    Q.pop();
    for (auto node : adj[top]) {
      in_deg[node]--;
      if (in_deg[node] == 0) {
        Q.push(node);
        P[node] = top;
      }
    }
  }

  REP(i, 1, N) {
    if (P[i] == i)
      cout << 0 << endl;
    else
      cout << P[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define INF 1000000000
#define eps 1e-7
#define mod 1000000007
#define int ll
using namespace std;

int P[100000 + 10];
vi adj[100000 + 10];
int in_deg[100000 + 10];

signed main(void) {
  int N, M;
  cin >> N >> M;

  REP(i, 1, N) P[i] = i;

  rep(i, N - 1 + M) {
    int A, B;
    cin >> A >> B;
    in_deg[B]++;
    adj[A].push_back(B);
  }

  /* breadth first search */
  queue<int> Q;
  REP(i, 1, N) if (in_deg[i] == 0) Q.push(i);
  while (!Q.empty()) {
    int top = Q.front();
    Q.pop();
    for (auto node : adj[top]) {
      in_deg[node]--;
      if (in_deg[node] == 0) {
        Q.push(node);
        P[node] = top;
      }
    }
  }

  REP(i, 1, N) {
    if (P[i] == i)
      cout << 0 << endl;
    else
      cout << P[i] << endl;
  }

  return 0;
}
Problem p03142 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
const int INF = 2e9 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair

int read() {
  int xx = 0, ww = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ww = -ww;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = 10 * xx + ch - 48;
    ch = getchar();
  }
  return xx * ww;
}

int n, m, de[10010];
int from, to, fa[10010];
vector<int> ed[10010];
queue<int> q;

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n + m - 1; i++) {
    from = read();
    to = read();
    ed[from].pb(to);
    de[to]++;
  }
  //	for(int i=1; i<=n; i++) printf("%d\n",de[i]);
  for (int i = 1; i <= n; i++)
    if (de[i] == 0) {
      q.push(i);
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int j = 0; j < ed[u].size(); j++) {
          int v = ed[u][j];
          de[v]--;
          if (de[v] == 0) {
            fa[v] = u;
            q.push(v);
          }
        }
      }
      fa[i] = 0;
      break;
    }
  for (int i = 1; i <= n; i++)
    printf("%d\n", fa[i]);
  return 0;
}

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

typedef long long ll;
typedef pair<int, int> pii;
const int INF = 2e9 + 10;
#define fi first
#define se second
#define pb push_back
#define mp make_pair

int read() {
  int xx = 0, ww = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ww = -ww;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = 10 * xx + ch - 48;
    ch = getchar();
  }
  return xx * ww;
}

int n, m, de[100010];
int from, to, fa[100010];
vector<int> ed[100010];
queue<int> q;

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n + m - 1; i++) {
    from = read();
    to = read();
    ed[from].pb(to);
    de[to]++;
  }
  //	for(int i=1; i<=n; i++) printf("%d\n",de[i]);
  for (int i = 1; i <= n; i++)
    if (de[i] == 0) {
      q.push(i);
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int j = 0; j < ed[u].size(); j++) {
          int v = ed[u][j];
          de[v]--;
          if (de[v] == 0) {
            fa[v] = u;
            q.push(v);
          }
        }
      }
      fa[i] = 0;
      break;
    }
  for (int i = 1; i <= n; i++)
    printf("%d\n", fa[i]);
  return 0;
}
Problem p03143 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 100005
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, m, cnt, ans, head[MAXN], f[MAXN], b[MAXN];
LL num[MAXN];

struct Edge {
  int next, to;
  LL dis;
} edge[MAXN * 2];
struct E {
  int x, y;
  LL z;
} e[MAXN];

void addedge(int from, int to, LL dis) {
  edge[++cnt].next = head[from];
  edge[cnt].to = to;
  edge[++cnt].dis = dis;
  head[from] = cnt;
}

bool CMP(E x, E y) { return x.z < y.z; }

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

int main() {
  scanf("%d%d", &n, &m);
  for (rint i = 1; i <= n; ++i)
    scanf("%lld", &num[i]);
  for (rint i = 1, x, y; i <= m; ++i) {
    scanf("%d%d%lld", &e[i].x, &e[i].y, &e[i].z);
    addedge(e[i].x, e[i].y, e[i].z);
    addedge(e[i].y, e[i].x, e[i].z);
  }
  for (rint i = 1; i <= n; ++i)
    f[i] = i;
  sort(e + 1, e + m + 1, CMP);
  for (rint i = 1; i <= m; ++i) {
    int fx = find(e[i].x), fy = find(e[i].y);
    if (fx == fy) {
      if (num[fx] < e[i].z)
        ans++, b[fx]++;
    } else {
      f[fx] = fy, num[fy] += num[fx];
      b[fy] += b[fx];
      if (num[fy] >= e[i].z) {
        ans -= b[fy];
        b[fy] = 0;
      } else {
        b[fy]++;
        ans++;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 100005
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, m, cnt, ans, head[MAXN], f[MAXN], b[MAXN];
LL num[MAXN];

struct Edge {
  int next, to;
  LL dis;
} edge[MAXN * 2];
struct E {
  int x, y;
  LL z;
} e[MAXN];

void addedge(int from, int to, LL dis) {
  edge[++cnt].next = head[from];
  edge[cnt].to = to;
  edge[cnt].dis = dis;
  head[from] = cnt;
}

bool CMP(E x, E y) { return x.z < y.z; }

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

int main() {
  scanf("%d%d", &n, &m);
  for (rint i = 1; i <= n; ++i)
    scanf("%lld", &num[i]);
  for (rint i = 1, x, y; i <= m; ++i) {
    scanf("%d%d%lld", &e[i].x, &e[i].y, &e[i].z);
    addedge(e[i].x, e[i].y, e[i].z);
    addedge(e[i].y, e[i].x, e[i].z);
  }
  for (rint i = 1; i <= n; ++i)
    f[i] = i;
  sort(e + 1, e + m + 1, CMP);
  for (rint i = 1; i <= m; ++i) {
    int fx = find(e[i].x), fy = find(e[i].y);
    if (fx == fy) {
      if (num[fx] < e[i].z)
        ans++, b[fx]++;
    } else {
      f[fx] = fy, num[fy] += num[fx];
      b[fy] += b[fx];
      if (num[fy] >= e[i].z) {
        ans -= b[fy];
        b[fy] = 0;
      } else {
        b[fy]++;
        ans++;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}