Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
vector<int> to(200010, 0);
vector<ll> time1[500010];
ll n, k;
void bfs(int i, ll count) {
  // cout<<count<<endl;
  if (count > n + n + 10)
    return;
  time1[i].push_back(count);
  bfs(to[i], count + 1);
}

int main() {
  cin >> n >> k;
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    to[i] = x;
  }
  bfs(0, 0);
  for (int i = 0; i < n; i++) {
    bool ok = 0;
    int now = 1;
    while (1) {
      // cout<<i<<"check"<<endl;
      // cout<<time1[i].size()<<endl;
      if (time1[i].size() == 0)
        break;
      if (now + 1 > time1[i].size() - 1) {
        // cout<<i<<" is  out"<<endl;
        break;
      }

      if (time1[i][now] - time1[i][now - 1] ==
          time1[i][now + 1] - time1[i][now]) {
        ok = 1;
        break;
      }
      now++;
    }
    // cout<<"end"<<endl;
    if (ok) {
      ll loop = time1[i][now] - time1[i][now - 1];
      if ((k - time1[i][now - 1]) % loop == 0) {
        if (k >= time1[i][now - 1]) {
          cout << i + 1 << endl;
          // cout<<"answer"<<endl;
          return 0;
        } else {
          rep(i, n) {
            for (auto u : time1[i]) {
              if (u == k) {
                cout << i + 1 << endl;
                return 0;
              }
            }
          }
        }
      }
    }
  }
  rep(i, n) {
    for (auto u : time1[i]) {
      if (u == k) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
vector<ll> to(200010, 0);
vector<ll> time1[100];
ll n, k;
void bfs(int i, int count) {
  // cout<<count<<endl;
  if (count > n + n + 10)
    return;
  time1[i].push_back(count);
  bfs(to[i], count + 1);
}

int main() {
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    to[i] = x;
  }
  bfs(0, 0);
  // cout<<"Yes"<<endl;
  /*
  rep(i,n){
      for(auto u : time1[i]){
          cout<<u<<" ";
      }
      cout<<endl;
  }*/
  for (int i = 0; i < n; i++) {
    bool ok = 0;
    int now = 1;
    while (1) {
      // cout<<i<<"check"<<endl;
      // cout<<time1[i].size()<<endl;
      if (time1[i].size() == 0)
        break;
      if (now + 1 > time1[i].size() - 1) {
        // cout<<i<<" is  out"<<endl;
        break;
      }

      if (time1[i][now] - time1[i][now - 1] ==
          time1[i][now + 1] - time1[i][now]) {
        ok = 1;
        break;
      }
      now++;
    }
    // cout<<"end"<<endl;
    if (ok) {
      ll loop = time1[i][now] - time1[i][now - 1];
      if ((k - time1[i][now - 1]) % loop == 0) {
        cout << i + 1 << endl;
        // cout<<"answer"<<endl;
        return 0;
      }
    }
  }
  return 0;
}