Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;
int k;
int n, m, x, ans = INF;
int a[211111];
int f[211111];
vector<int> v, v2;
signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int now = 0;
  f[0] = 1;
  int fini;
  v.push_back(0);
  while (true) {
    now = a[now];
    if (f[now]) {
      fini = now;
      break;
    }
    f[now] = 1;
    v.push_back(now);
  }
  int finif = 0;
  rep(i, v.size()) {
    if (v[i] == fini)
      finif = 1;
    if (finif)
      v2.push_back(v[i]);
  }
  int fcnt = 0, scnt = 0, mode = 0;
  rep(i, v.size()) {
    if (mode == 0) {
      fcnt++;
      if (i == v.size() - 1)
        continue;
      if (v[i + 1] == fini) {
        mode = 1;
      }
    } else {
      scnt++;
    }
  } // cout<<fcnt<<" "<<scnt<<endl;
  // cout<<v.size()<<endl;

  // rep(i,v2.size())cout<<v2[i]+1<<" ";cout<<endl;

  k -= fcnt;
  k++;
  if (scnt == 0) {
    k %= fcnt;
    if (k == 0)
      cout << v2[fcnt - 1] + 1 << endl;
    else
      cout << v2[k - 1] + 1 << endl;
    return 0;
  }
  k %= scnt;

  // cout<<endl;
  // cout<<k<<endl;
  if (k == 0)
    cout << v2[scnt - 1] + 1 << endl;
  else
    cout << v2[k - 1] + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;
int k;
int n, m, x, ans = INF;
int a[211111];
int f[211111];
vector<int> v, v2;
signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  int now = 0;
  f[0] = 1;
  int fini;
  v.push_back(0);
  while (true) {
    now = a[now];
    if (f[now]) {
      fini = now;
      break;
    }
    f[now] = 1;
    v.push_back(now);
  }
  int finif = 0;
  rep(i, v.size()) {
    if (v[i] == fini)
      finif = 1;
    if (finif)
      v2.push_back(v[i]);
  }
  int fcnt = 0, scnt = 0, mode = 0;
  rep(i, v.size()) {
    if (mode == 0) {
      fcnt++;
      if (i == v.size() - 1)
        continue;
      if (v[i + 1] == fini) {
        mode = 1;
      }
    } else {
      scnt++;
    }
  } // cout<<fcnt<<" "<<scnt<<endl;
  // cout<<v.size()<<endl;

  // rep(i,v.size())cout<<v[i]+1<<" ";cout<<endl;

  if (fcnt > k) {
    cout << v[k] + 1 << endl;
    return 0;
  }

  k -= fcnt;
  k++;
  if (scnt == 0) {
    k %= fcnt;
    if (k == 0)
      cout << v2[fcnt - 1] + 1 << endl;
    else
      cout << v2[k - 1] + 1 << endl;
    return 0;
  }
  k %= scnt;

  // cout<<endl;
  // cout<<k<<endl;
  if (k == 0)
    cout << v2[scnt - 1] + 1 << endl;
  else
    cout << v2[k - 1] + 1 << endl;
}