alexjercan/bugnet huggingface dataset:
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

const int INF = 100000000;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> node(n + 1);
  for (int i = 0; i < n; i++) {
    int u, k, tmp;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> tmp;
      node[u].push_back(tmp);
    }
  }

  // search
  vector<int> res(n + 1, INF);
  queue<int> que;
  queue<int> copied;
  queue<int> zeros;
  que.push(1);

  int count = 0;
  for (int i = 0; i < n; i++) {
    while (que.size()) {
      int k = que.front();
      que.pop();
      // cout << "-> " << k << endl;
      if (res[k] >= INF)
        res[k] = count;
      for (int v : node[k]) {
        if (res[v] >= INF)
          copied.push(v);
      }
    }
    count++;
    que = queue<int>(copied);
    copied = queue<int>(zeros);
  }

  // output
  for (int i = 1; i <= n; i++) {
    if (res[i] >= INF)
      res[i] = -1;
    cout << i << " " << res[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

const int INF = 100000000;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> node(n + 1);
  for (int i = 0; i < n; i++) {
    int u, k, tmp;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> tmp;
      node[u].push_back(tmp);
    }
  }

  // search
  vector<int> res(n + 1, INF);
  queue<int> que;
  queue<int> copied;
  queue<int> zeros;
  que.push(1);

  int count = 0;
  for (int i = 0; i < n; i++) {
    while (que.size()) {
      int k = que.front();
      que.pop();
      // cout << "-> " << k << endl;
      if (res[k] >= INF)
        res[k] = count;
      for (int v : node[k]) {
        copied.push(v);
      }
    }
    count++;
    que = copied;
    copied = zeros;
  }

  // output
  for (int i = 1; i <= n; i++) {
    if (res[i] >= INF)
      res[i] = -1;
    cout << i << " " << res[i] << endl;
  }

  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: 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);
    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;
}

Error: TLE

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);
    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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INFTY = (1 << 21);
int n;
vv(int, M, MAX, MAX, 0);
vi d(MAX, INFTY);
inline void bfs(int s) {
  deque<int> dq;
  dq.eb(s);
  d[s] = 0;
  int u;
  while (!dq.empty()) {
    u = dq.front();
    dq.pop_front();
    rep(v, 0, n) {
      if (M[u][v] == 0 || d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      dq.eb(v);
    }
  }
  rep(i, 0, n) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}
int main() {
  cin >> n;
  int u, k, v;
  rep(i, 0, n) {
    cin >> u >> k;
    --u;
    rep(j, 0, k) {
      cin >> v;
      M[u][--v] = 1;
    }
  }
  bfs(0);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
vi M(MAX, -1);
int main() {
  int n;
  cin >> n;
  M[1] = 0;
  deque<pair<int, int>> dq;
  int u, k, v;
  rep(i, 0, n) {
    cin >> u >> k;
    rep(j, 0, k) {
      cin >> v;
      if (u == 1)
        M[v] = 1;
      else
        dq.eb(mp(u, v));
    }
  }
  while (!dq.empty()) {
    tie(u, v) = dq.front();
    dq.pop_front();
    if (M[u] == -1)
      dq.eb(mp(u, v));
    else if (M[v] == -1 || M[v] > M[u] + 1)
      M[v] = M[u] + 1;
  }
  rep(i, 1, n + 1) { cout << i << " " << M[i] << endl; }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i = 0; i < N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j = 0; j < n; j++) {
      int t;
      cin >> t;
      t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front();
    que.pop();
    int x = tmp.first, depth = tmp.second;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      continue;
    }
    for (int i = 0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

  for (int i = 0; i < N; i++) {
    cout << i + 1 << " " << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i = 0; i < N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j = 0; j < n; j++) {
      int t;
      cin >> t;
      t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front();
    que.pop();
    int x = tmp.first, depth = tmp.second;
    if (N < depth)
      continue;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      ans[x] = min(ans[x], depth);
    }
    for (int i = 0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

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

#include <iostream>
using namespace std;

typedef struct {
  int A[1000];
  int ans;
  bool c;
} node;
node T[1000];
int Q[10000000];
int head = 0;
int tail = 1;
int n;

void function(int num) {
  while (true) {
    if (head == tail)
      break;
    num = Q[head++];
    T[num].c = true;
    for (int i = 2; i <= n; i++) {
      if (T[num].A[i] == 1 && T[i].c == false) {
        if (T[i].ans == 0) {
          T[i].ans = T[num].ans + 1;
          T[num].A[i] = 0;
        }
        Q[tail++] = i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }
}

int main() {
  int num;
  int son;
  int z;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    cin >> son;
    for (int j = 0; j < son; j++) {
      cin >> z;
      T[num].A[z] = 1;
    }
  }
  Q[0] = 1;
  function(1);
  T[1].ans = 0;
  for (int i = 2; i <= n; i++) {
    if (T[i].ans == 0) {
      T[i].ans = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << i << " " << T[i].ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

typedef struct {
  int A[1000];
  int ans;
  bool c;
} node;
node T[1000];
int Q[10000000];
int head = 0;
int tail = 1;
int n;

void function(int num) {
  while (true) {
    if (head == tail)
      break;
    num = Q[head++];
    for (int i = 2; i <= n; i++) {
      if (T[num].A[i] == 1) {
        if (T[i].ans == 0) {
          T[i].ans = T[num].ans + 1;
          T[num].A[i] = 0;
        }
        Q[tail++] = i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }
}

int main() {
  int num;
  int son;
  int z;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    cin >> son;
    for (int j = 0; j < son; j++) {
      cin >> z;
      T[num].A[z] = 1;
    }
  }
  Q[0] = 1;
  function(1);
  T[1].ans = 0;
  for (int i = 2; i <= n; i++) {
    if (T[i].ans == 0) {
      T[i].ans = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << i << " " << T[i].ans << endl;
  }

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

#include <iostream>
#include <queue>
using namespace std;
int main() {

  int n, g[100][100] = {}, tmp1, amo, tmp2;
  queue<int> q;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp1 >> amo;
    for (int j = 0; j < amo; j++) {
      cin >> tmp2;
      g[tmp1 - 1][tmp2 - 1] = 1;
    }
  }
  int dis[100] = {}, flag[100] = {1};
  for (int i = 0; i < n; i++)
    if (i)
      dis[i] = -1;
  q.push(0);
  for (; !q.empty();) {
    for (int i = 0; i < n; i++)
      if (g[q.front()][i] && flag[i] != 1) {
        dis[i] = dis[q.front()] + 1;
        flag[i] = 1;
        q.push(i);
      }
    q.pop();
  }
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << dis[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n, g[100][100], f[100], c1[100], c2[100];
void bfs(int now, int far) {
  for (int i = 0; i < n; i++)
    if (g[now][i])
      f[i] = min(f[i], far + 1);
  for (int i = 0; i < n; i++)
    if (g[now][i] && c1[i] != 1)
      bfs(i, far + 1);
}
int main() {

  int a, b, tmp;
  cin >> n;
  for (int i = 0; i < n; i++) {
    if (i)
      f[i] = 1000;
    cin >> a >> b;
    for (int j = 0; j < b; j++) {
      cin >> tmp;
      g[a - 1][tmp - 1] = 1;
    }
  }

  bfs(0, 0);

  for (int i = 0; i < n; i++)
    if (f[i] == 1000)
      f[i] = -1;
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << f[i] << endl;

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

#include <iostream>

using namespace std;

#define N_MAX 100

int Adj[N_MAX][N_MAX] = {};
int Min_d[N_MAX];

void bfs(int v, int d, int num_node);

int main() {
  int n, i, u, k, v, j;

  cin >> n;

  for (i = 0; i < n; i++)
    Min_d[i] = -1;

  //??£??\\???????????????
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    while (k-- > 0) {
      cin >> v;
      Adj[u - 1][v - 1] = 1;
    }
  }
  /*
      cout << "show Adjacency matrix" << endl;
      for (i = 0; i < n; i++) {
          for (j = 0; j < n ; j++) {
              if (j == n - 1)
                  cout << Adj[i][j] << endl;
              else
                  cout << Adj[i][j] << ' ';
          }
      }
  */
  //??¢?´¢??????
  Min_d[0] = 0;
  bfs(1, 1, n);

  //????????????
  for (i = 1; i <= n; i++) {
    cout << i << ' ' << Min_d[i - 1] << endl;
  }

  return 0;
}

void bfs(int v, int d, int num_node) {
  int i;
  //    cout << "bfs node : " << v << endl;

  for (i = 1; i <= num_node; i++) {
    if (Adj[v - 1][i - 1] == 1) {
      if (Min_d[i - 1] == -1 || Min_d[i - 1] > d) {
        Min_d[i - 1] = d;
        bfs(i, d + 1, num_node);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define N_MAX 100

int Adj[N_MAX][N_MAX] = {};
int Min_d[N_MAX];

void bfs(int v, int d, int num_node);

int main() {
  int n, i, u, k, v, j;

  cin >> n;

  for (i = 0; i < n; i++)
    Min_d[i] = -1;

  //??£??\\???????????????
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    while (k-- > 0) {
      cin >> v;
      Adj[u - 1][v - 1] = 1;
    }
  }
  /*
      cout << "show Adjacency matrix" << endl;
      for (i = 0; i < n; i++) {
          for (j = 0; j < n ; j++) {
              if (j == n - 1)
                  cout << Adj[i][j] << endl;
              else
                  cout << Adj[i][j] << ' ';
          }
      }
  */
  //??¢?´¢??????
  Min_d[0] = 0;
  bfs(1, 1, n);

  //????????????
  for (i = 1; i <= n; i++) {
    cout << i << ' ' << Min_d[i - 1] << endl;
  }

  return 0;
}

void bfs(int v, int d, int num_node) {
  int i;
  //    cout << "bfs node : " << v << endl;

  for (i = 1; i <= num_node; i++) {
    if (Adj[v - 1][i - 1] == 1) {
      if (Min_d[i - 1] == -1 || Min_d[i - 1] > d)
        Min_d[i - 1] = d;
      bfs(i, d + 1, num_node);
    }
  }
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
#define INF 9999999

int graph[101][101];
int dis[101];    // 距離を格納
bool check[101]; // 訪問済みチェック
int main() {
  int n;
  cin >> n;
  fill(dis, dis + 101, INF);
  dis[1] = 0;
  check[1] = true;
  for (int i = 0; i < n; i++) {
    int u, k, v;
    cin >> u >> k;
    for (int j = 1; j <= k; j++) {
      graph[u][0] = k;
      cin >> graph[u][j];
    }
  }
  queue<int> qu;
  qu.push(1);
  while (!qu.empty()) {
    int num = qu.front();
    qu.pop();
    for (int j = 1; j <= graph[num][0]; j++) {
      int temp = graph[num][j];
      if (check[temp]) { // 訪問済みならば
        continue;
      } else {
        check[temp] = true;
      }
      dis[temp] = dis[num] + 1;
      qu.push(temp);
    }
  }
  // 表示部
  for (int i = 1; i <= n; i++) {
    cout << i << " ";
    if (dis[i] >= INF)
      cout << -1 << "\\n";
    else
      cout << dis[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
#define INF 9999999

int G[101][101];
int dis[101];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 1; j <= k; j++) {
      G[u][0] = k;
      cin >> G[u][j];
    }
  }
  fill(dis, dis + 101, INF);
  dis[1] = 0;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int num = Q.front();
    Q.pop();
    for (int i = 1; i <= G[num][0]; i++) {
      int k = G[num][i];
      dis[k] = min(dis[num] + 1, dis[k]);
      Q.push(k);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << i << " ";
    if (dis[i] == INF)
      cout << -1 << "\\n";
    else
      cout << dis[i] << "\\n";
  }
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node {
  int u, k, v[99];
  int distance;
};

int main(int argc, char *argv[]) {
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> node[i].u >> node[i].k;

    for (int j = 0; j < node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance = -1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while (!que.empty()) {
    Node fro = que.front();

    for (int i = 0; i < fro.k; i++) {
      int x = fro.v[i] - 1;

      if (node[x].distance == -1) {
        node[x].distance = fro.distance + 1;

        que.push(node[x]);
      }
    }

    que.pop();
  }

  for (int i = 0; i < n; i++)
    cout << node[i].u << " " << node[i].distance << endl;
}

Error: TLE

Fixed submission:

//
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node {
  int u, k, v[99];
  int distance;
};

int main(int argc, char *argv[]) {
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> node[i].u >> node[i].k;

    for (int j = 0; j < node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance = -1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while (!que.empty()) {
    Node fro = que.front();

    for (int i = 0; i < fro.k; i++) {
      int x = fro.v[i] - 1;

      if (node[x].distance == -1)
        node[x].distance = fro.distance + 1;

      que.push(node[x]);
    }

    que.pop();
  }

  for (int i = 0; i < n; i++)
    cout << node[i].u << " " << node[i].distance << endl;
}
Problem p02239 (C++) — buggy submission, verdict: 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) {
  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

Error: 0

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) {
  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
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n;
  bool connected[200][200] = {};

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int u, k, v;

    scanf("%d%d", &u, &k);
    u--;

    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;

      connected[u][v] = true;
    }
  }

  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];

  for (int i = 0; i < n; i++)
    ans[i] = -1;

  q1.push(0);

  for (int i = 0; q1.size(); i++) {

    while (q1.size()) {
      int p = q1.front();
      q1.pop();

      if (visited[p])
        continue;
      visited[p] = true;

      ans[p] = i;

      for (int j = 0; j < n; j++) {
        if (connected[p][j]) {
          q2.push(j);
        }
      }
    }

    q1 = q2;
    q2 = queue<int>();
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n;
  bool connected[200][200] = {};

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int u, k, v;

    scanf("%d%d", &u, &k);
    u--;

    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;

      connected[u][v] = true;
    }
  }

  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];

  for (int i = 0; i < n; i++)
    ans[i] = -1;

  q1.push(0);

  for (int i = 0; q1.size(); i++) {

    while (q1.size()) {
      int p = q1.front();
      q1.pop();

      if (visited[p])
        continue;
      visited[p] = true;

      ans[p] = i;

      for (int j = 0; j < n; j++) {
        if (connected[p][j]) {
          q2.push(j);
        }
      }
    }

    q1 = q2;
  }

  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);

  return 0;
}