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

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

int main() {
  long long p = 0, q = 0;
  long long n = 0, k = 0;
  cin >> n >> k;
  vector<bool> a(n, false);
  vector<long long> b(n, 0);
  long long count = k, genzaiti = 1;
  bool kikkake = false;

  for (int i = 0; i < n; i++) {
    cin >> b.at(i);
  }
  while (kikkake == false) {
    if (count == 0) {
      goto FIN;
    }
    if (a.at(genzaiti - 1) == false) {
      a.at(genzaiti - 1) = true;
    } else {
      kikkake = true;
      break;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
  }
  p = genzaiti;
  while (kikkake == true) {
    if (count == 0) {
      goto FIN;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
    q++;

    if (p == genzaiti) {
      kikkake = false;
    }
  }
  count = count % q;
  while (kikkake == false) {
    if (count <= 0) {
      goto FIN;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
  }

FIN:
  cout << genzaiti;
}


Error: WA

Fixed submission:

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

int main() {
  int p = 0, q = 0;
  int n = 0, k = 0;
  cin >> n >> k;
  vector<bool> a(n, false);
  vector<long long> b(n, 0);
  int count = k, genzaiti = 1;
  bool kikkake = false;

  for (int i = 0; i < n; i++) {
    cin >> b.at(i);
  }
  while (kikkake == false) {
    if (count == 0) {
      goto FIN;
    }
    if (a.at(genzaiti - 1) == false) {
      a.at(genzaiti - 1) = true;
    } else {
      kikkake = true;
      break;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
  }
  p = genzaiti;
  while (kikkake == true) {
    if (count == 0) {
      goto FIN;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
    q++;

    if (p == genzaiti) {
      kikkake = false;
    }
  }
  count = count % q;
  while (kikkake == false) {
    if (count <= 0) {
      goto FIN;
    }
    genzaiti = b.at(genzaiti - 1);
    count--;
  }

FIN:
  cout << genzaiti;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  int che = 0, en;
  vector<int> wan, want;
  bool ok = true;
  map<int, int> mp;
  while (ok) {
    mp[che]++;
    if (mp[che] > 1)
      ok = false;
    if (ok) {
      wan.push_back(che);
    } else
      en = che;
    che = p[che];
  }
  ok = true;
  ll pre = -1;
  for (auto &v : wan) {
    if (ok) {
      pre++;
      if (v == en) {
        ok = false;
        want.push_back(en);
      }
    } else {
      want.push_back(v);
    }
  }
  ll len = want.size();
  if (k <= pre) {
    cout << wan[k] + 1 << endl;
    return 0;
  }
  k -= pre;
  k %= len;
  cout << want[k] + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  int che = 0, en;
  vector<int> wan, want;
  bool ok = true;
  map<int, int> mp;
  while (ok) {
    mp[che]++;
    for (auto &v : mp) {
      if (v.second > 1) {
        ok = false;
        break;
      }
    }
    if (ok) {
      wan.push_back(che);
      che = p[che];
    } else {
      en = che;
    }
  }
  ok = true;
  ll pre = -1;
  for (auto &v : wan) {
    if (ok) {
      pre++;
      if (v == en) {
        ok = false;
        want.push_back(en);
      }
    } else {
      want.push_back(v);
    }
  }
  ll len = want.size();
  if (k <= pre) {
    cout << wan[k] + 1 << endl;
    return 0;
  }
  k -= pre;
  k %= len;
  cout << want[k] + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <array>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(void) {
  int n;
  long int k;
  std::array<int, 200000> next_town;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int next;
    cin >> next;
    next_town[i] = next - 1;
  }

  int current_index = 0;

  std::map<int, int> arrive_time;
  bool dumped = false;
  for (long int i = 0; i < k; i++) {
    arrive_time[current_index] = i;
    current_index = next_town[current_index];

    if (!dumped && arrive_time.find(current_index) != arrive_time.end()) {
      int roop_start = arrive_time[current_index];
      int roop_length = i - roop_start + 1;
      i = k - (k - roop_start) % roop_length - 1;
      dumped = true;
    }
  }
  cout << current_index + 1;
}

Error: WA

Fixed submission:

#include <array>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(void) {
  int n;
  long int k;
  std::array<int, 200000> next_town;
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int next;
    cin >> next;
    next_town[i] = next - 1;
  }

  int current_index = 0;

  std::map<int, int> arrive_time;
  bool dumped = false;
  for (long int i = 0; i < k; i++) {
    arrive_time[current_index] = i;
    current_index = next_town[current_index];

    if (!dumped && arrive_time.find(current_index) != arrive_time.end()) {
      int roop_start = arrive_time[current_index];
      int roop_length = i - roop_start;
      i = k - (k - 1 - roop_start) % roop_length - 1;
      dumped = true;
    }
  }
  cout << current_index + 1;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: 0

Fixed submission:

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

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  vector<long long> ans(n, n + 1);
  ans.at(0) = a.at(0);
  long long A = 0;
  long long B = 0;
  for (long long i = 0; i < n - 1; i++) {
    ans.at(i + 1) = a.at(ans.at(i) - 1);
    for (long long j = 0; j <= i; j++) {
      if (ans.at(i + 1) == 1) {
        A = i + 1;
        B = 1;
        break;
      }
      if (ans.at(i + 1) == ans.at(j)) {
        A = i + 2;
        B = j + 1;
        break;
      }
    }
  }
  vector<long long> ANS(A - B);
  for (long long i = 0; i < A - B; i++) {
    if (B + i >= n) {
      cout << a.at(k % n - 1) << endl;
      return 0;
    }
    ANS.at(i) = ans.at(B + i);
  }
  if ((k - B) % (A - B) == 0) {
    cout << ANS.at(A - B - 1) << endl;
    return 0;
  }
  cout << ANS.at((k - B) % (A - B) - 1) << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void printv(vector<ll> v) {
  for (ll elem : v)
    cout << elem << " ";
  cout << endl;
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<ll> v;
  vector<bool> b(n + 1, false);
  ll i = 1;
  while (!b[i]) {
    v.push_back(i);
    b[i] = true;
    i = a[i];
  }
  // cout << "a = "; printv(a);
  // cout << "v = "; printv(v);
  if (k < (ll)v.size()) {
    cout << v[k] << endl;
  } else {
    ll j;
    // printf("a[v.back()] = %lld\\n", a[v.back()]);
    ll m = (ll)v.size();
    for (j = 0; j < m; j++) {
      // printf("j = %lld v[j] = %lld\\n", j, v[j]);
      if (v[j] == a[v.back()]) {
        if (j != 0) {
          v.erase(v.begin(), v.begin() + j);
        }
        break;
      } else {
        // v.erase(v.begin());
        // cout << "v = "; printv(v);
      }
    }
    k -= j;
    // cout << "v = "; printv(v);
    cout << v[k % v.size()] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void printv(vector<ll> v) {
  for (ll elem : v)
    cout << elem << " ";
  cout << endl;
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<ll> v;
  vector<bool> b(n + 1, false);
  ll i = 1;
  while (!b[i]) {
    v.push_back(i);
    b[i] = true;
    i = a[i];
  }
  // cout << "a = "; printv(a);
  // cout << "v = "; printv(v);
  if (k < (ll)v.size()) {
    cout << v[k] << endl;
  } else {
    ll j;
    // printf("a[v.back()] = %lld\\n", a[v.back()]);
    ll m = (ll)v.size();
    for (j = 0; j < m; j++) {
      // printf("j = %lld v[j] = %lld\\n", j, v[j]);
      if (v[0] == a[v.back()]) {
        break;
      } else {
        v.erase(v.begin());
        // cout << "v = "; printv(v);
      }
    }
    k -= j;
    // cout << "v = "; printv(v);
    cout << v[k % v.size()] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  unsigned long int k;
  int *a, *cntr;
  cin >> n >> k;
  a = new int[2 * n];
  cntr = a + n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cntr[i] = 0;
  }
  int crt_twn = 1;
  unsigned long int roop_twn, rest;
  int rp_flag = 0;
  for (unsigned long int i = 0; i < k; i++) {
    // cout<<crt_twn<<endl;
    crt_twn = a[crt_twn - 1];
    if (cntr[crt_twn - 1] != 0) {
      roop_twn = i - cntr[crt_twn - 1];
      rest = k - i - 1;
      rp_flag = 1;
      // cout << "over"<< endl << "rootptwn" <<  roop_twn << endl;
      break;
    }
    cntr[crt_twn - 1] = i;
  }

  // cout << endl << "rest" << rest << endl;
  // cout << rp_flag;
  if (rp_flag == 1) {
    rest = rest % roop_twn;
    // cout << rest;
    for (int j = 0; j < rest; j++) {
      // cout<<crt_twn<<endl;
      crt_twn = a[crt_twn - 1];
    }
  }

  cout << crt_twn;

  delete[] a;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  int *a;
  cin >> n >> k;
  a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int crt_twn = 1;
  for (int i = 0; i < k; i++) {
    crt_twn = a[crt_twn - 1];
  }
  cout << crt_twn;

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

#include <deque>
#include <iostream>
#include <vector>

using namespace std;
int n;
long long k;

int main() {
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  vector<int> p;            // path
  vector<int> d(n + 1, -1); // discovered
  int cycle_len = 1, acyclic_len = 0;
  {
    int v = 1, t = 0;
    while (d[v] == -1) {
      // visit the vertex
      d[v] = t;
      t++;
      p.push_back(v);

      // move to the next vertex
      v = a[v - 1];
    }
    cycle_len = t - d[v];
    acyclic_len = d[v];
  }

  if (k <= acyclic_len) {
    cout << p[k] << endl;
  } else {
    k -= acyclic_len;
    k %= cycle_len;
    k += acyclic_len;
    cout << p[k] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <deque>
#include <iostream>
#include <vector>

using namespace std;
int n;
long long k;

int solve() {
  vector<int> a(n + 1, 0);
  deque<bool> visited(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }

  int cycle_ptr, cur = 1;
  while (true) {
    if (k == 0) {
      return cur;
    }
    if (visited[cur]) {
      cycle_ptr = cur;
      break;
    }
    visited[cur] = true;
    cur = a[cur];
    k--;
  }

  int cycle_len = 0;
  do {
    if (k == 0) {
      return cur;
    }
    cycle_len++;
    cur = a[cur];
    k--;
  } while (cur != cycle_ptr);

  k = k % cycle_len;
  do {
    if (k == 0) {
      return cur;
    }
    cur = a[cur];
    k--;
  } while (k > 0);
}

int main() {
  cin >> n >> k;
  printf("%d\\n", solve());
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

/*
        Date:2020.5.10
        Name:Aliemo
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 200010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int n, k, now, last, sum, ans;

int a[MAXN], vis[MAXN];

bool f;

signed main() {
  n = read(n);
  k = read(k);
  for (rr int i = 1; i <= n; i++)
    a[i] = read(a[i]);
  now = 1;
  vis[now]++;
  while (k--) {
    now = a[now];
    // cout << now;
    if (vis[now] == 1 && !f) {
      // ans++;
      vis[now]++;
      f = 1;
      int t = now;
      t = a[t];
      ans++;
      while (vis[t] != 2) {
        t = a[t];
        ans++;
      }
      // cout << ans << " ";
      k %= ans;
    }
    vis[now]++;
  }
  cout << now;
}


Error: WA

Fixed submission:

/*
        Date:2020.5.10
        Name:Aliemo
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 1010
#define inf 1e18

using namespace std;

inline int read(int &T) {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return T = f ? -s : s;
}

void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}

int n, k, now, last, sum, ans;

int a[MAXN], vis[MAXN];

bool f;

signed main() {
  n = read(n);
  k = read(k);
  for (rr int i = 1; i <= n; i++)
    a[i] = read(a[i]);
  now = 1;
  vis[now]++;
  while (k--) {
    now = a[now];
    // cout << now;
    if (vis[now] == 1 && !f) {
      // ans++;
      vis[now]++;
      f = 1;
      int t = now;
      t = a[t];
      ans++;
      while (vis[t] != 2) {
        t = a[t];
        ans++;
      }
      // cout << ans << " ";
      k %= ans;
    }
    vis[now]++;
  }
  cout << now;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)
const ll PSIZE = 10000000;

int ans;
void dfs(int s, ll mark[], ll a[], ll k, ll step, ll n) {
  if (step == k) {
    ans = s;
    return;
  }

  if (mark[s] != 0 || (s == 1 && step != 0)) {
    ll cir = step - mark[s];
    ll path = (k - mark[s]) % cir;
    ll mark2[n + 1];
    memset(mark2, 0, sizeof(mark2));
    dfs(s, mark2, a, path, 0, n);
    return;
  }

  mark[s] += step;
  dfs(a[s], mark, a, k, step + 1, n);
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 1, n + 1) { cin >> a[i]; }
  ll mark[n + 1];
  memset(mark, 0, sizeof(mark));
  dfs(1, mark, a, k, 0, n);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)
const ll PSIZE = 10000000;

ll frac(int n) {
  ll ans = 1;
  rep(i, 2, n + 1) { ans *= i; }
  return ans;
}
int ans;
void dfs(int s, ll mark[], ll a[], ll k, ll step, ll n) {
  if (step == k) {
    ans = s;
    return;
  }

  if (mark[s] != 0 || (s == 1 && step != 0)) {
    ll cir = step - mark[s];
    ll path = (k - mark[s]) % cir;
    ll mark2[n + 1];
    memset(mark2, 0, sizeof(mark2));
    dfs(s, mark2, a, path, 0, n);
    return;
  }

  mark[s] += step;
  dfs(a[s], mark, a, k, step + 1, n);
}

int main() {
  int n, m, k;

  cin >> n >> m >> k;

  ll com = frac(n - 1) / (frac(n - 1 - k) * frac(k));
  ll ans = com * m * (m - 1) * (m - k);
  cout << ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  long long N;
  long long K;
  cin >> N >> K;

  long long A[N];
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }

  bool visit[N];
  for (long long i = 0; i < N; i++) {
    visit[i] = false;
  }
  long long M = 0;
  long long start = 1;
  while (!visit[start - 1]) {
    visit[start - 1] = true;
    start = A[start - 1];
    M++;
  }
  long long loop = start;

  // independent
  start = 1;
  long long m = 0;
  while (start != loop) {
    start = A[start - 1];
    m++;
  }

  if (K < M) {
    start = 1;
    for (long long i = 0; i < K; i++) {
      start = A[start - 1];
    }
  } else {
    K = (K - m) % (M - m);

    start = 1;
    for (long long i = 0; i < m + K; i++) {
      start = A[start - 1];
    }
  }
  cout << start << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  long long N;
  long long K;
  cin >> N >> K;

  long long A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i + 1 == A[i]) {
      N--;
    }
  }

  K %= N;

  int start = 1;

  for (int i = 0; i < K; i++) {
    start = A[start - 1];
  }
  cout << start << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll, ll> T;
#define PI 3.14159265359
#define MOD 1000000007
ll A[200001];
ll visited[200001];
ll roop = 0;
ll end = 0;
ll length = 0;
// ループを探す
void cicle(ll x, ll cn) {
  ll p = A[x];
  if (visited[p] == -1) {
    visited[p] = cn + 1;
    cicle(p, cn + 1);
  } else {
    ::roop = cn + 1 - visited[p];
    ::end = p;
    ::length = cn + 1;
  }
}
// xからn回で行ける位置をかえす
ll ans = 0;
void go(ll x, ll n) {
  if (n == 0) {
    ::ans = x;
  } else {
    go(A[x], n - 1);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  rep(i, 200001) {
    visited[i] = -1;
    A[i] = 0;
  }
  visited[0] = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    a--;
    A[i] = a;
  }
  cicle(0, 0);
  if (K > ::length) {
    K -= ::length;
    K %= ::roop;
    go(::end, K);
  } else {
    go(0, K);
  }
  cout << ::ans + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll, ll> T;
#define PI 3.14159265359
#define MOD 1000000007
ll A[200001];
ll visited[200001];
ll roop = 0;
ll end = 0;
ll length = 0;
// ループを探す
ll cicle(ll x, ll cn) {
  ll p = A[x];
  if (visited[p] == -1) {
    visited[p] = cn + 1;
    cicle(p, cn + 1);
  } else {
    ::roop = cn + 1 - visited[p];
    ::end = p;
    ::length = cn + 1;
  }
}
// xからn回で行ける位置をかえす
ll ans = 0;
ll go(ll x, ll n) {
  if (n == 0) {
    ::ans = x;
  } else {
    go(A[x], n - 1);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  rep(i, 200001) {
    visited[i] = -1;
    A[i] = 0;
  }
  visited[0] = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    a--;
    A[i] = a;
  }
  cicle(0, 0);
  if (K > ::length) {
    K -= ::length;
    K %= ::roop;
    go(::end, K);
  } else {
    go(0, K);
  }
  cout << ::ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// map<string,ll> mp,mp1;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<char> deq;
// vector<ll> vll;
ll num[ten52];
ll path[ten52];

int main(void) {
  // double ans=0.0,d,d1,d2;
  // float f,g,h;
  long long int m, n, i, j, k = 0, save;
  long long int a, a1, a2, a3, sum;
  string s, s1, s2;
  // char c,c1;
  sum = a = a1 = a2 = a3 = 0;
  cin >> n >> m;
  save = m;
  for (i = 0; i < n; i++) {
    cin >> num[i];
    path[i + 1] = -1;
  }
  for (i = 1, j = 1; i <= (n >= m ? m : n); i++) {
    if (path[num[j - 1]] == -1) {
      path[num[j - 1]] = i;
      j = num[j - 1];
    } else {
      sum = i - path[num[j - 1]];
      m -= path[num[j - 1]];
      break;
    }
  }
  if (save != m) {
    m %= sum;
    for (j = 0, k = 1; j < i - sum + m; j++)
      k = num[k - 1];
  } else
    for (j = 0, k = 1; j < m; j++)
      k = num[k - 1];
  cout << k;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// map<string,ll> mp,mp1;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<char> deq;
// vector<ll> vll;
ll num[ten52];
ll path[ten52];

int main(void) {
  // double ans=0.0,d,d1,d2;
  // float f,g,h;
  long long int m, n, i, j, k = 1;
  long long int a, a1, a2, a3, sum;
  string s, s1, s2;
  // char c,c1;
  sum = a = a1 = a2 = a3 = 0;
  cin >> n >> m;
  path[1] = 0;
  for (i = 0; i < n; i++) {
    cin >> num[i];
    path[i + 1] = -1;
  }
  for (i = 1, j = 1; i <= m; i++) {
    if (path[num[j - 1]] == -1) {
      path[num[i - 1]] = i;
      j = num[j - 1];
    } else {
      sum = i - path[num[j - 1]];
      m -= path[num[j - 1]];
      break;
    }
  }
  m %= sum;
  for (j = 0, k = 1; j < i - sum + m; j++)
    k = num[k - 1];
  cout << k;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, pos = 0;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  map<ll, ll> voyage;
  while (voyage[pos] < 1 && k > 0) {
    voyage[pos] = k;
    pos = a[pos];
    k--;
  }
  if (voyage[pos] > 0)
    k %= voyage[pos] - k;
  while (k > 0) {
    pos = a[pos];
    k--;
  }
  cout << pos + 1 << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k, pos = 0;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }

  map<ll, ll> voyage;
  while (voyage[pos] < 1 && k > 0) {
    voyage[pos] = k;
    pos = a[pos];
    k--;
  }
  k %= voyage[pos] - k;
  while (k > 0) {
    pos = a[pos];
    k--;
  }
  cout << pos + 1 << "\\n";

  return 0;
}