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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector<bool> vb;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
const char newl = '\\n';

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;
  vl a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vb checked(n, 0);
  ll K = k, num = 0, num2 = 0, r = 0;
  while (k && !checked[num]) {
    checked[num] = 1;
    k--;
    num = a[num];
  }
  if (k == 0) {
    cout << num + 1 << newl;
    return 0;
  }

  while (num2 != num) {
    r++;
    num2 = a[num2];
  }

  ll l = K - k - r;
  k %= l;

  while (k) {
    k--;
    num = a[num];
  }
  cout << num + 1 << newl;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl /*, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for (ll i = 1; i <= ll(n); ++i)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 2e5 + 5;
const char newl = '\\n';

ll k, num, cnt = 0, ans;
vector<bool> checked(MAX);
vl a(MAX);

void teleport(ll v) {
  if (checked[v]) {
    num = v;
    return;
  }

  k--;
  checked[v] = 1;
  teleport(a[v]);
  return;
}

void find_num(ll v) {
  if (v == num)
    return;
  cnt++;
  find_num(a[v]);
  return;
}

void goal(ll v) {
  if (k == 0) {
    ans = v;
    return;
  }
  k--;
  goal(a[v]);
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, const_k;
  cin >> n >> k;
  const_k = k;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  // teleport(0);
  num = 0;

  find_num(0);

  ll l = const_k - k + cnt;
  k %= l;

  goal(num);

  cout << ans + 1 << newl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) 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>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<int> go;
  map<int, int> b;
  ll cut1 = 0, cut2 = 0;
  b[1] = 1;
  rep(i, k) {
    cut1++;
    if (i == 0) {
      b[a[go[i] - 1]]++;
    } else {
      b[a[go[i] - 1]]++;
      if (b[a[go[i] - 1]] == 3)
        break;
      if (b[a[go[i] - 1]] == 2) {
        cut2++;
        go.push_back(a[go[i] - 1]);
      }
    }
  }
  ll num = cut1 - cut2 * 2;
  ll ans = (k - num) % cut2;
  cout << go[ans + num - 1] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) 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: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  ll k;
  vector<int> a(n);
  cin >> n >> k;
  rep(i, n) 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 < 1)
    cout << s[k] << endl;
  else {
    k -= 1;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;         // 0-index, town
    A.at(i) = a; // A[i] = town i から行ける街
  }
  vector<ll> loop;
  vector<ll> visit(N);
  for (ll i = 0; i < N; i++)
    visit.at(i) = 0;

  ll town = 0; // 0start
  bool is_loop = false;
  vector<ll> before_loop;
  for (ll i = 0; i < N * 2; i++) {
    // cout << town << endl;
    if (visit.at(town) == 1) {
      is_loop = true;
    }
    if (is_loop) {
      if (visit.at(town) == 2)
        break; // loop終了
      else {
        loop.push_back(town);
        visit.at(town) = 2;
        town = A.at(town); // 次の町へ
      }
    } else {
      before_loop.push_back(town);
      visit.at(town) = 1;
      town = A.at(town);
    }
  }
  // for (ll i = 0;i<loop.size();i++) cout << before_loop.at(i) << endl;
  if (K < before_loop.size()) {
    cout << before_loop.at(K) + 1 << endl;
  } else {
    ll a = K - before_loop.size();
    cout << loop.at(a % loop.size()) + 1 << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A;
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;
    A.push_back(a);
  }
  map<ll, ll> mp; // 重複確認
  ll loop_start;
  ll start = 0;
  vector<ll> saisyo;
  ll loop_start_index;
  for (ll i = 0; i < N; i++) {
    if (mp[start] == 0) { // はじめて
      mp[start]++;
      saisyo.push_back(start);
      start = A.at(start);       // つぎのまちへ
    } else if (mp[start] == 1) { // 二回目
      loop_start = start;
      loop_start_index = i;
      break;
    }
  }
  vector<ll> loop;
  vector<ll> before_loop;
  bool is_loop = false;
  for (ll i = 0; i < saisyo.size(); i++) {
    if (saisyo.at(i) == loop_start)
      is_loop = true;
    if (is_loop)
      loop.push_back(saisyo.at(i));
    else
      before_loop.push_back(saisyo.at(i));
  }
  if (K <= loop_start_index)
    cout << saisyo.at(K) << endl;
  else {
    ll amari = K % loop.size();
    for (ll i = loop_start_index; i < saisyo.size(); i++) {
      if (saisyo.at(i) % loop.size() == amari) {
        cout << saisyo.at(i) << endl;
      }
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll a(n);
  rep(i, n) cin >> a[i];
  vll d(n, 0);
  vll d2(n, 0);
  vll dco(n, 0);
  ll point = 1;
  ll count = 0;
  while (true) {
    ll next = a[point - 1];
    if (d[next - 1] == 1) {
      k -= d2[next - 1];
      ll p = count - d2[next - 1] + 1;
      k %= p;
      cout << dco[k + d2[next - 1] - 1] << endl;
      return 0;
    } else if (count == k) {
      cout << point << endl;
      return 0;
    } else {
      count++;
      d2[next - 1] = count;
      d[next - 1] = 1;
      dco[count - 1] = next;
      point = next;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll a(n);
  rep(i, n) cin >> a[i];
  vll d(n, 0);
  vll d2(n, 0);
  vll dco(n, 0);
  ll point = 1;
  ll count = 0;
  dco[0] = 0;
  while (true) {
    ll next = a[point - 1];
    if (d[next] == 1) {
      k -= d2[next];
      ll p = count - d2[next] + 1;
      k %= p;
      cout << dco[k + d2[next]] << endl;
      return 0;
    } else if (count == k) {
      cout << point << endl;
      return 0;
    } else {
      count++;
      d2[next] = count;
      d[next] = 1;
      dco[count] = next;
      point = next;
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const int N = 2e5 + 5;

ll n, k, a[N];
bool visited[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int ptr = 1, c = 0, period = -1, ptr1;
  vi cycle(n + 1);

  while (true) {
    if (visited[ptr]) {
      period = c - cycle[ptr];
      ptr1 = ptr;
      break;
    }

    cycle[ptr] = c;
    visited[ptr] = true;

    ptr = a[ptr];
    c += 1;
  }

  if (k < c) {
    for (int i = 1; i <= n; i++) {
      if (cycle[i] == k) {
        cout << i << "\\n";
        return 0;
      }
    }
  }

  k -= c;
  k %= period;

  ptr = ptr1;
  c = 0;

  while (true) {
    if (c == k) {
      cout << ptr << "\\n";
      return 0;
    }
    ptr = a[ptr];
    c += 1;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const int N = 1e5 + 5;

ll n, k, a[N];
bool visited[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int ptr = 1, c = 0, period = -1, ptr1;
  vi cycle(n + 1);

  while (true) {
    if (visited[ptr]) {
      period = c - cycle[ptr];
      ptr1 = ptr;
      break;
    }

    cycle[ptr] = c;
    visited[ptr] = true;

    ptr = a[ptr];
    c += 1;
  }

  if (k < c) {
    for (int i = 1; i <= n; i++) {
      if (cycle[i] == k) {
        cout << i << "\\n";
        return 0;
      }
    }
  }

  k -= c;
  k %= period;

  ptr = ptr1;
  c = 0;

  while (true) {
    if (c == k) {
      cout << ptr << "\\n";
      return 0;
    }
    ptr = a[ptr];
    c += 1;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[200005], v[200005];
signed main() {
  int n, k;
  cin >> n >> k;
  int i, j;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1, j = 1; !v[i] && k; i = a[i], j++, k--)
    v[i] = j;         // timer at which you went to this city
  k = k % (j - v[i]); // mod k with loop length
  for (; k; i = a[i], k--)
    ;
  cout << i << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

signed main() {
  int n, k, x = 0;
  cin >> n >> k;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> x;
    m.insert(make_pair(i + 1, x));
  }

  int curr = 0, id = 1;
  for (int i = 0; i < k; i++) {
    curr = m[id];
    id = curr;
  }
  cout << curr << "\\n";
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  long n, k, cur = 1, cnt = 1;
  cin >> n >> k;
  vector<long> a(n);
  vector<bool> flag(n, true);
  rep(i, n) cin >> a.at(i);
  bool f = true;
  do {
    k--;
    f &= flag.at(cur - 1);
    flag.at(cur - 1) = false;
    cur = a.at(cur - 1);
  } while (k > 0 && f);
  long temp = a.at(cur - 1);
  while (k > 0 && temp != cur) {
    cnt++;
    temp = a.at(temp - 1);
  }
  k %= cnt;
  rep(i, k) cur = a.at(cur - 1);
  cout << cur << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  long n, k, cur = 1, cnt = 1;
  cin >> n >> k;
  vector<long> a(n);
  vector<bool> flag(n, true);
  rep(i, n) cin >> a.at(i);
  bool f = true;
  do {
    k--;
    f &= flag.at(cur - 1);
    flag.at(cur - 1) = false;
    cur = a.at(cur - 1);
  } while (k > 0 && f);
  long temp = a.at(cur - 1);
  while (temp != cur) {
    cnt++;
    temp = a.at(temp - 1);
  }
  k %= cnt;
  rep(i, k) cur = a.at(cur - 1);
  cout << cur << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int teleporters_count;
  long long teleport_times;
  vector<int> teleport_dests;
  int *city_steps;

  cin >> teleporters_count >> teleport_times;
  city_steps = new int[teleporters_count];

  for (int i = 0; i < teleporters_count; i++) {
    int td;
    cin >> td;
    teleport_dests.push_back(td - 1);

    city_steps[i] = -1;
  }

  city_steps[0] = 0;
  int current_pos = 0;
  bool rounded = false;
  while (teleport_times > 0) {
    int next_pos = teleport_dests[current_pos];

    if (city_steps[next_pos] == -1) {
      city_steps[next_pos] = city_steps[current_pos] + 1;
    } else if (!rounded) {
      long long cycle_length =
          city_steps[current_pos] + 1 - city_steps[next_pos];

      teleport_times %= cycle_length;
      rounded = true;
    }

    if (teleport_times > 0) {
      current_pos = next_pos;
      teleport_times -= 1;
    }
  }

  cout << current_pos + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int teleporters_count;
  long long teleport_times;
  vector<int> teleport_dests;
  int *city_steps;

  cin >> teleporters_count >> teleport_times;
  city_steps = new int[teleporters_count];

  for (int i = 0; i < teleporters_count; i++) {
    int td;
    cin >> td;
    teleport_dests.push_back(td - 1);

    city_steps[i] = -1;
  }

  city_steps[0] = 0;
  int current_pos = 0;
  bool rounded = false;
  while (teleport_times > 0) {
    int next_pos = teleport_dests[current_pos];

    if (city_steps[next_pos] == -1 && !rounded) {
      city_steps[next_pos] = city_steps[current_pos] + 1;
    } else {
      long long cycle_length =
          city_steps[current_pos] + 1 - city_steps[next_pos];

      teleport_times %= cycle_length;
      rounded = true;
    }

    if (teleport_times > 0) {
      current_pos = next_pos;
      teleport_times -= 1;
    }
  }

  cout << current_pos + 1 << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define ar array
#define nl '\\n'
#define AC                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
using namespace std;
const int M = 1e9 + 7;
const int N = 2 * 1e5;

int cur_pos[N + 5];
int tele[N + 5];
ll pred[N + 5];

int main() {
  AC int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> tele[i];
    tele[i]--;
  }
  memset(pred, -1, sizeof(pred));

  cur_pos[0] = 0;
  pred[0] = 0;
  for (ll i = 1; i <= k; i++) {
    cur_pos[i] = tele[cur_pos[i - 1]];
    if (pred[cur_pos[i]] != -1) {
      // city already visited
      ll cycle_len = i - pred[cur_pos[i]];
      ll last_steps = (k - i) % cycle_len;
      ll last_pos = cur_pos[pred[cur_pos[i]] + last_steps];
      cout << last_pos + 1 << endl;
      return 0;
    } else
      pred[cur_pos[i]] = i;
  }
  cout << cur_pos[k] + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ar array
#define nl '\\n'
#define AC                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
using namespace std;
const int M = 1e9 + 7;
const int N = 1e5;

int cur_pos[N + 5];
int tele[N + 5];
ll pred[N + 5];

int main() {
  AC int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> tele[i];
    tele[i]--;
  }
  memset(pred, -1, sizeof(pred));

  cur_pos[0] = 0;
  pred[0] = 0;
  for (ll i = 1; i <= k; i++) {
    cur_pos[i] = tele[cur_pos[i - 1]];
    if (pred[cur_pos[i]] != -1) {
      // city already visited
      ll cycle_len = i - pred[cur_pos[i]];
      ll last_steps = (k - i) % cycle_len;
      ll last_pos = cur_pos[pred[cur_pos[i]] + last_steps];
      cout << last_pos + 1 << endl;
      return 0;
    } else
      pred[cur_pos[i]] = i;
  }
  cout << cur_pos[k] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define erep(n) for (ll i = 0; i <= n; ++i)
#define erep2(i, n) for (ll i = 0; i <= n; ++i)
#define erep3(i, a, b) for (ll i = a; i <= b; ++i)
#define erep4(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define rrep2(i, a, b, c) for (ll i = a; i > b; i -= c)
#define errep(i, a, b) for (ll i = a; i >= b; --i)
#define errep2(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define print(a) cout << a << endl;
#define printVec(v)                                                            \\
  printf("{");                                                                 \\
  for (const auto &i : v) {                                                    \\
    std::cout << i << ", ";                                                    \\
  }                                                                            \\
  printf("}\\n");
#define cin(x) cin >> x;
const ll MOD = 1000000007;
inline int newline() {
  putchar('\\n');
  return 0;
}

int main() {
  ll n, k;
  cin(n);
  cin(k);

  vector<ll> a(n);
  rep2(i, n) cin(a[i]);

  ll town = 1;

  while (k) {
    if (k & 1)
      town = a[town - 1];

    vector<ll> an(n);
    rep2(i, n) an[i] = a[a[i] - 1];

    a.swap(an);

    k >>= 1;
  }

  print(town);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define erep(n) for (ll i = 0; i <= n; ++i)
#define erep2(i, n) for (ll i = 0; i <= n; ++i)
#define erep3(i, a, b) for (ll i = a; i <= b; ++i)
#define erep4(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define rrep2(i, a, b, c) for (ll i = a; i > b; i -= c)
#define errep(i, a, b) for (ll i = a; i >= b; --i)
#define errep2(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define print(a) cout << a << endl;
#define printVec(v)                                                            \\
  printf("{");                                                                 \\
  for (const auto &i : v) {                                                    \\
    std::cout << i << ", ";                                                    \\
  }                                                                            \\
  printf("}\\n");
#define cin(x) cin >> x;
const ll MOD = 1000000007;
inline int newline() {
  putchar('\\n');
  return 0;
}

int main() {
  ll n, k;
  cin(n);
  cin(k);

  vector<ll> a(n);
  rep2(i, n) cin(a[i]);

  ll town = 1;

  while (k) {
    if (k & 1)
      town = a[town];

    vector<ll> an(n);
    rep2(i, n) an[i] = a[a[i]];

    a.swap(an);

    k >>= 1;
  }

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

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &it : A)
    cin >> it;
  vector<int> used(N);
  used[1 - 1]++;
  int cnt = 0;
  cnt++;
  int next = A[0];
  vector<int> to;
  bool twice = false;
  while (true) {
    if (cnt == K) {
      cout << next << endl;
      return 0;
    }
    next = A[next - 1];
    cnt++;
    used[next - 1]++;
    if (used[next - 1] == 2)
      twice = true;
    if (used[next - 1] == 3)
      break;
    if (twice) {
      to.push_back(next);
    }
  }
  // cerr << cnt << endl;
  // cerr << to.size() << endl;
  K -= cnt;
  // for (auto &it : to)
  // cerr << it << endl;
  ll idx = K % to.size();
  ll ans = to[idx];
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &it : A)
    cin >> it;
  vector<int> used(N);
  used[1 - 1]++;
  int cnt = 0;
  cnt++;
  int next = A[0];
  vector<int> to;
  bool twice = false;
  while (true) {
    if (cnt == K) {
      cout << next << endl;
      return 0;
    }
    next = A[next - 1];
    cnt++;
    used[next - 1]++;
    if (used[next] == 2)
      twice = true;
    if (used[next] == 3)
      break;
    if (twice) {
      to.push_back(next);
    }
  }
  cerr << cnt << endl;
  cerr << to.size() << endl;
  K -= cnt;
  for (auto &it : to)
    cerr << it << endl;
  ll idx = K % to.size();
  ll ans = to[idx];
  cout << ans << endl;
  return 0;
}