alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
// using Graph = vector<vector<ll>>;

#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define MOD 1000000007

const int MAX_M = 100000;

int main() {
  ll n, k;
  // string s, t;
  cin >> n >> k;

  vector<ll> a(n);
  // map<string,ll> m;

  rep(i, 0, n) { cin >> a[i]; }

  ll i = 1;
  vector<ll> q;
  ll cnt = 0;
  vector<ll> seen(n + 1, -1);

  while (seen[i] == -1) {

    seen[i] = cnt;
    cnt++;
    q.push_back(i);

    i = a[i - 1];
  }
  // cout << seen[i] << endl;

  if (k <= seen[i]) {
    cout << q[k] << endl;
  } else {
    cnt -= seen[i]; // 周期
    k -= seen[i];
    k %= cnt;
    cout << q[seen[i] + k] << endl;
  }

  /*rep(i,0,q.size()){
    cout << q[i] << endl;
  }*/

  // cout << fixed << setprecision(10);
  // cout << q.at(k-1) << endl;
  // cout << ((ok)? "Yes" : "No") << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define MOD 1000000007

const int MAX_M = 100000;

ll n, k, cnt = 0;

vector<bool> seen, finished;
int pos = -1;    // サイクル中に含まれる頂点 pos
stack<int> hist; // 訪問履歴

void dfs(const Graph &G, int v, int p) {
  seen[v] = true;
  cnt++;
  hist.push(v);
  for (auto nv : G[v]) {
    if (nv == p)
      continue; // 逆流を禁止する

    // 完全終了した頂点はスルー
    if (finished[nv])
      continue;

    // サイクルを検出
    if (seen[nv] && !finished[nv]) {
      pos = nv;
      return;
    }

    // 再帰的に探索
    dfs(G, nv, v);

    // サイクル検出したならば真っ直ぐに抜けていく
    if (pos != -1)
      return;
  }
  hist.pop();
  finished[v] = true;
}

int main() {
  // ll n, k;    //n枚,y円
  // string s, t;
  cin >> n >> k;

  vector<ll> a(n);
  vector<ll> q;
  // map<string,ll> m;
  Graph G(n);

  rep(i, 0, n) {
    ll x;
    cin >> x;
    G[i].push_back(x - 1);
  }

  seen.assign(n, false), finished.assign(n, false);
  pos = -1;
  dfs(G, 0, -1);

  // サイクルを復元
  set<int> cycle;
  while (!hist.empty()) {
    int t = hist.top();
    cycle.insert(t);
    hist.pop();
    if (t == pos)
      break;
  }

  ll size = cycle.size();
  if ((cnt != size) && (k > cnt - size)) {
    k -= (cnt - size);
  }
  k %= size;

  ll j = 0;

  for (auto itr = cycle.begin(); itr != cycle.end(); ++itr) {
    if (j == k)
      cout << *itr + 1;
    j++;
  }

  // cout << fixed << setprecision(10);
  // cout << cycle.at(k) << endl;
  // cout << ((ok)? "Yes" : "No") << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

long long int n, k;
long long int now = 1;
long long int counter = 0;

int main() {
  cin >> n >> k;

  vector<long long int> a(n);
  vector<long long int> isVisit(n);

  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    isVisit[i] = -1;
  }

  while (k > 0) {
    isVisit[now - 1] = counter;
    now = a[now - 1];
    ++counter;
    --k;
    if (isVisit[now - 1] > 0) {
      counter = counter - isVisit[now - 1];
      break;
    }
  }

  int p = k % counter;

  for (int i = 0; i < p; i++) {
    now = a[now - 1];
  }
  cout << now;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

long long int n, k;
long long int now = 1;
long long int counter = 0;
int main() {
  cin >> n >> k;

  vector<int> a(n);
  vector<bool> isVisit(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    isVisit[i] = false;
  }

  while (k > 0) {
    isVisit[now - 1] = true;
    now = a[now - 1];
    --k;
    if (isVisit[now - 1])
      break;
  }
  int tmp = now;
  while (true) {
    tmp = a[tmp - 1];
    counter++;
    if (tmp == now)
      break;
  }
  int p = k % counter;
  for (int i = 0; i < p; i++) {
    now = a[now - 1];
  }
  cout << now;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<vector<ll>> graph(n, vector<ll>(2, -1));
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    graph.at(i).at(0) = x - 1;
  }
  ll pos = 0;
  vector<ll> path;
  ll loop_pos;
  while (true) {
    if (graph.at(pos).at(1) == -1) {
      path.push_back(pos);
      graph.at(pos).at(1) = 1;
      pos = graph.at(pos).at(0);
    } else {
      loop_pos = pos;
      break;
    }
  }
  ll head = find(path.begin(), path.end(), loop_pos) - path.begin();
  ll loop = path.size() - head;
  ll mod = (k - head) % loop;
  // cout << head << " " << loop << " " << mod << endl;
  if (k <= head) {
    cout << path.at(k) + 1 << endl;
  } else {
    cout << path.at(head + mod) + 1 << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int64_t n, k;
  cin >> n >> k;
  vector<int64_t> a(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int64_t x;
    cin >> x;
    a.at(i) = x;
  }
  int64_t dep = 1;
  vector<int64_t> path = {1};
  set<int64_t> alr = {1};
  while (true) {
    int64_t next = a.at(dep);
    path.push_back(next);
    if (alr.count(next))
      break;
    alr.insert(next);
    dep = next;
  }
  // for (int i = 0; i < path.size(); i++)cout << path.at(i) << " ";
  int64_t x_2 = path.size() - 1;
  int64_t x_1 = 0;
  for (int64_t i = 0; i < path.size() - 1; i++) {
    if (path.at(i) == path.at(x_2)) {
      x_1 = i;
      break;
    }
  }
  int64_t mod = x_2 - x_1;
  int64_t head = x_1;

  if (k < head) {
    cout << path.at(k) << endl;
  }
  if (mod == 0) {
    cout << path.at(x_1) << endl;
  }
  int64_t loop = (k + 1 - head) / mod;
  if (k > path.size())
    k -= (loop - 1) * mod;
  int64_t ans = path.at(k);
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int N;
  int64_t K;

  vector<int> A;

  cin >> N >> K;

  rep(i, N) {
    int a;
    cin >> a;
    A.push_back(a - 1);
  }

  vector<bool> f(N, false);
  int step = 0;
  int P = 0; // roopする町
  f[0] = true;
  for (; step < K; step++) {
    P = A[P];
    if (f[P])
      break;
    f[P] = true;
    if (step == K - 1) {
      cout << P + 1;
      return 0;
    }
  }
  step++;

  int p = P;
  int ropN = 1; // いくつでループするか
  for (; ropN < N; ropN++) {

    p = A[p];
    if (p == P)
      break;
  }

  int ans = 0;
  if (step > K) {

    for (int i = 0; i < K; i++) {
      ans = A[ans];
    }
  } else {
    ans = P;
    for (int i = 0; i < (K - step) % ropN; i++) {
      ans = A[ans];
    }
  }

  // cout << step << " " << ropN << " ";

  cout << ans + 1;

  // cin >> N;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  int N;
  int64_t K;

  vector<int> A;

  cin >> N >> K;

  rep(i, N) {
    int a;
    cin >> a;
    A.push_back(a - 1);
  }

  vector<bool> f(N, false);
  int step = 0;
  int P = 0; // roopする町
  f[0] = true;
  for (step = 0; step < K; step++) {
    P = A[P];
    if (f[P])
      break;
    f[P] = true;
  }

  int p = 0;
  int ropN = 0;
  for (; ropN < N; ropN++) {

    p = A[p];
    if (p == P)
      break;
  }

  int ans = 0;
  if (step > K) {

    for (int i = 0; i < K; i++) {
      ans = A[ans];
    }
  } else {
    ans = P;
    for (int i = 0; i < K % ropN; i++) {
      ans = A[ans];
    }
  }

  cout << ans + 1;

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

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;
  for (int i = 0; i < N; i++)
    A[i]--;

  long long c = 0;
  int pos = 0;
  vector<int> visited(N, -1);
  visited[0] = 0;
  while (1) {
    // cout << c << endl;
    if (c == K) {
      cout << pos + 1 << endl;
      return 0;
    }
    pos = A[pos];
    c++;
    if (visited[pos] == -1)
      visited[pos] = c;
    else {
      long long x = (c - visited[pos]);
      c = K / x * x + (c % x);
      if (c > K)
        c -= x;
      while (1) {
        // cout << c << endl;
        if (c == K) {
          cout << pos + 1 << endl;
          return 0;
        }
        pos = A[pos];
        c++;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &i : A)
    cin >> i;
  for (int i = 0; i < N; i++)
    A[i]--;

  long long c = 0;
  int pos = 0;
  vector<int> visited(N, -1);
  visited[0] = 0;
  while (1) {
    if (c == K) {
      cout << pos + 1 << endl;
      return 0;
    }
    pos = A[pos];
    c++;
    if (visited[pos] == -1)
      visited[pos] = c;
    else {
      c = K / (c - visited[pos]) * (c - visited[pos]) +
          (c % (c - visited[pos]));
      if (c > K)
        c -= (c - visited[pos]);
      while (1) {
        if (c == K) {
          cout << pos + 1 << endl;
          return 0;
        }
        pos = A[pos];
        c++;
      }
    }
  }

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
typedef long long int lli;
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define forab(i, a, b) for (int i = a; i < b; i++)
#define forll(i, a, b) for (lli i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define sortv(v) sort(v.begin(), v.end())
#define mod1 1000000007
#define mod2 998244353
#define debug(x) cout << #x << " : " << x << endl

int main() {
  optimize

      int n;
  lli k;
  cin >> n >> k;
  vector<int> done(200001, 0);

  vector<int> v(n);
  forab(i, 0, n) { cin >> v[i]; }

  vector<int> path;
  map<int, int> m;

  int x = v[0];
  path.pb(1);
  forab(i, 0, n) {
    if (done[x - 1] == 0) {
      path.pb(x);
    } else
      break;

    done[x - 1] = 1;
    x = v[x - 1];
  }

  auto it = find(path.begin(), path.end(), x);

  int lin = it - path.begin();

  int cyc = path.size() - lin;

  if (k < lin) {
    cout << path[k] << endl;
  } else {
    k -= (lli)lin;

    int rem = k % (lli)cyc;

    cout << path[lin + rem] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
typedef long long int lli;
#define optimize                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define forab(i, a, b) for (int i = a; i < b; i++)
#define forll(i, a, b) for (lli i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define sortv(v) sort(v.begin(), v.end())
#define mod1 1000000007
#define mod2 998244353
#define debug(x) cout << #x << " : " << x << endl

int main() {
  optimize

      int n;
  lli k;
  cin >> n >> k;

  vector<int> a(n);
  forab(i, 0, n) cin >> a[i];

  set<int> s;
  int j = 1;
  int rep = 0;
  int cy = 1;
  while (true) {
    j = a[j - 1];
    auto it = s.find(j);
    if (it == s.end()) {
      s.insert(j);
      cy++;
    } else {
      rep = j;
      break;
    }
  }

  int st = 1;
  j = 1;
  while (true) {
    j = a[j - 1];
    st++;
    if (j == rep)
      break;
  }

  if (k <= cy) {
    int j = 1;
    forll(i, 0LL, (lli)k - 1) { j = a[j - 1]; }

    cout << j << endl;
  } else {
    k -= (lli)st;
    lli rem = k % (lli)(cy - st);

    j = rep;
    forll(i, 0LL, rem) { j = a[j - 1]; }
    cout << j << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main(void) {
  int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {},
                          move[200000] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, y = check[i], MOD = x - y + 1;
      break;
    }
  if (K > x)
    K = (K - y) % MOD + y;
  return printf("%d\\n", move[K] + 1), 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main(void) {
  int N, i, j, x, y, MOD, A[200000] = {0}, check[200000] = {},
                          move[200000] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, y = check[i], MOD = x - y - 1;
      break;
    }
  if (K > x)
    K = (K - y) % MOD + y;
  return printf("%d\\n", move[K] + 1), 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main(void) {
  int N, i, j, x, MOD, A[200000] = {0}, check[200000] = {0}, move[200000] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, MOD = j - check[i];
      break;
    }
  if (K > x)
    K = (K - x + MOD - 1) % MOD + x - MOD + 1;
  printf("%d\\n", move[K] + 1);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
int main(void) {
  int N, i, j, x, MOD, A[200000] = {0}, check[10] = {0}, move[10] = {0};
  long long K;
  x = scanf("%d %lld", &N, &K);
  for (i = 0; i < N; i++)
    x = scanf("%d", &A[i]), check[i] = -1;
  for (i = 0, j = 0;; i = A[i] - 1, j++)
    if (check[i] < 0)
      check[i] = j, move[j] = i;
    else {
      x = j - 1, MOD = j - check[i];
      break;
    }
  if (K > x)
    K = (K - x + MOD - 1) % MOD + x - MOD + 1;
  printf("%d\\n", move[K] + 1);
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  ll N, K;
  cin >> N >> K;

  vector<int> A(N);

  rep(i, N) {
    cin >> A[i];
    A[i]--;
  } // input

  int place = 0;

  vector<int> B(2000000, -1);

  B[0] = 0;

  for (int i = 1;; i++) {

    K--;

    if (B[A[place]] != -1) {
      K %= (i - B[A[place]]);
    }

    else {
      B[A[place]] = i;
    }

    place = A[place];

    if (K == 0)
      break;
  }

  cout << place + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  ll N, K;
  cin >> N >> K;

  vector<int> A(N);

  rep(i, N) {
    cin >> A[i];
    A[i]--;
  } // input

  int place = 0;

  vector<int> B(200000, 0);

  for (int i = 1;; i++) {

    if (B[A[place]] != 0) {
      K %= (B[A[place]] - i);
    }

    else {
      B[A[place]] = i;
    }

    place = A[place];

    K--;

    if (K == 0)
      break;
  }

  cout << place + 1 << endl;

  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error



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

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
const int N = 2e5 + 5;
int n, t, m;
int b, k, x;

vector<int> a(N);
vector<int> tele;

int32_t main() {
  IOS;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];
  map<int, int> mp;
  mp[1]++;
  tele.push_back(1);
  if (k <= n) {
    int p = 1, coun = 0;
    while (coun < k) {
      p = a[p];
      coun++;
    }
    cout << p << endl;
    return 0;
  }
  int flag = 0;
  int i = a[1];

  while (1 <= i <= n) {

    if (mp[i] > 0) {
      flag = i;
      break;
    }
    mp[i]++;
    tele.push_back(i);
    // cout<<i<<" ";
    i = a[i];
  }

  int cycle = 0, dis = 0;
  for (int i = 0; i < tele.size(); i++) {
    if (tele[i] != flag)
      dis++;
    else {
      cycle = tele.size() - i;
      break;
    }
  }
  // cout<<dis<<" "<<cycle<<endl;
  k -= dis;
  int idx = k % cycle;
  // cout<<idx<<endl;
  cout << tele[dis + idx] << endl;
}


Error: 0

Fixed submission:

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

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
const int N = 1e3 + 5;
int n, t, m;
int b, k, x;
string s1, s2;
int mat[N][N];
vector<int> a(N);
vector<int> tele;
int cost = 1e18;

int32_t main() {
  IOS;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];
  map<int, int> mp;
  mp[1]++;
  tele.push_back(1);
  if (k <= n) {
    int p = 1, coun = 0;
    while (coun < k) {
      p = a[p];
      coun++;
    }
    cout << p << endl;
    return 0;
  }
  int flag = 0;
  int i = a[1];

  while (1 <= i <= n) {

    if (mp[i] > 0) {
      flag = i;
      break;
    }
    mp[i]++;
    tele.push_back(i);
    // cout<<i<<" ";
    i = a[i];
  }

  int cycle = 0, dis = 0;
  for (int i = 0; i < tele.size(); i++) {
    if (tele[i] != flag)
      dis++;
    else {
      cycle = tele.size() - i;
      break;
    }
  }
  // cout<<dis<<" "<<cycle<<endl;
  k -= dis;
  int idx = k % cycle;
  // cout<<idx<<endl;
  cout << tele[dis + idx] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    c = s.size() - ord[v];
    l = ord[v];
  }
  if (k < l) {
    cout << s[k] << endl;
  } else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long count = 0;            // テレポートの回数
  vector<bool> seen(n + 1);       // 訪れていたらtrue
  vector<long long> visit(n + 1); // 訪れた順番
  seen[1] = true;
  visit[1] = 0;
  long long town = 1;           // 今いる場所
  long long T = 0;              // 周期
  long long start = 0;          // 周期が始まるとき
  vector<long long> see(k + 1); // 今何番目にいるか
  see[0] = 1;
  while (bool flag = true) {
    count++;
    town = a[town - 1];
    see[count] = town;
    if (seen[town] == false) {
      seen[town] = true;
      visit[town] = count;
    } else {
      T = count - visit[town];
      start = visit[town];
      flag = false;
    }
  }
  if (k < T) {
    cout << see[k] << endl;
  } else {
    long long n = (k - start) & T;
    cout << see[n + start] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 2e5 + 7;
/*
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,val;}ee[M*2];
void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
*/

int to[M];
int vs[M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> to[i];
  int tp = 1;
  ll nm = 0;
  while (1) {
    vs[tp]++;
    nm++;
    if (nm == k) {
      cout << to[tp] << endl;
      return 0;
    }

    tp = to[tp];
    if (vs[tp] == 2)
      break;
  }
  ll nw = 0;
  for (int i = 1; i <= n; i++)
    if (vs[i] == 2)
      nw++;
  k -= nm;
  k %= nw;
  while (k) {
    tp = to[tp];
    k--;
  }
  cout << tp << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 2e5 + 7;
/*
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,val;}ee[M*2];
void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
*/

int to[M];
int vs[M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> to[i];
  int tp = 1;
  ll nm = 0;
  while (1) {
    vs[tp]++;
    nm++;
    if (nm == k) {
      cout << to[tp] << endl;
      return 0;
    }

    tp = to[tp];
    if (vs[tp] == 2)
      break;
  }
  ll nw = 0;
  for (int i = 1; i <= n; i++)
    if (vs[i] == 2)
      nw++;
  k -= nm;
  k %= nw;
  while (k) {
    tp = to[tp];
  }
  cout << tp << endl;
  return 0;
}