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

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

vector<int> graph[100];
int start[100], endt[100];
int cnt;

void dfs(int num) {
  start[num] = ++cnt;
  for (int i = 0; i < graph[num].size(); i++) {
    if (graph[num][i] != num && !endt[graph[num][i]])
      dfs(graph[num][i]);
  }
  endt[num] = ++cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int now, m;
    cin >> now >> m;
    for (int j = 0; j < m; j++) {
      int t;
      cin >> t;
      graph[now - 1].push_back(t - 1);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!endt[i])
      dfs(i);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << start[i] << " " << endt[i] << endl;
  }
  return 0;
}

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

vector<int> graph[100];
int start[100], endt[100];
int cnt;

void dfs(int num) {
  start[num] = ++cnt;
  for (int i = 0; i < graph[num].size(); i++) {
    if (graph[num][i] != num && !start[graph[num][i]] && !endt[graph[num][i]])
      dfs(graph[num][i]);
  }
  endt[num] = ++cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int now, m;
    cin >> now >> m;
    for (int j = 0; j < m; j++) {
      int t;
      cin >> t;
      graph[now - 1].push_back(t - 1);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!endt[i])
      dfs(i);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << start[i] << " " << endt[i] << endl;
  }
  return 0;
}
Problem p02238 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, tt;
int color[N], nt[N], M[N][N], d[N], f[N];

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      dfs_visit(i);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j--) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, tt;
int color[N], nt[N], M[N][N], d[N], f[N];

int next(int u) {
  for (int v = nt[u]; v < n; v++) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE) {
      dfs_visit(i);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}
Problem p02238 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// u?????£??\??????v?????????????????????
int next(int u) {
  for (int v = 0; v < n; ++v) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

//?????????????????¨????????±???????????¢?´¢
void dfs_visit(int r) {
  for (int i = 0; i < n; ++i) {
    nt[i] = 0;
  }
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  //?????????
  for (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  //????¨???????u????§??????¨????????±???????????¢?´¢
  for (int u = 0; u < n; ++u) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      M[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; ++j) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// u?????£??\??????v?????????????????????
int next(int u) {
  for (int v = nt[u]; v < n; ++v) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

//?????????????????¨????????±???????????¢?´¢
void dfs_visit(int r) {
  for (int i = 0; i < n; ++i) {
    nt[i] = 0;
  }
  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;
  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  //?????????
  for (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;
  //????¨???????u????§??????¨????????±???????????¢?´¢
  for (int u = 0; u < n; ++u) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      M[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; ++j) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  dfs();
  return 0;
}
Problem p02239 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1 << 21;
const int maxn = 100 + 10;
int n;
vector<int> g[maxn];
int vis[maxn];
int d[maxn];
void bfs(int s) {
  queue<int> que;
  que.push(s);
  vis[s] = 1;
  d[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!vis[v]) {
        que.push(v);
        d[v] = min(d[v], d[u] + 1);
      }
    }
  }
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  int no, cnt, dd;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &no, &cnt);
    for (int j = 0; j < cnt; j++) {
      scanf("%d", &dd);
      g[no - 1].push_back(dd - 1);
    }
  }
  for (int i = 0; i < n; i++)
    d[i] = INF;
  bfs(0);
  for (int i = 0; i < n; i++) {
    printf("%d %d\n", i + 1, d[i] == INF ? -1 : d[i]);
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1 << 21;
const int maxn = 100 + 10;
int n;
vector<int> g[maxn];
int vis[maxn];
int d[maxn];
void bfs(int s) {
  queue<int> que;
  que.push(s);
  vis[s] = 1;
  d[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!vis[v]) {
        que.push(v);
        vis[v] = 1;
        d[v] = min(d[v], d[u] + 1);
      }
    }
  }
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  int no, cnt, dd;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &no, &cnt);
    for (int j = 0; j < cnt; j++) {
      scanf("%d", &dd);
      g[no - 1].push_back(dd - 1);
    }
  }
  for (int i = 0; i < n; i++)
    d[i] = INF;
  bfs(0);
  for (int i = 0; i < n; i++) {
    printf("%d %d\n", i + 1, d[i] == INF ? -1 : d[i]);
  }

  return 0;
}
Problem p02239 (C++)

Buggy submission (status: Time Limit Exceeded):
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <limits.h>
#include <queue>

using namespace std;
static const int NMAX = 100;
int N, D[NMAX] = {};
bool G[NMAX][NMAX] = {{false}};

void breadthFirstSearch(int base) {
  int ix;
  for (int i = 0; i < N; i++) {
    D[i] = INT_MAX;
  }

  D[base] = 0;
  queue<int> q;
  q.push(base);
  while (!q.empty()) {
    ix = q.front();
    q.pop();
    for (int i = 0; i < N; i++) {
      if (G[ix][i]) {
        q.push(i);
        if (D[ix] + 1 < D[i])
          D[i] = D[ix] + 1;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    cout << i + 1 << " " << ((D[i] == INT_MAX) ? (-1) : D[i]) << endl;
  }
}

int main() {
  int ix, deg, cache;
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> ix >> deg;
    for (int j = 0; j < deg; j++) {
      cin >> cache;
      G[ix - 1][cache - 1] = true;
    }
  }

  breadthFirstSearch(0);

  return 0;
}


Fixed submission:
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <limits.h>
#include <queue>

using namespace std;
static const int NMAX = 100;
int N, D[NMAX] = {};
bool G[NMAX][NMAX] = {{false}};

void breadthFirstSearch(int base) {
  int ix;
  for (int i = 0; i < N; i++) {
    D[i] = INT_MAX;
  }

  D[base] = 0;
  queue<int> q;
  q.push(base);
  while (!q.empty()) {
    ix = q.front();
    q.pop();
    for (int i = 0; i < N; i++) {
      if (!G[ix][i])
        continue;
      if (D[i] != INT_MAX)
        continue;
      D[i] = D[ix] + 1;
      q.push(i);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << i + 1 << " " << ((D[i] == INT_MAX) ? (-1) : D[i]) << endl;
  }
}

int main() {
  int ix, deg, cache;
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> ix >> deg;
    for (int j = 0; j < deg; j++) {
      cin >> cache;
      G[ix - 1][cache - 1] = true;
    }
  }

  breadthFirstSearch(0);

  return 0;
}
Problem p02239 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class Graph {
public:
  int v;
  int cost = inf;
  int ftime = -1;
  int ltime = -1;
  vector<int> e;
};
vector<Graph> g;
int cnt = 0;
void dfs(int pos) {
  if (g[pos].ftime != -1)
    return;
  cnt++;
  g[pos].ftime = cnt;
  for (int i = 0; i < g[pos].e.size(); i++) {
    dfs(g[pos].e[i]);
  }
  cnt++;
  g[pos].ltime = cnt;
}
void bfs(int pos) {
  for (int i = 0; i < g[pos].e.size(); i++) {
    // if( g[ g[pos].e[i] ].cost != inf ) continue;
    g[g[pos].e[i]].cost = min(g[g[pos].e[i]].cost, g[pos].cost + 1);
    bfs(g[pos].e[i]);
  }
}

int main(void) {

  int n;
  cin >> n;
  g.resize(n);
  int u, k;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    g[u].v = u;
    g[u].e.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> g[u].e[j];
      g[u].e[j]--;
    }
    sort(g[u].e.begin(), g[u].e.end());
  }

  g[0].cost = 0;
  bfs(0);
  for (int i = 0; i < n; i++)
    cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl;

  // for(int i=0; i<n; i++) dfs(i);
  // for(int i=0; i<n; i++){
  //   cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl;
  // }

  return 0;
}

// EOF

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class Graph {
public:
  int v;
  int cost = inf;
  int ftime = -1;
  int ltime = -1;
  vector<int> e;
};
vector<Graph> g;
int cnt = 0;
void dfs(int pos) {
  if (g[pos].ftime != -1)
    return;
  cnt++;
  g[pos].ftime = cnt;
  for (int i = 0; i < g[pos].e.size(); i++) {
    dfs(g[pos].e[i]);
  }
  cnt++;
  g[pos].ltime = cnt;
}
void bfs(int pos) {
  queue<int> q;
  q.push(pos);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int i = 0; i < g[p].e.size(); i++) {
      if (g[g[p].e[i]].cost != inf)
        continue;
      g[g[p].e[i]].cost = min(g[g[p].e[i]].cost, g[p].cost + 1);
      q.push(g[p].e[i]);
    }
  }
}

int main(void) {

  int n;
  cin >> n;
  g.resize(n);
  int u, k;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    g[u].v = u;
    g[u].e.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> g[u].e[j];
      g[u].e[j]--;
    }
    sort(g[u].e.begin(), g[u].e.end());
  }

  g[0].cost = 0;
  bfs(0);
  for (int i = 0; i < n; i++)
    cout << g[i].v + 1 << " " << (g[i].cost == inf ? -1 : g[i].cost) << endl;

  // for(int i=0; i<n; i++) dfs(i);
  // for(int i=0; i<n; i++){
  //   cout << g[i].v + 1 << " " << g[i].ftime << " " << g[i].ltime << endl;
  // }

  return 0;
}

// EOF
Problem p02239 (C++)

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

using namespace std;

int main() {
  int n, u, k, v;
  cin >> n;
  vector<int> ans(n, 1000000000);
  vector<vector<int>> adj(n);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    while (k--)
      cin >> v, adj[u - 1].push_back(v - 1);
  }
  queue<pair<int, int>> q; //<idx,path>
  q.push(pair<int, int>(0, 1));
  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    ans[tmp.first] = min(ans[tmp.first], tmp.second);
    for (int i = 0; i < adj[tmp.first].size(); i++)
      q.push(pair<int, int>(adj[tmp.first][i], tmp.second + 1));
  }
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i] - 1) << endl;
}

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

using namespace std;

int main() {
  int n, u, k, v;
  cin >> n;
  vector<int> ans(n, 1000000000);
  vector<vector<int>> adj(n);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    while (k--)
      cin >> v, adj[u - 1].push_back(v - 1);
  }
  queue<pair<int, int>> q; //<idx,path>
  q.push(pair<int, int>(0, 1));
  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    ans[tmp.first] = min(ans[tmp.first], tmp.second);
    if (ans[tmp.first] != tmp.second)
      continue;
    for (int i = 0; i < adj[tmp.first].size(); i++)
      q.push(pair<int, int>(adj[tmp.first][i], tmp.second + 1));
  }
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << (ans[i] == 1000000000 ? -1 : ans[i] - 1) << endl;
}
Problem p02239 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int n, m, l, p;
int a[100][100];
int D[100];
void bfs(int src) {
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int dst = 0; dst < n; dst++) {
      if (D[dst] < 0 && a[cur][dst] == 1) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cerr << i + 1 << " " << D[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    D[i] = -1;
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> m;
    cin >> l;
    for (int j = 0; j < l; j++) {
      cin >> p;
      a[m - 1][p - 1] = 1;
    }
  }
  bfs(0);
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int n, m, l, p;
int a[100][100];
int D[100];
void bfs(int src) {
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int dst = 0; dst < n; dst++) {
      if (D[dst] < 0 && a[cur][dst] == 1) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << D[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    D[i] = -1;
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> m;
    cin >> l;
    for (int j = 0; j < l; j++) {
      cin >> p;
      a[m - 1][p - 1] = 1;
    }
  }
  bfs(0);
}
Problem p02239 (C++)

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++)
      M[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}
Problem p02239 (C++)

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

int D[100];
int a[100][100];

int main() {
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
      a[i][j] = 0;
    }
  }
  int n, u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      a[u][v] = 1;
    }
  }
  queue<int> Q; // 整数を管理するキューの定義
  for (int i = 1; i < n + 1; i++) {
    D[i] = -1;
  }
  int src = 1;
  Q.push(src);
  D[src] = 0; // 出発点
  while (!Q.empty()) {
    int cur = Q.front(); // 先頭要素を取り出す
    Q.pop();
    // 動作確認用表示
    cerr << "visiting " << cur << ' ' << D[cur] << endl;
    for (int dst = 1; dst <= n; dst++) { // 各行き先dstに対して
      if ((a[cur][dst] == 1 &&
           D[dst] == -1)) { // curからdstに辺があり、dstが未訪問なら
        D[dst] = D[cur] + 1; //
        Q.push(dst);         // dstを訪問先に入れる
      }
    }
  }
  for (int i = 1; i < n + 1; i++) {
    cout << i << " " << D[i] << endl;
  }
}

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

int D[100];
int a[100][100];

int main() {
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
      a[i][j] = 0;
    }
  }
  int n, u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      a[u][v] = 1;
    }
  }
  queue<int> Q; // 整数を管理するキューの定義
  for (int i = 1; i < n + 1; i++) {
    D[i] = -1;
  }
  int src = 1;
  Q.push(src);
  D[src] = 0; // 出発点
  while (!Q.empty()) {
    int cur = Q.front(); // 先頭要素を取り出す
    Q.pop();
    for (int dst = 1; dst <= n; dst++) { // 各行き先dstに対して
      if ((a[cur][dst] == 1 &&
           D[dst] == -1)) { // curからdstに辺があり、dstが未訪問なら
        D[dst] = D[cur] + 1; //
        Q.push(dst);         // dstを訪問先に入れる
      }
    }
  }
  for (int i = 1; i < n + 1; i++) {
    cout << i << " " << D[i] << endl;
  }
}
Problem p02239 (C++)

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

int res[101];
int G[101][101] = {0};

int main() {
  int n, u, v, k;
  std::queue<int> Q;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> u >> k;
    for (int j = 0; j < k; j++) {
      std::cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }

  for (int i = 0; i < n; i++)
    res[i] = INF;

  Q.push(0);
  res[0] = 0;

  while (!Q.empty()) {
    u = Q.front();
    for (int i = 0; i < n; i++) {
      if (G[u][i]) {
        Q.push(i);
        res[i] = std::min(res[i], res[u] + 1);
      }
    }
    Q.pop();
  }

  for (int i = 0; i < n; i++) {
    if (res[i] != INF)
      std::cout << i + 1 << " " << res[i] << "\n";
    else
      std::cout << i + 1 << " -1\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 999999999
using namespace std;

int res[101];
int G[101][101] = {0};

int main() {
  int n, u, v, k;
  std::queue<int> Q;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> u >> k;
    for (int j = 0; j < k; j++) {
      std::cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }

  for (int i = 0; i < n; i++)
    res[i] = INF;

  Q.push(0);
  res[0] = 0;

  while (!Q.empty()) {
    u = Q.front();
    for (int i = 0; i < n; i++) {
      if (G[u][i]) {
        Q.push(i);
        res[i] = std::min(res[i], res[u] + 1);
        G[u][i] = 0;
      }
    }
    Q.pop();
  }

  for (int i = 0; i < n; i++) {
    if (res[i] != INF)
      std::cout << i + 1 << " " << res[i] << "\n";
    else
      std::cout << i + 1 << " -1\n";
  }

  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 10000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assign() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int j = 0; j < n; j++) {
    if (color[j] == NIL) {
      dfs(j, id++);
    }
  }
}

int main() {
  int s, t, m, q;

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assign();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assign() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int j = 0; j < n; j++) {
    if (color[j] == NIL) {
      dfs(j, id++);
    }
  }
}

int main() {
  int s, t, m, q;

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assign();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02240 (C++)

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

using namespace std;
static const int MAX = 100;
static const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assign_color() {
  int id = 1;
  for (int i = 0; i < n; i++)
    color[i] = NIL;
  for (int u = 0; u < n; u++) {
    if (color[u] == NIL)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assign_color();
  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}


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

using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assign_color() {
  int id = 1;
  for (int i = 0; i < n; i++)
    color[i] = NIL;
  for (int u = 0; u < n; u++) {
    if (color[u] == NIL)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }
  assign_color();
  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int ancestor[10];
int find(int x) {
  if (ancestor[x] == x)
    return x;
  return ancestor[x] = find(ancestor[x]);
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int main() {
  int n, q, m, temp, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    ancestor[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    temp = find(x);
    q = find(y);
    ancestor[max(temp, q)] = min(temp, q);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y))
      printf("no\n");
    else
      printf("yes\n");
  }
}

Fixed submission:
#include <stdio.h>
int ancestor[100000];
int find(int x) {
  if (ancestor[x] == x)
    return x;
  return ancestor[x] = find(ancestor[x]);
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int main() {
  int n, q, m, temp, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    ancestor[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    temp = find(x);
    q = find(y);
    ancestor[max(temp, q)] = min(temp, q);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y))
      printf("no\n");
    else
      printf("yes\n");
  }
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 10000;
static const int NIL = -1;

int n, color[MAX];
vector<int> G[MAX];

void dfs(int r, int c) {
  stack<int> S;
  int u, i, v;

  S.push(r);
  color[r] = c;

  while (!S.empty()) {
    u = S.top();
    S.pop();
    for (i = 0; i < G[u].size(); i++) {
      v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assignColor() {
  int id = 1;
  int i, u;
  for (i = 0; i < n; i++)
    color[i] = NIL;
  for (u = 0; u < n; u++) {
    if (color[u] == NIL)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  int i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n, color[MAX];
vector<int> G[MAX];

void dfs(int r, int c) {
  stack<int> S;
  int u, i, v;

  S.push(r);
  color[r] = c;

  while (!S.empty()) {
    u = S.top();
    S.pop();
    for (i = 0; i < G[u].size(); i++) {
      v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assignColor() {
  int id = 1;
  int i, u;
  for (i = 0; i < n; i++)
    color[i] = NIL;
  for (u = 0; u < n; u++) {
    if (color[u] == NIL)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  int i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> g;
int n, m, q, s, t;

vector<bool> visited;
vector<int> color;
void dfs(int s) {
  for (int i = 0; i < g[s].size(); i++) {
    if (color[g[s][i]] == -1) {
      color[g[s][i]] = color[s];
      dfs(g[s][i]);
    }
  }
}
bool dfs2(int s, int t) {
  if (s == t)
    return true;
  visited[s] = true;
  for (int i = 0; i < g[s].size(); i++) {
    if (!visited[g[s][i]]) {
      if (dfs2(g[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  g.resize(n);
  color.resize(n, -1);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      color[i] = i;
      dfs(i);
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    visited.resize(n, false);
    string s1 = (dfs2(s, t) ? "yes" : "no");
    string s2 = (color[s] == color[t] ? "yes" : "no");
    if (s1 != s2)
      return 1;
    printf((color[s] == color[t] ? "yes\n" : "no\n"));
  }
  return 0;
}

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

vector<vector<int>> g;
int n, m, q, s, t;

vector<bool> visited;
vector<int> color;
void dfs(int s) {
  for (int i = 0; i < g[s].size(); i++) {
    if (color[g[s][i]] == -1) {
      color[g[s][i]] = color[s];
      dfs(g[s][i]);
    }
  }
}
bool dfs2(int s, int t) {
  if (s == t)
    return true;
  visited[s] = true;
  for (int i = 0; i < g[s].size(); i++) {
    if (!visited[g[s][i]]) {
      if (dfs2(g[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  g.resize(n);
  color.resize(n, -1);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      color[i] = i;
      dfs(i);
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    visited.resize(n, false);
    string s1 = (dfs2(s, t) ? "yes" : "no");
    string s2 = (color[s] == color[t] ? "yes" : "no");
    if (s1 != s2 && s1 == "yes")
      return 1;
    printf((color[s] == color[t] ? "yes\n" : "no\n"));
  }
  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[10000];

void bfs(int x) {
  color[x] = "gray";
  q.push(x);

  while (q.size() != 0) {
    int u = q.front();
    q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (color[v] == "white") {
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;
  }
  return;
}

int main() {

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;

  int a, b;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    color[i] = "white";
  }

  for (int j = 0; j < n; j++) {
    if (color[j] == "white") {
      bfs(j);
    }
  }

  int q;

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (ans[a] == ans[b]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}


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

int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[1000000];

void bfs(int x) {
  color[x] = "gray";
  q.push(x);

  while (q.size() != 0) {
    int u = q.front();
    q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (color[v] == "white") {
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;
  }
  return;
}

int main() {

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;

  int a, b;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    color[i] = "white";
  }

  for (int j = 0; j < n; j++) {
    if (color[j] == "white") {
      bfs(j);
    }
  }

  int q;

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (ans[a] == ans[b]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
  if (table[child] == -1)
    return child;
  return table[child] = parent(table, table[child]);
}

int main() {
  int n, m, q;
  int table[MAX];

  scanf("%d %d", &n, &m);

  for (int i = 0; i < n; i++)
    table[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);
    if (ps != pt) {
      table[ps] = pt;
    } else {
      table[s] = ps;
      table[t] = pt;
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int s, t;

    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);

    if (ps == pt) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }
  return 0;
}

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

const int MAX = 100000;

int parent(int table[], int child) {
  if (table[child] == -1)
    return child;
  return table[child] = parent(table, table[child]);
}

int main() {
  int n, m, q;
  int table[MAX];

  scanf("%d %d", &n, &m);

  for (int i = 0; i < n; i++)
    table[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);
    if (ps != pt) {
      table[ps] = pt;
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int s, t;

    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);

    if (ps == pt) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }
  return 0;
}
Problem p02240 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G(100);
vector<int> c(100);
void dfs(int s, int x) {
  if (c[s])
    return;
  c[s] = x;
  for (auto a : G[s])
    dfs(a, x);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; ++i)
    dfs(i, i + 1);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] - c[b] ? "no" : "yes") << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G(100000);
vector<int> c(100000);
void dfs(int s, int x) {
  if (c[s])
    return;
  c[s] = x;
  for (auto a : G[s])
    dfs(a, x);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; ++i)
    dfs(i, i + 1);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] - c[b] ? "no" : "yes") << endl;
  }
}
Problem p02240 (C++)

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

using namespace std;

vector<int> G[10000];
int color[10000];
int n;

void dfs(int idx, int c) {
  stack<int> s;
  s.push(idx);
  color[idx] = c;

  while (!s.empty()) {
    int u = s.top();
    s.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (color[v] == -1) {
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void AssignColor() {
  int id = 1;
  for (int i = 0; i < n; ++i)
    color[i] = -1;
  for (int u = 0; u < n; ++u) {
    if (color[u] == -1)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  AssignColor();

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}

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

using namespace std;

vector<int> G[100000];
int color[100000];
int n;

void dfs(int idx, int c) {
  stack<int> s;
  s.push(idx);
  color[idx] = c;

  while (!s.empty()) {
    int u = s.top();
    s.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (color[v] == -1) {
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void AssignColor() {
  int id = 1;
  for (int i = 0; i < n; ++i)
    color[i] = -1;
  for (int u = 0; u < n; ++u) {
    if (color[u] == -1)
      dfs(u, id++);
  }
}

int main() {
  int s, t, m, q;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  AssignColor();

  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02240 (C++)

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

using namespace std;

static const int N = 100;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c) {
  queue<int> q;
  q.push(i);
  color[i] = c;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (-1 == color[v]) {
        color[v] = c;
        q.push(v);
      }
    }
  }
}

void search() {
  for (int i = 0; i < n; ++i)
    color[i] = -1;

  int c = 0;
  for (int i = 0; i < n; ++i) {
    if (color[i] == -1)
      dfs(i, c++);
  }
}

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  search();

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    if (color[u] == color[v])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

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

using namespace std;

static const int N = 100001;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c) {
  queue<int> q;
  q.push(i);
  color[i] = c;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (-1 == color[v]) {
        color[v] = c;
        q.push(v);
      }
    }
  }
}

void search() {
  for (int i = 0; i < n; ++i)
    color[i] = -1;

  int c = 0;
  for (int i = 0; i < n; ++i) {
    if (color[i] == -1)
      dfs(i, c++);
  }
}

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  search();

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    if (color[u] == color[v])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02241 (C++)

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

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; i++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}
Problem p02241 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録
  // MSTにおける頂点vの親を記録
  // 訪問状態を記録

  for (int i = 0; i < n;
       i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) { // 最小の辺を探索
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK &&
          M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }

  return sum;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録
  // MSTにおける頂点vの親を記録
  // 訪問状態を記録

  for (int i = 0; i < n;
       i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) { // 最小の辺を探索
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK &&
          M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }

  return sum;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}