Problem p00144 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>

using namespace std;

int main() {

  int n;
  cin >> n;

  int data[n + 1][n + 1];

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

  int from, to, conesum;

  for (int i = 0; i < n; i++) {
    cin >> from >> conesum;
    for (int j = 0; j < conesum; j++) {
      cin >> to;
      data[from][to] = 1;
    }
  }
  // graph end

  int sumpake;
  cin >> sumpake;

  queue<int> Q;

  int ttl, myposi, min, sumin, sumina, finish = 0;

  int come[n + 1];

  for (int i = 0; i < sumpake; i++) {
    for (int i = 0; i < n + 1; i++)
      come[i] = 0;
    min = 0;
    cin >> from >> to >> ttl;
    myposi = from;
    come[myposi] = 1;
    for (int j = 0; j < ttl; j++) {
      if (j != 0) {
        for (int l = 0; l < sumina; l++) {
          if (Q.front() == to) {
            min = j;
            finish = 1;
            break;
          }
          myposi = Q.front();
          Q.pop();
          if (come[myposi] == 0) {
            for (int k = 1; k <= n; k++) {
              if (data[myposi][k] == 1) {
                Q.push(k);
                sumin++;
              }
            }
          }
          come[myposi] = 1;
        }
        if (finish == 1)
          break;
      } else {
        sumin = 0;
        for (int k = 1; k <= n; k++) {
          if (data[myposi][k] == 1) {
            Q.push(k);
            sumin++;
          }
        }
      }
      sumina = sumin;
      sumin = 0;
    }

    if (min == 0)
      cout << "NA";
    else
      cout << min + 1;
    cout << endl;
    finish = 0;
    while (!Q.empty())
      Q.pop();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

int main() {

  int n;
  cin >> n;

  int data[n + 1][n + 1];

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

  int from, to, conesum;

  for (int i = 0; i < n; i++) {
    cin >> from >> conesum;
    for (int j = 0; j < conesum; j++) {
      cin >> to;
      data[from][to] = 1;
    }
  }

  int sumpake;
  cin >> sumpake;

  queue<int> Q;

  int ttl, myposi, min, sumin, sumina, finish = 0;

  for (int i = 0; i < sumpake; i++) {
    min = 0;
    cin >> from >> to >> ttl;
    if (ttl == 0) {
      cout << "NA" << endl;
      continue;
    }
    myposi = from;
    for (int j = 0; j < ttl; j++) {
      if (j != 0) {
        for (int l = 0; l < sumina; l++) {
          if (Q.front() == to) {
            min = j;
            finish = 1;
            break;
          }
          myposi = Q.front();
          Q.pop();
          for (int k = 1; k <= n; k++) {
            if (data[myposi][k] == 1) {
              Q.push(k);
              sumin++;
            }
          }
        }
        if (finish == 1)
          break;
      } else {
        sumin = 0;
        for (int k = 1; k <= n; k++) {
          if (data[myposi][k] == 1) {
            Q.push(k);
            sumin++;
          }
        }
      }
      sumina = sumin;
      sumin = 0;
    }

    if (min == 0)
      cout << "NA";
    else
      cout << min + 1;
    cout << endl;
    finish = 0;
    while (!Q.empty())
      Q.pop();
  }

  return 0;
}