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

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

using namespace std;

const int N = 100;

int main() {
  int A[N][N] = {{0}};

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u;
    cin >> u;

    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      int x;
      cin >> x;
      A[u - 1][x - 1] = 1;
    }
  }

  vector<int> d(n, -1);
  using int_pair = pair<int, int>;
  queue<int_pair> Q;
  Q.push(int_pair(0, 0));
  while (!Q.empty()) {
    auto p = Q.front();
    Q.pop();

    int k = p.first;
    int t = p.second;
    if (d[k] == -1) {
      d[k] = t;
    } else if (d[k] < t) {
      continue;
    } else {
      d[k] = t;
    }
    for (int i = 0; i < n; ++i) {
      if (A[k][i] != 0) {
        Q.push(int_pair(i, t + 1));
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int N = 100;

int main() {
  int A[N][N] = {{0}};

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u;
    cin >> u;

    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      int x;
      cin >> x;
      A[u - 1][x - 1] = 1;
    }
  }

  vector<int> d(n, -1);
  using int_pair = pair<int, int>;
  queue<int_pair> Q;
  Q.push(int_pair(0, 0));
  while (!Q.empty()) {
    auto p = Q.front();
    Q.pop();

    int k = p.first;
    int t = p.second;
    if (d[k] > t)
      continue;

    if (d[k] == -1) {
      d[k] = t;
    } else {
      d[k] = min(d[k], t);
    }
    for (int i = 0; i < n; ++i) {
      if (A[k][i] != 0)
        Q.push(int_pair(i, t + 1));
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i + 1 << " " << d[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() {
  queue<int> Q;
  int n, u, k, vv, cur, dst;
  cin >> n;
  vector<int> D(n, -1), check(n, 0);
  vector<vector<int>> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(), v[u - 1].end());
  }
  D[0] = 0;
  check[0] = 1;
  Q.push(1);
  while (!Q.empty()) {
    cur = Q.front();
    Q.pop();
    for (int i = 0; i < v[cur - 1].size(); ++i) {
      dst = v[cur - 1][i];
      if (check[dst - 1] == 0) {
        D[dst - 1] = D[cur - 1] + 1;
        check[dst - 1] = 1;
        Q.push(dst);
      }
    }
  }
  for (int i = 0; i < n; ++i)
    cout << i + 1 << " " << D[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  queue<int> Q;
  int n, u, k, vv, cur, dst;
  cin >> n;
  vector<int> D(n, -1);
  vector<vector<int>> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(), v[u - 1].end());
  }
  D[0] = 0;
  Q.push(1);
  while (!Q.empty()) {
    cur = Q.front();
    Q.pop();
    for (int i = 0; i < v[cur - 1].size(); ++i) {
      dst = v[cur - 1][i];
      if (D[dst - 1] == -1)
        D[dst - 1] = D[cur - 1] + 1;
      Q.push(dst);
    }
  }
  for (int i = 0; i < n; ++i)
    cout << i + 1 << " " << D[i] << endl;
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#define INFTY -1
using namespace std;

int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k) {
  queue<int> q;
  q.push(k);
  int u;

  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (a[u][v] == 0)
        continue;
      if (d[v] == INFTY) {
        d[v] = d[u] + 1;
        q.push(v);
        // cout << v << " " << d[v] << endl;
      }
    }
  }
}

int main() {

  int u, k, x;
  cin >> n;

  d[0] = 0;
  for (int i = 1; i < n; i++)
    d[i] = INFTY;

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

  bfs(0);

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

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#define INFTY -1
using namespace std;

int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k) {
  queue<int> q;
  q.push(k);
  int u;

  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (a[u][v] == 0)
        continue;
      if (d[v] == INFTY) {
        d[v] = d[u] + 1;
        // cout << v << " " << d[v] << endl;
      }
      q.push(v);
    }
  }
}

int main() {

  int u, k, x;
  cin >> n;

  d[0] = 0;
  for (int i = 1; i < n; i++)
    d[i] = INFTY;

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

  bfs(0);

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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src) {
  // cerr<<"bfs root = "<<src<<endl;
  queue<int> q;
  q.push(src);
  D[src] = 0;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    // cerr<<"visiting "<<cur<<' '<<D[cur]<<endl;
    for (int dst = 0; dst < n; dst++) {
      if (matrix[cur][dst] == 1 && D[dst] == -1) {
        D[dst] = D[cur] + 1;
        q.push(dst);
      }
    }
  }
};

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

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      matrix[i][j] = 0;
    }
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < l[i].size(); j++) {
      matrix[i - 1][l[i][j] - 1] = 1;
    }
  }

  bfs(0);

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

Error: 0
bfs root = 0
visiting 0 0
visiting 1 1
visiting 3 1
visiting 2 2


Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src) {
  cerr << "bfs root = " << src << endl;
  queue<int> q;
  q.push(src);
  D[src] = 0;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    cerr << "visiting " << cur << ' ' << D[cur] << endl;
    for (int dst = 0; dst < n; dst++) {
      if (matrix[cur][dst] == 1 && D[dst] == -1) {
        D[dst] = D[cur] + 1;
        q.push(dst);
      }
    }
  }
};

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

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      matrix[i][j] = 0;
    }
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < l[i].size(); j++) {
      matrix[i - 1][l[i][j] - 1] = 1;
    }
  }

  bfs(0);

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

#include <iostream>
#include <queue>
using namespace std;
void debug1();
int n, gragh[110][110] = {}, d[110] = {};
queue<int> q;

int main() {
  cin >> n;
  // input
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      gragh[u][t] = 1;
    }
  }
  // debug1();
  // search
  d[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    // cerr << "visiting " << cur << " Distance: " << d[cur] << endl;
    for (int i = 1; i <= n; i++) {
      if (d[i] == 0 && gragh[cur][i] == 1 && i != 1) {
        d[i] = d[cur] + 1;
        q.push(i);
      }
    }
  }

  // from 0 to 1
  for (int i = 2; i <= n; i++) {
    if (d[i] == 0)
      d[i] = -1;
  }

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

void debug1() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cerr << gragh[i][j] << " ";
    }
    cerr << endl;
  }
}

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


Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
void debug1();
int n, gragh[110][110] = {}, d[110] = {};
queue<int> q;

int main() {
  cin >> n;

  // inicialize
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      gragh[i][j] = -1;
    }
  }
  // input
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      gragh[u][t] = 1;
    }
  }
  debug1();
  // search
  d[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    cerr << "visiting " << cur << " Distance: " << d[cur] << endl;
    for (int i = 1; i <= n; i++) {
      if (d[i] == 0 && gragh[cur][i] == 1) {
        d[i] = d[cur] + 1;
        q.push(i);
      }
    }
  }
  // output
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

void debug1() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cerr << gragh[i][j] << " ";
    }
    cerr << endl;
  }
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <assert.h>
#include <iostream>
#include <queue>
#define MAX_V 100
using namespace std;

int adjacencyMatrix[MAX_V][MAX_V] = {};
int d[MAX_V];
int V;

void bfs(int start) {
  queue<int> Q;
  Q.push(start);

  for (int i = 0; i < V; ++i)
    d[i] = -1;

  d[start] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < V; ++i) {
      if (d[i] != -1)
        continue;
      if (adjacencyMatrix[u][i] == 1) {
        d[i] = d[u] + 1;
        Q.push(i);
      }
    }
  }
  return;
}

int main() {
  cin >> V;

  for (int i = 0; i < V; ++i) {
    int u, k;
    cin >> u >> k;
    assert(u = i + 1);
    for (int j = 0; j < k; ++j) {
      int tmp;
      cin >> tmp;
      adjacencyMatrix[i][tmp - 1] = 1;
    }
  }

  bfs(0);

  for (int i = 0; i < V; ++i)
    cout << i + 1 << " " << d[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <assert.h>
#include <iostream>
#include <queue>
#define INF 100000000
#define MAX_V 100
using namespace std;

int adjacencyMatrix[MAX_V][MAX_V] = {};
int d[MAX_V];
int V;

void bfs(int start) {
  queue<int> Q;
  Q.push(start);

  for (int i = 0; i < V; ++i)
    d[i] = INF;

  d[start] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < V; ++i) {
      if (d[i] != INF)
        continue;
      if (adjacencyMatrix[u][i] == 1)
        d[i] = d[u] + 1;
      Q.push(i);
    }
  }
  return;
}

int main() {
  cin >> V;

  for (int i = 0; i < V; ++i) {
    int u, k;
    cin >> u >> k;
    assert(u = i + 1);
    for (int j = 0; j < k; ++j) {
      int tmp;
      cin >> tmp;
      adjacencyMatrix[i][tmp - 1] = 1;
    }
  }

  bfs(0);

  for (int i = 0; i < V; ++i)
    cout << i + 1 << " " << d[i] << endl;

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

#include <iostream>
#include <queue>

using namespace std;

int D[100100]; // 始点からの距離を入れる

int g[1001][1001] = {{0}}; // 要素を0で初期化
void bfs(int src, int n) { // srcは開始位置この場合だと頂点1だから0
  // cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for (int dst = 0; dst < n; dst++) {
      if (g[cur][dst] == 1 && D[dst] == 0) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n) {
  cout << 1 << " " << 0 << endl;
  for (int i = 1; i < n; ++i) {
    if (D[i] == 0) {
      cout << i + 1 << " " << -1 << endl;
    } else {
      cout << i + 1 << " " << D[i] << endl;
    }
  }
}

int main() {
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n, a, b, c;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    for (int i = 0; i < b; ++i) {
      cin >> c;
      g[a - 1][c - 1] = 1; /* code */
    }
  }
  bfs(0, n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

int D[100100];             // 始点からの距離を入れる
int g[1001][1001] = {{0}}; // 要素を0で初期化
void bfs(int src, int n) { // srcは開始位置この場合だと頂点1だから0
  cerr << "bfs_root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting_" << cur << " " <<D[cur] <<endl;
    for (int dst = 0; dst < n; dst++) {
      if (g[cur][dst] == 1 && D[dst] == 0) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
}
void ans(int n) {
  for (int i = 0; i < n; ++i) {
    cout << i + 1 << D[i] << endl;
  }
}

int main() {
  // int g[1001][1001]={{0}};//要素を0で初期化
  int n, a, b, c;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    for (int i = 0; i < b; ++i) {
      cin >> c;
      g[a - 1][c - 1] = 1; /* code */
    }
  }
  bfs(0, n);
  ans(n);
  // for (int i = 0; i < n; ++i)
  // {
  //    for (int j = 0; j < n; ++j)
  //    {
  //     if (j==n-1)
  //     {
  //       cout << g[i][j] << endl;
  //     }else{
  //       cout << g[i][j] <<" ";
  //     }
  //    }
  // }
}
Problem p02239 (C++) — buggy submission, verdict: 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])
        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;
}


Error: TLE

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

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

int main() {
  queue<int> q;
  int n, k, u, max = 0;
  int g[101] = {}, c[101];
  int d[101][101] = {};
  int di;
  cin >> n;
  for (int i = 0; i < 101; i++)
    c[i] = -1;
  c[1] = 0;
  g[1] = 1;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    if (max < k)
      max = k;
    for (int j = 0; j < k; j++) {
      cin >> di;
      d[u][j] = di;
    }
  }

  q.push(1);

  int a;
  while (!q.empty()) {
    a = q.front();
    for (int i = 0; i <= max; i++) {

      if (d[a][i] > 1 && g[d[a][i]] == 0) {
        g[d[a][i]] = 1;
        q.push(d[a][i]);
        c[d[a][i]] = c[a] + 1;
      }
    }
    q.pop();
  }
  for (int i = 1; i <= n; i++)
    cout << i << " " << c[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  queue<int> q;
  int n, k, u, max = 0;
  int g[101] = {}, f[101];
  int d[101][101] = {};
  int di;
  cin >> n;
  for (int i = 0; i < 101; i++)
    f[i] = -1;
  f[1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    if (max < k)
      max = k;
    for (int j = 0; j < k; j++) {
      cin >> di;
      if (g[di] == 0) {
        g[di] = 1;
        d[u][j] = di;
      }
    }
  }

  q.push(1);

  int cnt = 1, fg;
  int a;
  while (!q.empty()) {
    fg = 0;
    a = q.front();
    for (int i = 0; i <= max; i++) {

      if (d[a][i] > 0) {
        fg = 1;
        q.push(d[a][i]);
        f[d[a][i]] = cnt;
      }
    }
    q.pop();
    if (fg == 1)
      cnt++;
  }
  for (int i = 1; i <= n; i++)
    cout << i << " " << f[i] << endl;
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>

using namespace std;
static const int N = 100;

int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> Q;
  Q.push(s);
  d[s] = 0;
  int u, v;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (v = 0; v < n; v++) {
      if (M[u][v] == 0)
        continue;
      if (d[v] != -1)
        continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
  for (int i = 0; i < n; i++)
    printf("%d %d\\n", i + 1, d[i]);
}

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;
    }
  }
  for (int i = 0; i < n; i++)
    d[i] = -1;
  bfs(0);
  return 0;
}


Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>

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

#include <cassert>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

const int MAXINT = 1000;

void bfs(std::vector<std::vector<int>> &adj_list, std::vector<int> &d,
         int root = 0) {
  std::queue<int> target_q;
  // std::queue<int> dist_q;
  target_q.push(root);
  d[root] = 0;
  // dist_q.push(0;)
  while (!target_q.empty()) {
    int vtx = target_q.front();
    target_q.pop();
    for (int const &u : adj_list[vtx]) {
      if (d[vtx] + 1 < d[u]) {
        target_q.push(u);
        // std::cout<< "/* message */" << std::endl;
        d[u] = d[vtx] + 1;
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int n; // .. 100
  std::cin >> n;
  std::vector<std::vector<int>> adj_list(n);
  std::vector<int> d(n, MAXINT);

  for (int q = 0; q < n; ++q) {
    int i_plus_1, k;
    std::cin >> i_plus_1;
    std::cin >> k;
    for (int p = 0; p < k; ++p) {
      int j_plus_1;
      std::cin >> j_plus_1;
      // adj[i_plus_1-1][j_plus_1-1] = true;
      adj_list[i_plus_1 - 1].push_back(j_plus_1 - 1);
    }
  }
  bfs(adj_list, d, 0);

  for (int i = 0; i < n; ++i) {

    // if(d[i] < MAXINT){
    std::cout << (i + 1) << " " << (d[i] == MAXINT ? -1 : d[i]) << std::endl;
  }

  // for(auto it = adj_list.begin(); it != adj_list.end(); ++it){
  //   std::cout << ": ";
  //   for(auto jt = (*it).begin(); jt != (*it).end(); ++jt){
  //     std::cout << (*jt) << ' ';
  //   }
  //   std::cout << std::endl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

const int MAXINT = 1000000000;

void bfs(std::vector<std::vector<int>> &adj_list, std::vector<int> &d,
         int root = 0) {
  std::queue<int> target_q;
  // std::queue<int> dist_q;
  target_q.push(root);
  d[root] = 0;
  // dist_q.push(0;)
  while (!target_q.empty()) {
    int vtx = target_q.front();
    target_q.pop();
    for (int const &u : adj_list[vtx]) {
      target_q.push(u);
      if (d[vtx] + 1 < d[u]) {
        // std::cout<< "/* message */" << std::endl;
        d[u] = d[vtx] + 1;
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int n; // .. 100
  std::cin >> n;
  std::vector<std::vector<int>> adj_list(n);
  std::vector<int> d(n, MAXINT);

  for (int q = 0; q < n; ++q) {
    int i_plus_1, k;
    std::cin >> i_plus_1;
    std::cin >> k;
    for (int p = 0; p < k; ++p) {
      int j_plus_1;
      std::cin >> j_plus_1;
      // adj[i_plus_1-1][j_plus_1-1] = true;
      adj_list[i_plus_1 - 1].push_back(j_plus_1 - 1);
    }
  }
  bfs(adj_list, d, 0);

  for (int i = 0; i < n; ++i) {

    // if(d[i] < MAXINT){
    std::cout << (i + 1) << " " << (d[i] == MAXINT ? -1 : d[i]) << std::endl;
  }

  // for(auto it = adj_list.begin(); it != adj_list.end(); ++it){
  //   std::cout << ": ";
  //   for(auto jt = (*it).begin(); jt != (*it).end(); ++jt){
  //     std::cout << (*jt) << ' ';
  //   }
  //   std::cout << std::endl;
  // }

  return 0;
}