alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(void) {

  int a[200010] = {};
  unsigned long long int visited[200010];
  int n;
  unsigned long long int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    visited[i] = -1;
  }
  int b = 1;
  visited[1] = 0;
  for (unsigned long long int i = 1; i <= k; i++) {
    b = a[b];
    if (visited[b] != -1) {
      k -= i;
      k %= (i - visited[b]);

      while (k--) {
        b = a[b];
      }
      break;
    }
    visited[b] = i;
  }

  cout << b << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(void) {

  int a[200010];
  int n;
  long long int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int b = 1;
  while (k--) {
    b = a[b];
  }

  cout << b << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef long long ll;
typedef long double ld;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second

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

  int n;
  ll k;
  cin >> n >> k;
  vi v(n);
  for (auto &z : v)
    cin >> z, --z;
  vector<bool> visited(n, 0);

  int tren = 0;
  while (k > 0) {
    if (visited[tren])
      break;
    visited[tren] = 1;
    tren = v[tren];
    // cout << tren+1 << '\\n';
    --k;
  }
  // cout << tren+1 << '\\n';
  if (k == 0) {
    cout << tren + 1 << '\\n';
    return 0;
  }

  vi put;
  put.pb(tren);
  int ntren = v[tren];

  while (ntren != tren) {
    put.pb(ntren);
    ntren = v[ntren];
  }
  // for(auto z : put)
  // cout << z << ' ';

  cout << put[k % sz(put)] + 1 << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef long long ll;
typedef long double ld;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second

const int MAXN = 2e5;
const int INF = 1e9;
vi adj[MAXN + 1], nz;
bool visited[MAXN + 1];
bool ok = 0;
int brojac = 0;
int taj;

void dfs(int s) {
  if (ok)
    return;
  if (visited[s]) {
    taj = s;
    ok = 1;
    return;
  }
  visited[s] = 1;
  ++brojac;
  dfs(adj[s][0]);
}

void dfs2(int s) {
  if (ok) {
    return;
  }

  if (!nz.empty() && nz[0] == s) {
    ok = 1;
    return;
  }

  nz.pb(s);
  dfs2(adj[s][0]);
}

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

  int n;
  ll k;
  cin >> n >> k;
  memset(visited, 0, sizeof(visited));

  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    adj[i].pb(a);
  }

  brojac = 0;
  dfs(1);
  ok = 0;
  k -= (ll)brojac;
  dfs2(taj);
  // for(auto z : nz)
  // cout << z << ' ';

  cout << nz[k % sz(nz)] << '\\n';
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a.at(i);
    a.at(i)--;
  }

  map<ll, ll> mp;
  ll count = 0;

  ll curr = 0;
  mp[curr] = 0;

  while (true) {
    curr = a[curr];
    count++;
    if (count == k) {
      cout << curr + 1 << endl;
      return 0;
    }
    if (mp.count(curr)) {
      break;
    }
    mp[curr] = count;
  }

  ll loop_begin = curr;
  ll loop = count - mp[loop_begin];

  k = k - mp[loop_begin];
  k = k % loop;

  curr = loop_begin;
  for (int i = 0; i < k; i++) {
    curr = a[curr];
  }

  cout << curr + 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a.at(i);
    a.at(i)--;
  }

  map<ll, ll> mp;
  ll count = 0;

  ll curr = 0;
  mp[curr] = 0;

  while (true) {
    curr = a[curr];
    count++;
    if (count == k) {
      return curr + 1;
    }
    if (mp.count(curr)) {
      break;
    }
    mp[curr] = count;
  }

  ll loop_begin = curr;
  ll loop = count - mp[loop_begin];

  k = k - mp[loop_begin];
  k = k % loop;

  curr = loop_begin;
  for (int i = 0; i < k; i++) {
    curr = a[curr];
  }

  cout << curr + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

// マクロ
// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x) begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
// 出力
#define PRINT(x) cout << (x) << endl
#define PRINTA(x, n) REP(i, n) PRINT(x[i])
// 定数
#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7
// 略記
#define PB push_back
#define MP make_pair

ll N, K, A[200009];

int main() {
  cin >> N >> K;
  FOR(i, 1, N) cin >> A[i];

  ll dest[200009];
  ll visited[200009];

  REP(i, N + 1) {
    dest[i] = 0;
    visited[i] = -1;
  }

  dest[0] = 1;
  visited[1] = 0;

  ll T, a;

  FOR(i, 1, N) {
    dest[i] = A[dest[i - 1]];
    if (visited[dest[i]] >= 0) {
      a = visited[dest[i]];
      T = i - a;
      break;
    }
    visited[dest[i]] = i;
  }

  ll ans;
  if (K < a)
    ans = dest[K];
  else {
    K -= a;
    ans = dest[a + K % T];
  }
  PRINT(ans);
  return 0;
}

Error: 0

Fixed submission:

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

// マクロ
// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (ll)(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) (x) begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
// 出力
#define PRINT(x) cout << (x) << endl
#define PRINTA(x, n) REP(i, n) PRINT(x[i])
// 定数
#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7
// 略記
#define PB push_back
#define MP make_pair

ll N, K, A[200009];

int main() {
  cin >> N >> K;
  FOR(i, 1, N) cin >> A[i];

  ll dest[200009];
  ll visited[200009];

  dest[0] = 1;
  visited[1] = 0;

  ll T, a;

  FOR(i, 1, N) {
    dest[i] = A[dest[i - 1]];
    if (visited[dest[i]]) {
      a = visited[dest[i]];
      T = i - visited[dest[i]];
      dest[i] = A[dest[i - 1]];
      break;
    }
    visited[dest[i]] = i;
  }

  ll ans;
  if (K < a)
    ans = dest[K];
  else {
    K -= a;
    ans = dest[a + K % T];
  }
  PRINT(ans);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n;
int a[maxn];
int vis[maxn];
int c, p;

void dfs(int i) {
  vis[i] = 1;
  if (vis[a[i]]) {
    p = a[i];
    return;
  }
  if (!vis[a[i]])
    dfs(a[i]);
}

void dfs1(int i) {
  vis[i] = 1;
  c++;
  if (!vis[a[i]])
    dfs1(a[i]);
}

int main() {
  n = read();
  ll k;
  cin >> k;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  dfs(1);
  memset(vis, 0, sizeof(vis));
  dfs1(p);
  int x = 1;
  while (k && x != p)
    x = a[x], k--;
  k %= c;
  for (int i = 0; i < k; ++i) {
    x = a[x];
  }
  cout << x << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 1e2 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n;
int a[maxn];
int vis[maxn];
int c, p;

void dfs(int i) {
  vis[i] = 1;
  if (vis[a[i]]) {
    p = a[i];
    return;
  }
  if (!vis[a[i]])
    dfs(a[i]);
}

void dfs1(int i) {
  vis[i] = 1;
  c++;
  if (!vis[a[i]])
    dfs1(a[i]);
}

int main() {
  n = read();
  ll k;
  cin >> k;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  dfs(1);
  memset(vis, 0, sizeof(vis));
  dfs1(p);
  int x = 1;
  while (k && x != p)
    x = a[x], k--;
  k %= c;
  x = p;
  for (int i = 0; i < k; ++i) {
    x = a[x];
  }
  cout << x << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define ins insert

#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)

#define int long long int

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

int32_t main() {
  fast();

  int n, k;

  cin >> n >> k;

  bool visited[n + 1];

  memset(visited, false, sizeof(visited));

  map<int, int> m;

  int a[n + 1];

  f1(i, n) {
    cin >> a[i];
    m[i] = a[i];
  }

  vector<int> res;

  int tmp, i, j;
  int prev = 1;
  res.pb(1);
  map<int, int> store;
  while (k) {
    if (!visited[prev]) {
      store[prev] = res.size() - 1;
      visited[prev] = true;

      prev = m[prev];

      res.pb(prev);
    } else {
      tmp = prev;
      break;
    }
    k--;
  }

  if (k) {
    i = store[tmp];
    j = res.size() - 1;
    int l = j - i;
    k %= l;

    i += k;

    cout << res[i];
  } else {
    cout << prev;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define ins insert

#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)

#define int long long int

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

int32_t main() {
  fast();

  int n, k;
  cin >> n >> k;
  int a[n + 1];

  map<int, int> m;

  f1(i, n) {
    cin >> a[i];
    m[i] = a[i];
  }

  int prev = 1;

  while (k) {
    prev = m[prev];
    k--;
  }

  cout << prev;

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

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

int main() {
  int64_t n, k;
  cin >> n >> k;
  vector<int64_t> a(n + 1);
  for (int64_t i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  vector<bool> visited(n + 1, false);
  visited.at(1) = true;
  vector<int64_t> seq(n, 0);
  seq.at(0) = 1;
  int64_t pos = 1;
  int64_t i;
  for (i = 1; i <= n; i++) {
    pos = a.at(pos);
    if (visited.at(pos)) {
      break;
    }
    visited.at(pos) = true;
    seq.at(i) = pos;
  }
  i--;
  int64_t j;
  for (j = 0; j < n; j++) {
    if (seq.at(j) == pos) {
      break;
    }
  }
  if (k <= i) {
    cout << seq.at(k) << endl;
  } else {
    cout << seq.at((k - j) % (i - j + 1) + j) << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int64_t n, k;
  cin >> n >> k;
  vector<int64_t> a(n + 1);
  for (int64_t i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  vector<bool> visited(n + 1, false);
  visited.at(1) = true;
  vector<int64_t> seq(n, 0);
  seq.at(0) = 1;
  int64_t pos = 1;
  int64_t i;
  for (i = 1; i <= n; i++) {
    pos = a.at(pos);
    if (visited.at(pos)) {
      break;
    }
    visited.at(pos) = true;
    seq.at(i) = pos;
  }
  int64_t j;
  for (j = 0; j < n; j++) {
    if (seq.at(j) == pos) {
      break;
    }
  }
  if (k <= i) {
    cout << seq.at(k) << endl;
  } else {
    cout << seq.at(((k - j) % (i - j + 1)) + j - 1) << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

long long n, arr[200005], counts[200005] = {0};
vector<long long> v;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long k, X;
  cin >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  long long idx = 1, num = 1, start;
  while (1) {
    if (counts[idx] > 0) {
      // cout<<"Counts : "<<counts[idx]<<endl;
      if (counts[idx] > k) {
        cout << v[k] << endl;
        return 0;
      }
      start = counts[idx];
      X = num - counts[idx];
      start--;
      v.erase(v.begin(), v.begin() + start);
      break;
    }
    counts[idx] = num;
    v.push_back(idx);
    idx = arr[idx];
    num++;
  }
  k -= start;
  X = k % X;
  // cout<<"K : "<<k<<endl;
  // cout<<"X : "<<X<<endl;
  cout << v[X] << endl;
}


Error: 0

Fixed submission:

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

long long n, arr[200005], counts[200005] = {0};
vector<long long> v;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long k, X;
  cin >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  long long idx = arr[1], num = 2, start;
  counts[1] = 1;
  v.push_back(1);
  while (1) {
    if (counts[idx] > 0) {
      start = counts[idx];
      X = num - counts[idx];
      start--;
      v.erase(v.begin(), v.begin() + start);
      break;
    }
    counts[idx] = num;
    v.push_back(idx);
    idx = arr[idx];
    num++;
  }
  k -= start;
  X = k % X;
  cout << v[X] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (auto &x : a)
    --x;
  set<ll> vs;
  vector<ll> vv;
  ll i = 0;
  vs.insert(i);
  while (true) {
    vv.push_back(i);
    i = a[i];
    ll siz = vs.size();
    vs.insert(i);
    if (siz == vs.size())
      break;
  }
  vector<ll> loop = vector<ll>(find(vv.begin(), vv.end(), i), vv.end());
  if (k < vv.size())
    cout << vv[k] + 1 << endl;
  else {
    k -= vv.size();
    cout << loop[k % loop.size()] + 1 << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <random>

using namespace std;

using ll = long long;

#define cout ofs
#define cin ifs
void solve() {
  ifstream ifs("in.txt");
  ofstream ofs("out.txt");
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (auto &x : a)
    --x;
  set<ll> vs;
  vector<ll> vv;
  ll i = 0;
  vs.insert(i);
  while (true) {
    vv.push_back(i);
    i = a[i];
    ll siz = vs.size();
    vs.insert(i);
    if (siz == vs.size())
      break;
  }
  vector<ll> loop = vector<ll>(find(vv.begin(), vv.end(), i), vv.end());
  if (k < vv.size())
    cout << vv[k] + 1 << endl;
  else {
    k -= vv.size();
    cout << loop[k % loop.size()] + 1 << endl;
  }
}
#undef ofs
#undef ifs

void make_testcase() {
  ofstream ofs("in.txt");
  random_device rnd;
  mt19937 mt(rnd());
  ll n = rnd() % (int)2e5;
  ll k = 5;
  ofs << n << " " << k << endl;
  uniform_int_distribution<> rand(1, n);
  for (ll i = 0; i < n; ++i) {
    ofs << rand(mt) << " ";
  }
  ofs << endl;
}

void solve_naive() {
  ifstream ifs("in.txt");
  ofstream ofs("out_naive.txt");
  ll n, k;
  ifs >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    ifs >> a[i];
  for (auto &x : a)
    --x;
  ll now = 0;
  for (ll i = 0; i < k; ++i) {
    now = a[now];
  }
  ofs << now + 1 << endl;
}

int main() {
  // make_testcase();
  solve();
  solve_naive();
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

typedef pair<int, int> p;
typedef long long ll;
const int inf = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  ll sid = 0;
  vector<ll> sumi(n, -1);
  ll beg = 0;
  ll t = 0;
  sumi[0] = 0;
  // cout << beg << ", ";
  for (ll i = 1; i < n; i++) {
    beg = a[beg];
    // cout << beg << ", ";
    if (sumi[beg] == -1) {
      sumi[beg] = i;
    } else {
      t = i - sumi[beg];
      sid = sumi[beg];
      break;
    }
  }
  // cout << endl;
  // cout << sid << ", " << t << endl;
  ll ans = 0;
  if (k <= sid) {
    rep(i, k) { ans = a[ans]; }
    cout << ans + 1 << endl;
    return 0;
  }
  ll id = (k - sid) % t;
  rep(i, sid + id) { ans = a[ans]; }
  cout << ans + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i]--;
  int tmp = 0;
  ll cnt = 0;
  vector<int> b(n);
  rep(i, n) {
    tmp = a[tmp];
    b[i] = tmp;
    cnt++;
    if (tmp == 0)
      break;
  }
  // rep(i, cnt) cout << b[i] << endl;
  int l = k % (cnt - 1);
  int ans = b[l] + 1;
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    a[i] = x;
  }
  set<int> st;
  st.insert(0);
  vint data;
  data.push_back(0);
  int x = 0;
  rep(i, n) {
    if (st.find(a[x]) == st.end()) {
      st.insert(a[x]);
      data.push_back(a[x]);
      x = a[x];
    } else {
      x = a[x];
      break;
    }
  }
  // vout(data);
  // cout << endl;
  // cout << x << endl;
  ll remain;
  rep(i, data.size()) {
    if (data[i] == x) {
      remain = i;
    }
  }
  if (remain >= k)
    cout << data[k] + 1 << endl;
  else {
    k -= remain;
    ll roop = data.size() - remain;
    ll t = k % roop;
    cout << data[remain + t] + 1 << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) {
    int x;
    cin >> x;
    x--;
    a[i] = x;
  }
  set<int> st;
  st.insert(0);
  vint data;
  int x = 0;
  rep(i, n) {
    if (st.find(a[x]) == st.end()) {
      st.insert(a[x]);
      data.push_back(a[x]);
      x = a[x];
    } else {
      x = a[x];
      break;
    }
  }
  // vout(data);
  // cout << endl;
  // cout << x << endl;
  ll remain;
  rep(i, data.size()) {
    if (data[i] == x) {
      remain = i + 1;
    }
  }
  // cout << remain << endl;
  k -= remain;
  ll roop = data.size() - remain + 1;
  ll t = k % roop;
  cout << data[remain - 1 + t] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

ll n, k, a[200009], vis[200009], dis[200009], clen;

ll dfs(ll s, ll d) {
  if (vis[s]) {
    clen = d - dis[s];
    return s;
  }

  vis[s] = 1;
  dis[s] = d;
  return dfs(a[s], d + 1);
}

int main() {
  ll i, s;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];

  s = dfs(1, 0);

  if (k < dis[s]) {
    s = 1;
    while (k--)
      s = a[s];
  }

  else {
    k = (k - dis[s]) % clen;

    while (k--)
      s = a[s];
  }

  cout << s;
}


Error: WA

Fixed submission:

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

int n, a[200009], vis[200009], dis[200009], clen;
ll k;

int dfs(int s, int d) {
  if (vis[s]) {
    clen = d - dis[s];
    return s;
  }

  vis[s] = 1;
  dis[s] = d;
  dfs(a[s], d + 1);
}

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

  s = dfs(1, 0);

  k = (k - dis[s]) % clen;

  while (k--)
    s = a[s];

  cout << s;
}