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

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

const int N = 100;
int n;
int d[N + 1];
vector<vector<int>> ns;
queue<int> q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> v;
  ns.push_back(v);
  cin >> n;
  FOR(i, 1, n + 1) {
    int id, k, x;
    cin >> id;
    vector<int> v;
    cin >> k;
    REP(j, k) {
      cin >> x;
      v.push_back(x);
    }
    ns.push_back(v);
  }

  FOR(i, 1, n + 1) d[i] = -1;

  int u = 1;
  q.push(u);
  d[u] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    REP(i, ns[u].size()) {
      int v = ns[u][i];
      if (d[v] == -1) {
        q.push(v);
        d[v] = d[u] + 1;
      }
    }
  }

  FOR(i, 1, n + 1) { cout << i << " " << d[i] << endl; }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

const int N = 100;
int n;
int d[N + 1];
vector<vector<int>> ns;
queue<int> q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> v;
  ns.push_back(v);
  cin >> n;
  FOR(i, 1, n + 1) {
    int id, k, x;
    cin >> id;
    vector<int> v;
    cin >> k;
    REP(j, k) {
      cin >> x;
      v.push_back(x);
    }
    ns.push_back(v);
  }

  FOR(i, 1, n + 1) d[i] = -1;

  int u = 1;
  q.push(u);
  d[u] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    REP(i, ns[u].size()) {
      int v = ns[u][i];
      q.push(v);
      if (d[v] == -1)
        d[v] = d[u] + 1;
    }
  }

  FOR(i, 1, n + 1) { cout << i << " " << d[i] << endl; }
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <queue>
#include <stdio.h>

#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int n;
int M[N][N]; // ??£??\\??????
int d[N];    // ???????????¢

void bfs() {
  int i;
  for (i = 0; i < n; i++) {
    d[i] = -1;
  }

  int u;
  u = 0;
  d[u] = 0;
  queue<int> q;
  q.push(u);

  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (i = 0; i < n; i++) {
      if (M[u][i] == 0)
        continue;
      if (d[i] != -1)
        continue;
      q.push(i);
      d[i] = d[u] + 1;
    }
  }

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

int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  // input graph
  int u, k, v;
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &v);
      M[u - 1][v - 1] = 1;
    }
  }

  bfs();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>

#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int n;
int M[N][N]; // ??£??\\??????
int d[N];    // ???????????¢

void bfs() {
  int i;
  for (i = 0; i < n; i++) {
    d[i] = -1;
  }

  int u;
  u = 0;
  d[u] = 0;
  queue<int> q;
  q.push(u);

  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (i = 0; i < n; i++) {
      if (M[u][i] == 0)
        continue;
      q.push(i);
      if (d[i] == -1) {
        d[i] = d[u] + 1;
      }
    }
  }

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

int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  // input graph
  int u, k, v;
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &v);
      M[u - 1][v - 1] = 1;
    }
  }

  bfs();

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


Error: -6
terminate called after throwing an instance of 'std::__ios_failure'
  what():  basic_ios::clear: iostream error


Fixed submission:


#include <iostream>
#include <queue>

using namespace std;

const static int N = 100;
const static 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[u] != 0) {
        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, v, k;
  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; k++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int z[100], n, w[100];
int a[100][100];

void bfs() {
  int m;
  z[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    m = q.front();
    q.pop();
    if (w[m] == 1)
      continue;
    w[m] = 1;
    for (int i = 0; i < n; i++) {
      if (a[m][i] == 1) {
        q.push(i);
        z[i] = min(z[i], z[m] + 1);
      }
    }
  }
}

int main() {
  int p, x;
  for (int i = 0; i < 100; i++) {
    z[i] = w[i] = INF;
    for (int j = 0; j < 100; j++)
      a[i][j] = 0;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p >> p;
    for (int j = 0; j < p; j++) {
      cin >> x;
      a[i][x - 1] = 1;
    }
  }
  bfs();
  for (int i = 0; i < n; i++) {
    if (z[i] != INF)
      cout << i + 1 << " " << z[i] << endl;
    else
      cout << i + 1 << " " << -1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int z[100], n;
int a[100][100];

void bfs() {
  int m;
  z[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    m = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (a[m][i] == 1) {
        q.push(i);
        z[i] = min(z[i], z[m] + 1);
      }
    }
  }
}

int main() {
  int p, x;
  for (int i = 0; i < 100; i++) {
    z[i] = INF;
    for (int j = 0; j < 100; j++)
      a[i][j] = 0;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p >> p;
    for (int j = 0; j < p; j++) {
      cin >> x;
      a[i][x - 1] = 1;
    }
  }
  bfs();
  for (int i = 0; i < n; i++) {
    if (z[i] != INF)
      cout << i + 1 << " " << z[i] << endl;
    else
      cout << i + 1 << " " << -1 << endl;
  }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: 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);
        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;
}

Error: MLE

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);
        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, verdict: Runtime Error

#define scanf_s scanf
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 100
// #define MAX_ 1000
struct Graph {
  int id, k;
  list<int> node;
  int d = -1;
};
Graph G[MAX];
// int cou = 0;
void Breadth_cou(Graph *g, int cou) {
  for (auto i = g->node.begin(); i != g->node.end(); ++i) {
    if (G[*i].d == -1 || G[*i].d > cou) {
      if (G[*i].d != -1) {
        Breadth_cou(&G[*i], cou + 1);
      }
      G[*i].d = cou;
    }
  }
}
void Breadth(Graph *g) {
  if (g->k > 0) {
    Breadth_cou(g, g->d + 1);
    g->k = 0;
    for (auto i = g->node.begin(); i != g->node.end(); ++i) {
      Breadth(&G[*i]);
    }
  }
}
int main(void) {
  int u, xk, v, n;
  scanf_s("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf_s("%d %d", &u, &xk);
    G[u - 1].id = u - 1;
    G[u - 1].k = xk;
    for (int j = 0; j < xk; ++j) {
      scanf_s("%d", &v);
      G[u - 1].node.push_back(v - 1);
    }
  }
  G[0].d = 0;
  Breadth(&G[0]);
  for (int i = 0; i < n; ++i) {
    printf("%d %d\\n", G[i].id + 1, G[i].d);
  }
}

Error: 0

Fixed submission:

#define scanf_s scanf
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 100
// #define MAX_ 1000
struct Graph {
  int id, k;
  list<int> node;
  int d = -1;
};
Graph G[MAX];
// int cou = 0;
void Breadth_cou(Graph *g, int cou) {
  if (g->k >= 0) {
    for (auto i = g->node.begin(); i != g->node.end(); ++i) {
      if (G[*i].d == -1)
        G[*i].d = cou;
    }
  }
  g->k = 0;
}
void Breadth(Graph *g) {
  if (g->k >= 0) {
    Breadth_cou(g, g->d + 1);
    for (auto i = g->node.begin(); i != g->node.end(); ++i) {
      Breadth(&G[*i]);
    }
  }
}

int main(void) {
  int n, u, xk, v;
  scanf_s("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf_s("%d %d", &u, &xk);
    G[u - 1].id = u - 1;
    G[u - 1].k = xk;
    for (int j = 0; j < xk; ++j) {
      scanf_s("%d", &v);
      G[u - 1].node.push_back(v - 1);
    }
  }
  G[0].d = 0;
  Breadth(&G[0]);
  for (int i = 0; i < n; ++i) {
    printf("%d %d\\n", G[i].id + 1, G[i].d);
  }
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = a; i < (n); i++)
#define mod (ll)(1e9 + 7)
#define int ll

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[101][101] = {{}}, d[101] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for (int i = 1; i <= n; i++)
    d[i] = -1;
  d[v] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (graph[u][i] == 0)
        continue;
      if (d[i] != -1)
        continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
  ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = a; i < (n); i++)
#define mod (ll)(1e9 + 7)
#define int ll

#define N 100
#define no -1

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for (int i = 1; i <= n; i++)
    d[i] = no;
  d[v] = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (graph[u][i] == 0)
        continue;
      if (d[i] != no)
        continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
  ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

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


Error: TLE

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

/**
 * Bredth first search.
 * graphs could be circular.
 * there could be a better answer later.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N] = {0};

void bfs(int n, int dd) {
  int s[N] = {0}; // this needs to be a stack member.
  int k = adj[n][0];
  //	cout << n << ' ' << dd << ' ' << k << endl;
  int kk = 0;
  for (int j = 1; j <= k; j++) {
    int nn = adj[n][j];
    if (d[nn] != -1 && d[nn] <= dd)
      continue; // this condition is important.
    d[nn] = dd;
    s[kk++] = nn;
    //	cout << nn << endl;
  }

  for (int j = 0; j < kk; j++) {
    bfs(s[j], dd + 1);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int j, k;
    cin >> j >> k;
    d[j] = -1;
    adj[j][0] = k;
    for (int m = 1; m <= k; m++) {
      int a;
      cin >> a;
      adj[j][m] = a;
    }
  }

  d[1] = 0;

  bfs(1, 1);

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

  return 0;
}

Error: 0

Fixed submission:

/**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N];

void bfs(int n, int dd) {
  int k = adj[n][0];
  d[n] = dd++;
  for (int j = 1; j <= k; j++) {
    bfs(adj[n][j], dd);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int j, k;
    cin >> j >> k;
    adj[j][0] = k;
    for (int m = 1; m <= k; m++) {
      int a;
      cin >> a;
      adj[j][m] = a;
    }
    d[i] = -1;
  }

  bfs(1, 0);

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

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

#include <algorithm>
#include <queue>
#include <stdio.h>

#define MAX 100

int G[MAX + 1][MAX + 1], d[MAX + 1];
int n;
std::queue<int> Q;

void bfs() {
  d[1] = 0;

  Q.push(1);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int i = 1; i <= n; ++i) {
      if (G[cur][i] == 0)
        continue;
      if (d[i] != -1)
        continue;

      Q.push(i);
      d[i] = d[cur] + 1;
    }
  }
}

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

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

Error: TLE

Fixed submission:


#include <algorithm>
#include <queue>
#include <stdio.h>

#define MAX 100

int G[MAX + 1][MAX + 1], d[MAX + 1];
int n;
std::queue<int> Q;

void bfs() {
  d[1] = 0;

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

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

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

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

struct VRTX {
  int v[101] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[101];

void breadthFirstSearch(int x, int dist);

int main() {
  int i, j, id, dim;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> id >> dim;
    for (j = 0; j < dim; j++) {
      cin >> u[id].v[j];
    }
  }

  breadthFirstSearch(1, 0);

  for (i = 1; i <= n; i++) {
    cout << i << " " << u[i].dist << endl;
  }

  return 0;
}

void breadthFirstSearch(int x, int dist) {
  int i, nextdist;

  if (u[x].dist == -1)
    u[x].dist = dist;
  else if (u[x].dist <= dist)
    return;
  else
    u[x].dist = dist;

  i = 0;
  nextdist = u[x].dist + 1;
  while (u[x].v[i] != 0) {
    breadthFirstSearch(u[x].v[i], nextdist);
    i++;
  }
}

Error: 0

Fixed submission:

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

struct VRTX {
  int v[100] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[100];

void breadthFirstSearch(int x, int dist);

int main() {
  int i, j, id, dim;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> id >> dim;
    for (j = 0; j < dim; j++) {
      cin >> u[id].v[j];
    }
  }

  breadthFirstSearch(1, 0);

  for (i = 1; i <= n; i++) {
    cout << i << " " << u[i].dist << endl;
  }

  return 0;
}

void breadthFirstSearch(int x, int dist) {
  int i, nextdist;

  if (u[x].dist == -1)
    u[x].dist = dist;
  else if (u[x].dist == dist)
    return;
  else
    u[x].dist = min(u[x].dist, dist);

  i = 0;
  nextdist = u[x].dist + 1;
  while (u[x].v[i] != 0) {
    breadthFirstSearch(u[x].v[i], nextdist);
    i++;
  }
}
Problem p02239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N], 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 v, k, u;
  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;
    for (int j = 0; j < k; j++) {
      cin >> v;
      M[u - 1][v - 1] = 1;
    }
  }

  bfs(0);

  return 0;
}

Error: TLE

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], 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()) {
    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 v, k, u;
  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;
    for (int j = 0; j < k; j++) {
      cin >> v;
      M[u - 1][v - 1] = 1;
    }
  }

  bfs(0);

  return 0;
}