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

#include <bits //stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct P {
  int cost, pos;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
long long a[101];
const long long inf = 800000000;
vector<long long> e[101];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r, k, c;
    cin >> r >> k;
    for (int i = 0; i < k; i++) {
      cin >> c;
      e[r].push_back(c);
    }
  }
  int p;
  cin >> p;
  for (int z = 0; z < p; z++) {
    int s, d, v;
    cin >> s >> d >> v;
    fill(a, a + n + 1, inf);
    priority_queue<P> p;
    p.push(P{1, s});
    a[s] = 1;
    while (!p.empty()) {
      P t = p.top();
      p.pop();
      for (int i = 0; i < (int)e[t.pos].size(); i++) {
        if (a[t.pos] + 1 < a[e[t.pos][i]]) {
          a[e[t.pos][i]] = a[t.pos] + 1;
          p.push(P{a[t.pos] + 1, e[t.pos][i]});
        }
      }
    }
    if (a[d] <= v)
      cout << a[d] << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  vector<int> a[100];
  int b[100];
  bool l[100];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r, k, t;
    cin >> r >> k;
    r--;
    for (int j = 0; j < k; j++) {
      cin >> t;
      t--;
      a[r].push_back(t);
    }
    sort(a[r].begin(), a[r].end());
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    s--;
    d--;
    fill(b, b + n, 100000);
    b[s] = 0;
    fill(l, l + n, false);
    l[s] = true;
    int sum = 0;
    multimap<int, int> m;
    for (int j = 0; j < a[s].size(); j++) {
      m.insert(map<int, int>::value_type(1, a[s][j]));
    }
    multimap<int, int>::iterator it = m.begin();
    while (!l[d]) {
      it = m.begin();
      sum = (*it).first + 1;
      if (sum > v)
        break;
      int e = (*it).second;
      m.erase(it);
      for (int j = 0; j < a[e].size(); j++) {
        l[a[e][j]] = true;
        b[a[e][j]] = min(b[a[e][j]], sum);
      }
    }
    if (l[d]) {
      cout << b[d] << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
Problem p00144 (C++) — buggy submission, verdict: Runtime Error

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

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back

int bfs(int s, int t, int c);

vector<vector<int>> g(100001);

int main() {
  int n;
  cin >> n;
  loop(i, n) {
    int r, k, t;
    cin >> r >> k;
    loop(j, k) {
      cin >> t;
      g[r].pb(t);
    }
  }
  int p;
  cin >> p;
  loop(i, p) {
    int s, d, v;
    cin >> s >> d >> v;
    int ans = bfs(s, d, v);
    if (ans != -1) {
      cout << v - ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

int bfs(int s, int t, int c) {
  vector<bool> check(100001, false);
  queue<int> fq, nq, sq;
  int cnt = 0;
  fq.push(s);
  if (c == 0) {
    return -1;
  }
  while (1) {
    c--;
    while (!fq.empty()) {
      int pos = fq.front();
      fq.pop();
      if (check[pos]) {
        continue;
      } else {
        check[pos] = true;
      }
      // cout << pos << endl;
      if (pos == t) {
        return c;
      }
      for (int i : g[pos]) {
        nq.push(i);
      }
    }
    if (c == 0 || nq.empty()) {
      return -1;
    }
    fq = nq;
    nq = sq;
    // cout << c << "====" << endl;
  }
}

Error: 0

Fixed submission:

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

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back

int bfs(int s, int t, int c);

vector<vector<int>> g(101);

int main() {
  int n;
  cin >> n;
  loop(i, n) {
    int r, k, t;
    cin >> r >> k;
    loop(j, k) {
      cin >> t;
      g[r].pb(t);
    }
  }
  int p;
  cin >> p;
  loop(i, p) {
    int s, d, v;
    cin >> s >> d >> v;
    int ans = bfs(s, d, v);
    if (ans != -1) {
      cout << v - ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

int bfs(int s, int t, int c) {
  vector<bool> check(101, false);
  queue<int> fq, nq, sq;
  fq.push(s);
  while (1) {
    c--;
    while (!fq.empty()) {
      int pos = fq.front();
      fq.pop();
      if (check[pos]) {
        continue;
      } else {
        check[pos] = true;
      }
      // cout << pos << endl;
      if (pos == t) {
        return c;
      } else {
        for (int i : g[pos]) {
          nq.push(i);
        }
      }
    }
    if (c == 0 || nq.empty()) {
      return -1;
    }
    fq = nq;
    nq = sq;
  }
}
Problem p00144 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> ro[101], dis;
queue<int> Q;
int n, s, d, v;
int r, kk;

void reset() {
  dis.resize(n);
  for (int i = 0; i < n; i++)
    dis[i] = 100000000;
}

int bfs() {
  Q.push(s);
  dis[s] = 1;
  int t, a;
  while (!Q.empty()) {
    t = Q.front();
    Q.pop();

    for (int i = 0; i < ro[t].size(); i++) {
      a = ro[t][i];
      if (dis[a] > dis[t] + 1) {
        dis[a] = dis[t] + 1;
        if (dis[t] + 1 < v && a != d)
          Q.push(a);
      }
    }
  }
  return dis[d];
}

int main() {

  cin >> n;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> r >> kk;
    ro[r - 1].resize(kk);
    for (int j = 0; j < kk; j++) {
      cin >> a;
      ro[r - 1][j] = a - 1;
    }
  }

  int p;
  int ans;
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> s >> d >> v;
    s--;
    d--;
    reset();
    ans = bfs();
    if (ans <= v)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> ro[1000];
int s, d, v;
int s2, d2;
int kk, n, r, md;
bool flg[10000];

void visit(int sn, int dis) {
  //  for(int i=1;i<=n;i++)cout << flg[i];
  // cout <<" ";

  if (sn == d - 1 && dis <= v)
    md = min(md, dis);
  else if (dis < v && dis < md) {
    for (int i = 0; i < ro[sn].size(); i++) {
      if (flg[ro[sn][i]] == 0) {
        //	cout <<sn+1 << "to"<<ro[sn][i] << endl;
        flg[ro[sn][i]] = 1;
        visit(ro[sn][i] - 1, dis + 1);
        flg[ro[sn][i]] = 0;
      }
    }
  }
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> r >> kk;
    ro[r - 1].resize(kk);
    for (int j = 0; j < kk; j++)
      cin >> ro[r - 1][j];
  }

  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> s >> d >> v;

    for (int j = 1; j <= n; j++)
      flg[j] = 0;
    md = 10000000;
    flg[s] = 1;
    visit(s - 1, 1);

    if (md != 10000000)
      cout << md << endl;
    else
      cout << "NA" << endl;
  }

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

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

int router[200][200];
int b[200][200];

int main(void) {
  int N;
  cin >> N;
  for (int n = 0; n < N; n++) {
    int r, k;
    cin >> r >> k;
    for (int i = 0; i < k; i++) {
      int tmp;
      cin >> tmp;
      b[r][tmp] = 1;
    }
  }

  int pac_n;
  cin >> pac_n;
  for (int p = 0; p < pac_n; p++) {
    int start, des, ttl;
    cin >> start >> des >> ttl;

    queue<pair<int, int>> que; //<id, now_ttl>
    que.push(make_pair(start, ttl));
    bool flg = false;
    for (int x = 0; x <= N; x++) {
      for (int y = 0; y <= N; y++) {
        router[x][y] = b[x][y];
      }
    }
    while (!que.empty()) {
      pair<int, int> q = que.front();
      que.pop();
      if (q.first == des) {
        cout << ttl - q.second + 1 << endl;
        flg = true;
        break;
      }
      if (q.second > 1) {
        for (int i = 0; i <= N; i++) {
          if (router[q.first][i] == 1) {
            router[q.first][i] = 0;
            que.push(make_pair(i, q.second - 1));
          }
        }
      }
    }
    if (!flg)
      cout << "NA" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int router[100][100];

int main(void) {
  int N;
  cin >> N;
  for (int n = 0; n < N; n++) {
    int r, k;
    cin >> r >> k;
    for (int i = 0; i < k; i++) {
      int tmp;
      cin >> tmp;
      router[r][tmp] = 1;
    }
  }

  int pac_n;
  cin >> pac_n;
  for (int p = 0; p < pac_n; p++) {
    int start, des, ttl;
    cin >> start >> des >> ttl;

    queue<pair<int, int>> que; //<id, now_ttl>
    que.push(make_pair(start, ttl));
    bool flg = false;
    while (!que.empty()) {
      pair<int, int> q = que.front();
      que.pop();
      if (q.first == des) {
        cout << ttl - q.second + 1 << endl;
        flg = true;
        break;
      }
      if (q.second != 1) {
        for (int i = 0; i <= N; i++) {
          if (router[q.first][i] == 1) {
            que.push(make_pair(i, q.second - 1));
          }
        }
      }
    }
    if (!flg)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00144 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<int> node[200];

int que(int s, int d, int v) {
  queue<P> qu;
  int c[200];
  memset(c, -1, sizeof(c));
  for (int i = 0; i < node[s].size(); i++) {
    if (v - 1 > 0)
      qu.push(P(node[s][i], v - 1));
  }
  while (!qu.empty()) {
    P a = qu.front();
    qu.pop();
    //      cout<<a.first<<" "<<a.second<<endl;
    if (a.first == d)
      return v - a.second + 1;
    for (int i = 0; i < node[a.first].size(); i++) {
      if (a.second - 1 > 0) {
        if (c[node[a.first][i]] == -1) {
          c[node[a.first][i]] = a.second - 1;
          qu.push(P(node[a.first][i], a.second - 1));
        }
      }
    }
  }
  return -1;
}
int main() {
  int n;
  cin >> n;
  for (; n--;) {
    int k, t;
    cin >> k >> t;
    for (int i = 0; i < t; i++) {
      int tmp;
      cin >> tmp;
      node[k].push_back(tmp);
    }
  }
  int p;
  cin >> p;
  for (; p--;) {
    int s, d, v;
    cin >> s >> d >> v;
    int ans = que(s, d, v);
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
typedef pair<int, int> P;
vector<int> node[200];

int que(int s, int d, int v) {
  queue<P> qu;
  for (int i = 0; i < node[s].size(); i++) {
    if (v - 1 > 0)
      qu.push(P(node[s][i], v - 1));
  }
  while (!qu.empty()) {
    P a = qu.front();
    qu.pop();
    //      cout<<a.first<<" "<<a.second<<endl;
    if (a.first == d)
      return v - a.second + 1;
    for (int i = 0; i < node[a.first].size(); i++) {
      if (a.second - 1 > 0)
        qu.push(P(node[a.first][i], a.second - 1));
    }
  }
  return -1;
}
int main() {
  int n;
  cin >> n;
  for (; n--;) {
    int k, t;
    cin >> k >> t;
    for (int i = 0; i < t; i++) {
      int tmp;
      cin >> tmp;
      node[k].push_back(tmp);
    }
  }
  int p;
  cin >> p;
  for (; p--;) {
    int s, d, v;
    cin >> s >> d >> v;
    int ans = que(s, d, v);
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00144 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<int> node[200];

int que(int s, int d, int v) {
  queue<P> qu;
  int c[200];
  memset(c, -1, sizeof(c));
  for (int i = 0; i < node[s].size(); i++) {
    if (v - 1 > 0)
      qu.push(P(node[s][i], v - 1));
  }
  while (!qu.empty()) {
    P a = qu.front();
    qu.pop();
    //      cout<<a.first<<" "<<a.second<<endl;
    if (a.first == d)
      return v - a.second + 1;
    for (int i = 0; i < node[a.first].size(); i++) {
      if (a.second - 1 > 0) {
        if (c[node[a.first][i]] == -1) {
          c[node[a.first][i]] = a.second - 1;
          qu.push(P(node[a.first][i], a.second - 1));
        }
      }
    }
  }
  return -1;
}
int main() {
  int n;
  cin >> n;
  for (; n--;) {
    int k, t;
    cin >> k >> t;
    for (int i = 0; i < t; i++) {
      int tmp;
      cin >> tmp;
      node[k].push_back(tmp);
    }
  }
  int p;
  cin >> p;
  for (; p--;) {
    int s, d, v;
    cin >> s >> d >> v;
    int ans = que(s, d, v);
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int main() {
  int n;
  int node[100][100];
  cin >> n;
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      node[i][j] = 0;

  for (int x = 0; x < n; x++) {
    int k, t;
    cin >> k >> t;
    for (int i = 0; i < t; i++) {
      int tmp;
      cin >> tmp;
      node[k][tmp] = 1;
      //	  cout<<node[k][tmp]<<endl;
      //  cout<<"nodeadd:"<<k<<" "<<tmp<<endl;
    }
  }

  int p;
  cin >> p;
  for (; p--;) {
    int s, d, v;
    cin >> s >> d >> v;
    queue<P> qu;
    int ans = -1;
    for (int i = 0; i <= n; i++) {
      if (node[s][i] == 1) {
        qu.push(P(i, 2));
      }
    }
    while (!qu.empty()) {
      P a = qu.front();
      qu.pop();
      if (a.second > v)
        break;
      if (a.first == d) {
        ans = a.second;
        break;
      }
      for (int i = 0; i <= n; i++) {
        if (node[a.first][i] == 1)
          qu.push(P(i, a.second + 1));
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int N_MAX = 100;

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
  queue<int> que;
  fill(visited, visited + n + 1, -1);
  que.push(src);
  visited[src] = 1;
  while (!que.empty()) {
    int current = que.front();
    que.pop();
    if (current == dst) {
      if (visited[dst] > limit)
        break;
      cout << visited[dst] << endl;
      return;
    }
    for (int next = 1; next <= n; next++) {
      if (adj[current][next] && visited[next] == -1) {
        que.push(next);
        visited[next] = visited[current] + 1;
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    fill(adj[r], adj[r] + n, false);
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      adj[r][t] = true;
    }
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    bfs(s, d, v);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int N_MAX = 100;
const int INF = 0x7fffff;

class Node {
public:
  int pos, ttl;
  Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
  queue<Node> que;
  fill(visited, visited + n + 1, INF);
  que.push(Node(src, limit - 1));
  while (!que.empty()) {
    Node current = que.front();
    que.pop();
    if (current.pos == dst) {
      cout << limit - current.ttl << endl;
      return;
    }
    if (current.ttl >= 0 && visited[current.pos] <= current.ttl)
      continue;
    visited[current.pos] = current.ttl;
    if (current.ttl == 0)
      continue;
    for (int next = 1; next <= n; next++) {
      if (adj[current.pos][next] && visited[next] > current.ttl &&
          current.ttl > 0) {
        que.push(Node(next, current.ttl - 1));
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    fill(adj[r], adj[r] + n + 1, false);
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      adj[r][t] = true;
    }
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    bfs(s, d, v);
  }
  return 0;
}
Problem p00144 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 59
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> e[101];
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    while (k--) {
      int t;
      cin >> t;
      e[r].push_back(t);
    }
  }
  int p;
  cin >> p;
  while (p--) {
    int s, d, v;
    cin >> s >> d >> v;
    queue<pair<int, int>> que;
    que.push(make_pair(1, s));
    bool p[101] = {};
    while (!que.empty()) {
      int c = que.front().second;
      if (c == d || que.front().first > v)
        break;
      if (!p[c]) {
        for (int i = 0; i < e[c].size(); i++) {
          que.push(make_pair(que.front().first + 1, e[c][i]));
        }
      }
      p[c] = true;
      que.pop();
    }
    if (que.empty() || que.front().first > v) {
      cout << "NA" << endl;
    } else {
      cout << que.front().first << endl;
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

// 59
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> e[101];
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    while (k--) {
      int t;
      cin >> t;
      e[r].push_back(t);
    }
  }
  int p;
  cin >> p;
  while (p--) {
    int s, d, v;
    cin >> s >> d >> v;
    queue<pair<int, int>> que;
    que.push(make_pair(1, s));
    while (!que.empty()) {
      int c = que.front().second;
      if (c == d || que.front().first > v)
        break;
      for (int i = 0; i < e[c].size(); i++) {
        que.push(make_pair(que.front().first + 1, e[c][i]));
      }
      que.pop();
    }
    if (que.empty() || que.front().first > v) {
      cout << "NA" << endl;
    } else {
      cout << que.front().first << endl;
    }
  }
  return 0;
}
Problem p00144 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 999999
using namespace std;
int n, p;
int r, k;
int t;
int s, d, v, cost[110][110];
void warshall_floyd();
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        cost[i][j] = 0;
      else {
        cost[i][j] = INF;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> r >> k;
    for (int j = 0; j < k; j++) {
      cin >> t;
      cost[r][t] = 1;
    }
  }
  warshall_floyd();
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> s >> d >> v;
    if (cost[s][d] + 1 <= v)
      cout << cost[s][d] + 1 << endl;
    else
      cout << "NA" << endl;
  }
}

void warshall_floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 9999999
using namespace std;
int n, p;
int r, k;
int t;
int s, d, v, cost[100][100];
void warshall_floyd();
int main() {
  cin >> n;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == j)
        cost[i][j] = 0;
      else {
        cost[i][j] = INF;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> r >> k;
    for (int j = 0; j < k; j++) {
      cin >> t;
      cost[i][t] = 1;
    }
  }
  warshall_floyd();
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> s >> d >> v;
    if (cost[s][d] + 1 <= v)
      cout << cost[s][d] + 1 << endl;
    else
      cout << "NA" << endl;
  }
}

void warshall_floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}
Problem p00144 (C++) — buggy submission, verdict: Runtime Error

#include <list>
#include <set>
#include <stdio.h>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;

int dikstra(int s, int e, int deep) {
  int c = 0;

  list<int> que;
  set<int> done;
  set<int> done2;
  que.push_back(s);
  done.insert(s);

  while (1) {
    c++;
    if (c >= deep)
      return -1;

    done2.clear();

    int len = que.size();
    for (int i = 0; i < len; i++) {

      int v = que.front();
      que.pop_front();

      for (int j = 0; j < nodes[v].length; j++) {
        if (nodes[v].ks[j] == e) {
          return c + 1;
        } else {
          int k = nodes[v].ks[j];
          if (done.find(k) == done.end()) {
            que.push_back(k);
            done2.insert(k);
          }
        }
      }
    }
    done.insert(done2.begin(), done2.end());
  }
}

int main() {
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i, j;
  int t, u, v;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &t, &u);
    nodes[t - 1].ks = new int[u];
    nodes[t - 1].length = u;
    for (j = 0; j < u; j++) {
      scanf("%d", &v);
      nodes[t - 1].ks[j] = v - 1;
    }
  }

  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if ((r = dikstra(t - 1, u - 1, v)) != -1)
      printf("%d\\n", r);
    else
      puts("NA");
  }
}


Error: 0

Fixed submission:

#include <list>
#include <set>
#include <stdio.h>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;

int dikstra(int s, int e, int deep) {
  int c = 0;

  list<int> que;
  set<int> done;
  que.push_back(s);

  int flag = 1;

  while (flag) {
    c++;
    if (c >= deep)
      return -1;

    int len = que.size();
    for (int i = 0; flag && i < len; i++) {
      for (set<int>::iterator it = done.begin(); it != done.end(); it++)
        que.remove((*it));

      int v = que.front();
      que.pop_front();
      done.insert(v);

      for (int j = 0; flag && j < nodes[v].length; j++) {
        if (nodes[v].ks[j] == e) {
          flag = 0;
        } else {
          que.push_back(nodes[v].ks[j]);
        }
      }
    }
  }
  return c + 1;
}

int main() {
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i, j;
  int t, u, v;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &t, &u);
    nodes[t - 1].ks = new int[u];
    nodes[t - 1].length = u;
    for (j = 0; j < u; j++) {
      scanf("%d", &v);
      nodes[t - 1].ks[j] = v - 1;
    }
  }

  int m;
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if ((r = dikstra(t - 1, u - 1, v)) != -1)
      printf("%d\\n", r);
    else
      puts("NA");
  }
}