alexjercan/bugnet huggingface dataset:
Problem p02238 (C++) — buggy submission, verdict: Time Limit Exceeded

#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, 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++) { // 訪問した場所は探索しなくていいため、v+1にしている？
    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 argc, char const *argv[]) {
  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--; // 0 origin
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}


Error: TLE

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, 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++) { // 訪問した場所は探索しなくていいため、v+1にしている？
    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 argc, char const *argv[]) {
  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--; // 0 origin
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

const int MAX = 101;

vector<int> G[MAX];
int d[MAX], f[MAX];
int *T;

void dfs(int u) {

  if (!d[u])
    d[u] = ++(*T);

  for (int v : G[u]) {
    if (!d[v]) {
      d[v] = ++(*T);
      dfs(v);
    }
  }

  if (!f[u])
    f[u] = ++(*T);
}

int main() {

  int n;
  cin >> n;

  int t = 0;
  T = &t;
  fill(d, d + MAX, 0);
  fill(f, f + MAX, 0);

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

  for (int i = 1; i <= n; i++) {
    if (!d[i])
      dfs(i);
  }

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


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int MAX = 101;

vector<int> G[MAX];
int d[MAX], f[MAX];
int *T;

void dfs(int u) {

  if (!d[u])
    d[u] = ++(*T);

  for (int v : G[u]) {
    if (!d[v])
      d[v] = ++(*T);
    dfs(v);
  }

  if (!f[u])
    f[u] = ++(*T);
}

int main() {

  int n;
  cin >> n;

  int t = 0;
  T = &t;
  fill(d, d + MAX, 0);
  fill(f, f + MAX, 0);

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

  for (int i = 1; i <= n; i++) {
    if (!d[i])
      dfs(i);
  }

  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
}
Problem p02238 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int curTime = 1;
void dfs(int cur, const vector<vector<bool>> &a, vector<int> &d, vector<int> &f,
         vector<bool> &visited) {
  visited[cur] = true;
  d[cur] = curTime;
  curTime++;
  // cerr << "visiting [" << cur << "], time = " << curTime << endl;

  for (int dst = 1; dst <= a.size(); dst++) {
    if (a[cur][dst] &&
        !visited[dst]) { // cur????????¶??????????????????????????????
      dfs(dst, a, d, f, visited);
    }
  }
  f[cur] = curTime;
  curTime++;
  return;
}

int main() {
  int n;
  cin >> n;
  vector<vector<bool>> a(n + 1, vector<bool>(n + 1, false));
  for (int i = 1; i <= n; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      a[i][v] = true;
    }
  }

  vector<bool> visited(n + 1, false);
  vector<int> d(n + 1), f(n + 1);

  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i, a, d, f, visited);
    }
  }

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

Error: 0
visiting [1], time = 2
visiting [2], time = 3
visiting [4], time = 4
visiting [3], time = 5


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int curTime = 1;
void dfs(int cur, const vector<vector<bool>> &a, vector<int> &d, vector<int> &f,
         vector<bool> &visited) {
  visited[cur] = true;
  d[cur] = curTime;
  curTime++;
  cerr << "visiting [" << cur << "], time = " << curTime << endl;

  for (int dst = 1; dst <= a.size(); dst++) {
    if (a[cur][dst] &&
        !visited[dst]) { // cur????????¶??????????????????????????????
      dfs(dst, a, d, f, visited);
    }
  }
  f[cur] = curTime;
  curTime++;
  return;
}

int main() {
  int n;
  cin >> n;
  vector<vector<bool>> a(n + 1, vector<bool>(n + 1, false));
  for (int i = 1; i <= n; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      a[i][v] = true;
    }
  }

  vector<bool> visited(n + 1, false);
  vector<int> d(n + 1), f(n + 1);

  dfs(1, a, d, f, visited);

  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;
int V[100][100];
int d[100];
int t[100];
int n;
queue<int> qu;
void bfs() {
  t[0] = 1;
  d[0] = 0;
  qu.push(0);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = 0; i < n; i++) {
      if (V[u][i] == 1 && t[i] == 0) {
        t[i] = 1;
        d[i] = d[u] + 1;
        qu.push(i);
      }
    }
    t[u] = 2;
  }
  return;
}

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

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

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

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define show(x) #x " = " << x

int n;
vector<int> G[100];
int d[100];

int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    int u, k, v;
    cin >> u >> k;
    for (i = 0; i < k; i++)
      cin >> v, G[u - 1].push_back(v - 1);
  }
  for (i = 0; i < 100; i++)
    d[i] = -1;

  queue<int> q;
  q.push(0);
  d[0] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < G[v].size(); i++) {
      int x = G[v][i];
      if (d[x] == -1) {
        d[x] = d[v] + 1;
        q.push(x);
      }
    }
  }
  for (i = 0; i < n; i++)
    cout << i + 1 << " " << d[i] << endl;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

//
// Created by 李青坪 on 2018/11/3.
//

#include <algorithm>
#include <cstdio>
#include <queue>

#define MAXN 101
using namespace std;
int n;
int graph[MAXN][MAXN];
int dist[MAXN];
bool visited[MAXN];
queue<int> q;

void init() {
  fill(dist, dist + MAXN, -1);
  fill(visited, visited + MAXN, false);
  dist[1] = 0;
}

void bfs(int i) {
  for (int j = 1; j <= n; ++j) {
    if (graph[i][j] == 1) {
      if (visited[j])
        continue;
      q.push(j);
      visited[j] = true;
      dist[j] = dist[j] >= 0 ? min(dist[j], dist[i] + 1) : dist[i] + 1;
    }
  }
  while (!q.empty()) {
    int next = q.front();
    q.pop();
    bfs(next);
  }
}

int main() {
  scanf("%d", &n);
  int u, k;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &u, &k);
    int node;
    for (int j = 0; j < k; ++j) {
      scanf("%d", &node);
      graph[u][node] = 1;
    }
  }
  init();
  bfs(1);
  for (int l = 1; l <= n; ++l) {
    printf("%d %d\\n", l, dist[l]);
  }
}
/*
5
1 2 2 4
2 1 4
3 0
4 1 3
5 1 4
 */


Error: 0

Fixed submission:

//
// Created by 李青坪 on 2018/11/3.
//

#include <algorithm>
#include <cstdio>
#include <queue>

#define MAXN 101
using namespace std;
int n;
int graph[MAXN][MAXN];
int dist[MAXN];
queue<int> q;

void init() {
  fill(dist, dist + MAXN, -1);
  dist[1] = 0;
}

void bfs(int i) {
  for (int j = 1; j <= n; ++j) {
    if (graph[i][j] == 1) {
      q.push(j);
      dist[j] = dist[j] >= 0 ? min(dist[j], dist[i] + 1) : dist[i] + 1;
    }
  }
  while (!q.empty()) {
    int next = q.front();
    q.pop();
    bfs(next);
  }
}

int main() {
  scanf("%d", &n);
  int u, k;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &u, &k);
    int node;
    for (int j = 0; j < k; ++j) {
      scanf("%d", &node);
      graph[u][node] = 1;
    }
  }
  init();
  bfs(1);
  for (int l = 1; l <= n; ++l) {
    printf("%d %d\\n", l, dist[l]);
  }
}
/*
5
1 2 2 4
2 1 4
3 0
4 1 3
5 1 4
 */
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

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

int G[223][223];
int dis[223];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for (int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st){1, 0});

  while (!q.empty()) {

    st u = q.front();
    q.pop();
    if (dis[u.pos] < 1000000)
      continue;

    dis[u.pos] = u.v;

    for (int i = 1; i <= n; i++) {
      if (i == u.pos)
        continue;
      if (G[u.pos][i] == 0)
        continue;
      q.push((st){i, u.v + 1});
    }
  }

  for (int i = 1; i <= n; i++) {
    printf("%d %d\\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

Error: -11

Fixed submission:

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

int G[123][123];
int dis[123];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for (int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = G[t][u] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st){1, 0});

  while (!q.empty()) {

    st u = q.front();
    q.pop();
    dis[u.pos] = u.v;

    for (int i = 1; i <= n; i++) {
      if (i == u.pos)
        continue;
      if (G[i][u.pos] == 0)
        continue;
      if (dis[i] <= u.v)
        continue;
      q.push((st){i, u.v + 1});
    }
  }

  for (int i = 1; i <= n; i++) {
    printf("%d %d\\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
mat g;

lli u, k, v;

queue<lli> q;
queue<lli> d;
vbl vis;
vll ans;

int main() {
  cin >> n;
  g = mat(n + 1);
  vis = vbl(n + 1);
  ans = vll(n + 1, -1);
  for (lli i = 0; i < n; i++) {
    cin >> u >> k;
    for (lli j = 0; j < k; j++) {
      cin >> v;
      g[u].push_back(v);
    }
  }
  q.push(1);
  d.push(0);
  while (!q.empty()) {
    lli p = q.front();
    lli r = d.front();
    q.pop();
    d.pop();
    if (vis[p])
      continue;
    vis[p] = true;
    ans[p] = r;
    for (lli i = 0; i < g[p].size(); i++) {
      if (!vis[g[p][i]])
        q.push(g[p][i]), d.push(r + 1);
    }
  }
  for (lli i = 1; i <= n; i++)
    cout << i << " " << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
mat g;

lli u, k, v;

queue<lli> q;
vbl vis;
lli p;
vll dp;

lli solve(lli x) {
  if (x == 1)
    return dp[x] = 0;
  if (dp[x])
    return dp[x];
  lli ret = -1;
  for (lli i = 0; i < g[x].size(); i++) {
    ret = solve(g[x][i]) < 0 ? ret
          : ret < 0          ? solve(g[x][i]) + 1
                             : min(ret, solve(g[x][i]) + 1);
  }
  return dp[x] = ret;
}

int main() {
  cin >> n;
  g = mat(n + 1);
  dp = vll(n + 1);
  for (lli i = 0; i < n; i++) {
    cin >> u >> k;
    for (lli j = 0; j < k; j++) {
      cin >> v;
      g[v].push_back(u);
    }
  }
  for (lli i = 1; i <= n; i++) {
    cout << i << " " << solve(i) << endl;
  }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: 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();
    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;
  }
}

Error: 0
visiting 1 0
visiting 2 1
visiting 4 1
visiting 3 2


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();
    // 動作確認用表示
    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;
  }
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
// original idea: stupid wanna check the node with many bfs search
using namespace std;
#define N 105             // number of nodes
vector<int> neighbors[N]; // start from 1
queue<int> q;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
int vis[N];
int res = 0;
void bfs(int d) {
  if (q.empty())
    return;
  // until q is empty
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    if (dist[temp] != -1) {
      if (d < dist[temp])
        dist[temp] = d;
      else {
        dist[temp] = dist[temp];
      }
    } else {
      dist[temp] = d;
    }
    for (int i = 0; i < neighbors[temp].size(); i++) {
      d = dist[temp] + 1;                      // next lever
      if (dist[neighbors[temp].at(i)] == -1) { // not visited
        q.push(neighbors[temp].at(i));
        dist[neighbors[temp].at(i)] = d;
      }
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  memset(dist, -1, sizeof(dist));
  memset(vis, -1, sizeof(dist));
  for (int i = 0; i < n; i++) {
    int node;
    scanf("%d", &node);
    int num;
    scanf("%d", &num);
    for (int j = 0; j < num; j++) {
      int temp;
      scanf("%d", &temp);
      neighbors[node].push_back(temp);
      // neighbors[temp].push_back(node);
    }
  }
  q.push(1); // begin from 1
  bfs(0);
  for (int i = 1; i <= n; i++) {
    printf("%d %d\\n", i, dist[i]);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 105             // number of nodes
vector<int> neighbors[N]; // start from 1

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
void bfs(int start, int key, int d) {
  if (start == key) {
    // if(d < dist[key])
    dist[key] = d;
    return;
  }
  for (int i = 0; i < neighbors[start].size(); i++) {
    bfs(neighbors[start].at(i), key, d + 1);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  memset(dist, -1, sizeof(dist));
  for (int i = 0; i < n; i++) {
    int node;
    scanf("%d", &node);
    int num;
    scanf("%d", &num);
    for (int j = 0; j < num; j++) {
      int temp;
      scanf("%d", &temp);
      neighbors[node].push_back(temp);
    }
  }
  for (int i = 1; i <= n; i++) {
    bfs(1, i, 0);
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\\n", i, dist[i]);
  }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int G[101][101];
int d[101];
bool checked[101];

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

  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;

    for (int i = 0; i < k; i++) {
      int v;
      cin >> v;
      G[id][v] = 1;
    }
  }

  for (int i = 1; i <= 100; i++) {
    d[i] = -1;
  }

  queue<int> q;
  q.push(1);

  int td = 0;
  while (q.size() != 0) {
    int size = q.size();
    for (int i = 0; i < size; i++) {
      int id = q.front();
      q.pop();
      if (checked[id]) {
        continue;
      }
      d[id] = td;
      checked[id] = true;
      for (int j = 1; j <= n; j++) {
        if (G[id][j] == 1 && !checked[j]) {
          q.push(j);
        }
      }
    }
    td++;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int G[101][101];
int d[101];

void explore(int id, int n, int cnt) {
  if (cnt == n) {
    return;
  }
  if (d[id] == -1) {
    d[id] = cnt;
  } else {
    d[id] = min(d[id], cnt);
  }

  for (int i = 1; i <= n; i++) {
    if (G[id][i] == 1) {
      explore(i, n, cnt + 1);
    }
  }
}

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

  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;

    for (int i = 0; i < k; i++) {
      int v;
      cin >> v;
      G[id][v] = 1;
    }
  }

  for (int i = 1; i <= 100; i++) {
    d[i] = -1;
  }

  explore(1, n, 0);

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

  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int BFS(int s, int t, const vector<vector<int>> &G) {
  vector<int> stock1;
  vector<int> stock2;
  stock1.push_back(s);
  vector<int> check(101, 0);
  int d = 0;
  while (1) {
    for (int i = 0; i < stock1.size(); i++) {
      if (stock1[i] == t)
        return d;
      for (int j = 0; j < G[stock1[i]].size(); j++) {
        if (check[G[stock1[i]][j]] == 0) {
          check[G[stock1[i]][j]] = 1;
          stock2.push_back(G[stock1[i]][j]);
        }
      }
    }
    if (stock2.empty())
      return -1;
    stock1.clear();
    stock1.swap(stock2);
    d++;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << i << " " << BFS(1, i, G) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int BFS(int s, int t, const vector<vector<int>> &G) {
  vector<int> stock1;
  vector<int> stock2;
  stock1.push_back(s);
  int d = 0;
  while (1) {
    for (int i = 0; i < stock1.size(); i++) {
      if (stock1[i] == t)
        return d;
      for (int j = 0; j < G[stock1[i]].size(); j++) {
        stock2.push_back(G[stock1[i]][j]);
      }
    }
    if (stock2.empty())
      return -1;
    stock1.clear();
    stock1.swap(stock2);
    d++;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << i << " " << BFS(1, i, G) << endl;
  }
  return 0;
}