alexjercan/bugnet huggingface dataset:
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 unsigned long long ull;

int main() {
  int n;
  ull k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i)--;
  }
  vector<int> flag(n);
  vector<int> anslst(n);
  int count = 0;
  int now = 0;
  //    while(1) {
  //        if (cnt==k) {
  //            cout << now+1 << endl;
  //        }
  //    }

  while (flag[now] == 0) {
    if (count >= k) {
      cout << now + 1 << endl;
      return 0;
    }
    flag[now] = 1;
    anslst[count] = now;
    now = a[now];
    count++;
  }
  int start = 0;
  for (int i = 0; i < count; i++) {
    if (anslst[i] == now) {
      start = i;
      break;
    } else {
      continue;
    }
  }
  int loop = count - start;
  if (loop == 0) {
    cout << now << endl;
  } else {
    int mod = (k - start) % loop;
    cout << anslst[mod + start] + 1 << endl;
    //        cout << anslst[k%loop+start]+1 << endl;
  }

  return 0;
}

/* うまくいかなかったコード
int main() {
    ll n,k;
    cin >> n>>k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
//        int tmp=0;
//        cin >> tmp; tmp--;
//        a[i] = tmp;
        cin >> a.at(i);
        a.at(i)--;
//        cin >> a[i];
//        a[i]--;
    }
    vector<int> flag(n);
    vector<int> anslst(n);
    ll count=0;
    ll now= 0;
    while (flag[now] == 0) {
        if(count>=k){
            cout << now << endl;
            return 0;
        }
        flag[now] = 1;
        anslst[count] = now;
        now = a[now];
        count++;
    }
    int start=0;
    for(int i=0;i<count;i++){
        if(anslst[i]==now) {
            start = i;break;
        }else{continue;}
    }
    int loop = count-start;
    if (loop == 0) {
        cout << now <<endl;
    }else{
        ll mod = (k-start)%loop;
        cout << anslst[mod+start]+1 << endl;
//        cout << anslst[k%loop+start]+1 << endl;
    }



    return 0;
}

*/


Error: 0

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i)--;
  }

  vector<int> flag(n);
  vector<int> anslst(n);
  ll count = 0;
  ll now = 0;
  while (flag[now] == 0) {
    if (count >= k) {
      cout << now << endl;
      return 0;
    }
    flag[now] = 1;
    anslst[count] = now;
    now = a[now];
    count++;
  }
  int start = 0;
  for (int i = 0; i < count; i++) {
    if (anslst[i] == now) {
      start = i;
      break;
    } else {
      continue;
    }
  }
  int loop = count - start;
  if (loop == 0) {
    cout << now << endl;
  } else {
    ll mod = (k - start) % loop;
    cout << anslst[mod + start] + 1 << endl;
    //        cout << anslst[k%loop+start]+1 << endl;
  }

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

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

typedef unsigned long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 200001
#define int ll
ll N, K;
int A[maxn];
int vis[maxn];

int32_t main() {
  fast;

  cin >> N >> K;

  FOR(i, 1, N) { cin >> A[i]; }

  int x = 1;

  while (!vis[x]) {
    vis[x] = 1;
    x = A[x];
  }

  int num = 0;
  int start = x;
  do {
    num++;
    x = A[x];
  } while (x != start);

  x = 1;

  while (K > 0 && x != start) {
    x = A[x];
    K--;
  }

  K %= num;

  FOR(i, 1, K) x = A[x];
  cout << x;
}


Error: -8

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 200001
#define int ll
int N, K;
int A[maxn];

int32_t main() {
  fast;

  cin >> N >> K;

  FOR(i, 1, N) { cin >> A[i]; }

  int x = 1;
  int y = 1;
  int num = 0;

  do {
    x = A[x];
    y = A[A[y]];
  } while (x != y);

  while (x != y) {
    num++;
    x = A[x];
  }

  K %= num;

  int ans = 1;
  FOR(i, 1, K) { ans = A[ans]; }

  cout << ans;
}
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;

int main() {
  ll N, K;
  cin >> N >> K;

  // データ入力
  vector<ll> town(N + 1);
  vector<ll> step(N + 1, 0);
  for (ll i = 1; i <= N; i++) {
    cin >> town[i];
  }

  ll now = 1;
  ll step_su = 1;
  step[1] = 0;
  while (K > 0) {
    now = town[now];
    step_su++;
    K--;
    // 訪れたことなければ更新
    if (step[now] == 0) {
      step[now] = step_su;
    } else {
      ll x = step_su - step[now];
      K %= x;
    }

    // cout << now << " ";
  }
  // cout << endl;
  // cout << "ans" << endl;
  cout << now << endl;
}


Error: 0

Fixed submission:

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

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

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    // cout << "A.at(i):" << A.at(i) << " ";
  }

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

#include <bits/stdc++.h>
#define N 522222
#define LL long long

using namespace std;

vector<LL> loop;

LL pn, to[N], tims;
bool is_v[N], is_in[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> pn >> tims;
  for (int i = 1; i <= pn; i++) {
    cin >> to[i];
  }
  int nat = 1;
  while (is_v[nat] == 0 && tims != 0) {
    tims--;
    is_v[nat] = 1;
    nat = to[nat];
  }
  if (tims == 0)
    cout << nat << endl;
  else {
    while (is_in[nat] == 0) {
      loop.push_back(nat);
      is_in[nat] = 1;
      nat = to[nat];
    }
    cout << loop[tims % loop.size()] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 522222
#define LL long long

using namespace std;

vector<LL> loop;

LL pn, to[N], tims;
bool is_v[N], is_in[N];

int main() {
  // ios_base::sync_with_stdio(0);
  cin >> pn >> tims;
  for (int i = 1; i <= pn; i++) {
    cin >> to[i];
  }
  int nat = 1;
  while (is_v[nat] == 0 && tims != 0) {
    tims--;
    is_v[nat] = 1;
    nat = to[nat];
  }
  if (tims == 0)
    cout << nat << endl;
  else {
    while (is_in[nat] == 0) {
      loop.push_back(nat);
      is_in[nat] = 1;
      nat = to[nat];
    }
  }
  cout << loop[tims % loop.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, k;
  cin >> n >> k;
  int a[n];
  map<int, int> m;
  m[1]++;
  vector<int> v;
  v.push_back(1);
  int index = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0;; i = a[i] - 1) {
    m[a[i]]++;
    if (m[a[i]] <= 1) {
      v.push_back(a[i]);
      cout << endl;
    } else {

      for (int j = 0; j < v.size(); j++)
        if (v[j] == a[i]) {
          index = j;
          break;
        }
      break;
    }
  }
  if (k < index) {
    cout << v[k] << endl;
    return 0;
  }
  k -= index;
  k = k % (v.size() - index);
  cout << v[index + k] << endl;

  // cout<<k<<" "<<t<<" "<<v[k]<<endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define watch(x) cerr << (#x) << " is " << x << nl;
#define fi(a, b) for (ll i = a; i < b; i++)
#define fj(a, b) for (ll j = a; j < b; j++)
#define mp make_pair
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define pb(i) push_back(i)
#define mem(v, a) memset((v), a, sizeof(v));
#define mll map<ll, ll>
#define vll vector<ll>
#define nl endl
const long long inf = 1e18 + 1e17;
const int mod = 998244353;
const ll N = 600032LL;

void solve() {
  ll n, k;
  cin >> n >> k;
  vll v(n);
  fi(0, n) cin >> v[i];
  ll ar[n];
  memset(ar, 0, sizeof(ar));
  ll i = 0;
  for (; ar[i] == 0; i = v[i] - 1) {
    k--;
    ar[i]++;
    fi(0, n) cerr << ar[i] << " ";
    cerr << endl;
  }
  cerr << endl;
  ll len = 1;
  for (ll j = v[i] - 1; j != i; j = v[j] - 1) {
    len++;
    cerr << j + 1 << " ";
  }
  k = k % len;

  cerr << endl << len << " " << i << " " << k << endl;
  while (k-- > 0)
    i = v[i] - 1;
  cout << i + 1;
}
int main() {
  ll t;
  t = 1;
  ;
  while (t--) {
    // double p=1.0;p-=1;
    // printf("%.10lf",p);
    solve();
    // cout<<binfind(4);
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A[i] = tmp - 1;
  }
  vector<int> dest;
  int pos = 0;
  vector<bool> visited(N, false);
  while (!visited[pos]) {
    dest.push_back(pos);
    visited[pos] = true;
    pos = A[pos];
  }
  int loop_end = dest.back();
  int loop_start = A[loop_end];
  int loop_res;
  for (int i = 0; i < N; i++) {
    if (dest[i] == loop_start) {
      loop_res = i;
      break;
    }
  }
  int loop_size = dest.size() - loop_res;
  if (K < loop_res)
    printf("%d\\n", dest[K] + 1);
  else
    printf("%d\\n", dest[loop_res + (K - loop_res) % loop_size] + 1);
  return 0;
}


Error: 0

Fixed submission:

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A[i] = tmp - 1;
  }
  vector<int> dest;
  int pos = 0;
  vector<bool> visited(N, false);
  while (1) {
    dest.push_back(pos);
    if (visited[pos])
      break;
    visited[pos] = true;
    pos = A[pos];
  }
  int loop_end = dest.back();
  int loop_start = A[loop_end];
  int loop_res;
  for (int i = 0; i < N; i++) {
    if (dest[i] == loop_start)
      loop_res = i;
  }
  int loop_size = dest.size() - loop_res;
  if (K < loop_res)
    printf("%d\\n", dest[K] + 1);
  else
    printf("%d\\n", dest[loop_res + (K - loop_res) % loop_size] + 1);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define I(x) cin >> x;
#define D(x) cerr << (x) << " ";
#define BR cerr << "\\n";
#define P(x) cout << (x) << endl;
#define FIX cout << fixed << setprecision(10);

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;
vvl table(61, vl(200005));

int main(void) {

  I(N);
  I(K);
  FOR(i, 1, N + 1) { I(table[1][i]); }
  FOR(i, 2, 61) {
    FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; }
  }

  ll now = 1;
  FOR(i, 1, 61) {
    if ((K >> (i - 1)) & 1) {
      now = table[i][now];
    }
  }

  P(now);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define I(x) cin >> x;
#define D(x) cerr << (x) << " ";
#define BR cerr << "\\n";
#define P(x) cout << (x) << endl;
#define FIX cout << fixed << setprecision(10);

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;
vvl table(61, vl(ARRAY));

int main(void) {

  I(N);
  I(K);
  FOR(i, 1, N + 1) { I(table[1][i]); }
  FOR(i, 2, 61) {
    FOR(j, 1, N + 1) { table[i][j] = table[i - 1][table[i - 1][j]]; }
  }

  ll now = 1;
  FOR(i, 1, 61) {
    if ((K >> (i - 1)) & 1) {
      now = table[i][now];
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

int visited[200001];
int a[200001];
int cnt = 0;
int l = -1;

void dfs(int curr, int parent) {
  cnt++;
  if (!visited[curr]) {
    visited[curr] = cnt;
    dfs(a[curr], curr);
  } else
    l = parent;
}

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

  int n;
  ll k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dfs(1, 0);
  int s = visited[l] - visited[a[l]] + 1;
  vi topo;
  topo.push_back(a[l]);
  for (int i = 1; i < s; i++) {
    topo.push_back(a[topo[i - 1]]);
  }
  ll k2 = k - visited[a[l]] + 1;
  if (k2 < 0) {
    int tmp = 1;
    while (k--) {
      tmp = a[tmp];
    }
    cout << tmp;
  } else
    cout << topo[k2 % s];
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

int visited[200001];
int a[200001];
int cnt = 0;
int l;

void dfs(int curr, int parent) {
  cnt++;
  if (!visited[curr]) {
    visited[curr] = cnt;
    dfs(a[curr], curr);
  } else
    l = parent;
}

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

  int n;
  ll k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dfs(1, 0);
  int s = visited[l] - visited[a[l]] + 1;
  vi topo;
  topo.push_back(a[l]);
  for (int i = 1; i < s; i++) {
    topo.push_back(a[topo[i - 1]]);
  }
  ll k2 = k - visited[a[l]] + 1;
  cout << topo[k2 % s];
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

const int D = 60;
int main() {
  int i, j, n, now = 0;
  ll k;
  cin >> n >> k;
  int a[n][D];
  rep(i, n) cin >> a[i][0];
  // qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  rep(i, D - 1) rep(j, n) a[j][i + 1] = a[a[j][i] - 1][i];
  now = 1;
  rep(i, D) {
    if (k >> i & 1) {
      now = a[now - 1][i];
      //			cout << now << endl;
    }
  }
  // ooooooooooooooooooooooooooooooooooooooo
  cout << now << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using namespace std;
int main() {
  long i, j, n, ans = 0;
  long long k;
  cin >> n >> k;
  long a[n][50];
  rep(i, n) { cin >> a[i][0]; }
  rep(i, n) {
    rep(j, 49) { a[i][j + 1] = a[a[i][j]][j]; }
  }

  rep(i, 50) {
    if (k and pow(2, i)) {
      ans = a[ans][i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  v[0] = 0;
  int c = 0;
  for (int i = 1; i <= n; i++) {
    int d;
    cin >> d;
    v[i] = d;
    if (d == 1) {
      c++;
    }
  }

  vector<int> t(n + 1, 0);
  vector<int> l, var;
  // for(auto x:s){cout<<x<<" ";}cout<<endl;
  int i = 1;
  t[i] = 1;
  while (1) {
    int c = v[i];
    var.push_back(c);
    t[c]++;
    if (t[v[i]] == 2) {
      t[v[i]]--;
      var.pop_back();
      while (1) {
        int c = v[i];
        t[c]++;
        if (t[v[i]] == 3)
          break;
        l.push_back(v[i]);
        i = v[i];
      }
      break;
    }
    i = v[i];
  }
  /* for(auto x:intial){cout<<x<<" ";}cout<<endl;
  for(auto x:loop){cout<<x<<" ";}cout<<endl;*/
  if (k < var.size()) {
    cout << var[k - 1];
  } else {
    k -= var.size();
    k = k % l.size();
    if (k == 0)
      cout << l[l.size() - 1];
    else
      cout << l[k - 1];
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  v[0] = 0;
  int c = 0;
  for (int i = 1; i <= n; i++) {
    int d;
    cin >> d;
    v[i] = d;
    if (d == 1) {
      c++;
    }
  }
  vector<bool> visited(n + 1, false);
  vector<int> s;
  int x = 1;

  while (!visited[x]) {
    if (c > 0) {
      s.push_back(x);
    } else {
      s.push_back(v[x]);
    }
    visited[x] = true;
    x = v[x];
  }
  // for(auto x:s){cout<<x<<" ";}
  int d = k % s.size();
  cout << v[s[d - 1]];
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
using namespace std;
const int N = 2e5 + 5;
int n, x[N], ans;
ll m;
bool vis[N];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> x[i], x[i]--;
  int now = 0, cnt = 0;
  if (m <= n) {
    while (m--) {
      ans = x[ans];
    }
    cout << ans + 1 << "\\n";
    return 0;
  }
  while (!vis[now]) {
    vis[now] = true;
    now = x[now];
    cnt++;
  }
  int rem = now;
  m -= cnt;
  cnt = 1;
  now = x[now];
  while (rem != now) {
    now = x[now];
    cnt++;
  }
  m %= cnt;
  ans = rem;
  while (m--) {
    ans = x[ans];
  }
  cout << ans + 1 << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
int n, x[N], ans;
ll m;
bool vis[N];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> x[i], x[i]--;
  int now = 0, cnt = 0;
  if (m <= n) {
    while (m--) {
      ans = x[ans];
    }
    cout << ans << "\\n";
    return 0;
  }
  while (!vis[now]) {
    vis[now] = true;
    now = x[now];
    cnt++;
  }
  int rem = now;
  m -= cnt;
  cnt = 1;
  now = x[now];
  while (rem != now) {
    now = x[now];
    cnt++;
  }
  m %= cnt;
  ans = rem;
  while (m--) {
    ans = x[ans];
  }
  cout << ans + 1 << "\\n";

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

#include <bits/stdc++.h>
#define MAX 200010

using namespace std;

int gotown(long long int &count, int *townmap, int *route, int index) {
  vector<int> path;
  long long int towntime = 1;
  while (route[index] == 0 && count != 0) {
    path.push_back(index);
    route[index] = towntime;
    index = townmap[index];
    towntime++;
    count--;
  }
  return ((count == 0)
              ? index
              : path[(count % (towntime - route[index])) + route[index] - 1]);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int townnum, townmap[MAX], route[MAX] = {0};
  long long int count;
  cin >> townnum >> count;
  for (int i = 1; i <= townnum; i++)
    cin >> townmap[i];
  cout << gotown(count, townmap, route, 1) << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MAX 200010

using namespace std;

int gotown(long long int &count, int *townmap, int *route, int index) {
  vector<int> path;
  long long int towntime = 1;
  while (route[index] == 0) {
    path.push_back(index);
    route[index] = towntime;
    index = townmap[index];
    towntime++;
    count--;
  }
  return path[(count % (towntime - route[index])) + route[index] - 1];
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int townnum, townmap[MAX], route[MAX] = {0};
  long long int count;
  cin >> townnum >> count;
  for (int i = 1; i <= townnum; i++)
    cin >> townmap[i];
  cout << gotown(count, townmap, route, 1) << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long int N, K, i, j, x, ans = 1;
  cin >> N >> K;
  vector<int> A(N + 1), B(N + 1, -1);
  for (i = 1; i <= N; i++)
    cin >> A[i];
  for (i = 1; i <= K; i++) {
    B[ans] = i - 1;
    ans = A[ans];
    if (B[ans] != -1) {
      x = i - B[ans];
      break;
    }
    // for(j=1;j<=N;j++)if(B[j]>0||j==ans)B[j]++;
  }
  int a;
  if (x)
    a = (K - i) % x;
  // cout<<a<<","<<ans<<","<<(727202214173249351-2)%3<<","<<B[ans]<<","<<i<<endl;
  for (i = 0; i < a; i++) {
    ans = A[ans];
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

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

int main() {
  long int N, K, i, j, ans = 0;
  cin >> N >> K;
  vector<int> A(N), B(N);
  for (i = 0; i < N; i++)
    cin >> A[i];
  for (i = 0; i < K; i++) {
    ans = A[ans] - 1;
    B[ans]++;
    for (j = 0; j < N; j++)
      if (B[j] > 0 && j != ans)
        B[j]++;
    if (B[ans] > 1)
      break;
  }
  int a = (K - i - 1) % (B[ans] - 1);
  cout << a << "," << ans << "," << (727202214173249351 - 2) % 3 << ","
       << B[ans] << "," << i << endl;
  for (i = 0; i < a; i++) {
    ans = A[ans] - 1;
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int nm = 3e5;
int n, a, b[nm], s[nm], t, c, r;
string k;

int main() {
  cin >> n >> k;
  a = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i]--;
  }
  if (k.size() <= 6) {
    int kInt = stoi(k);
    for (int i = 0; i < kInt; i++)
      a = b[a];
    cout << a + 1 << endl;
    return 0;
  }
  fill(s, s + n, -1);
  c = a;
  for (int i = 0;; i++, c = b[c])
    if (s[c] < 0)
      s[c] = i;
    else {
      r = -s[c];
      t = i - s[c];
      break;
    }
  for (int i = 0, b = 1; i < (int)k.size(); i++, b = b * 10 % t)
    r += (k[k.size() - 1 - i] - '0') * b;
  r %= t;
  if (r < 0)
    r += t;
  for (int i = 0; i < r; i++)
    c = b[c];
  cout << c + 1 << endl;
}


Error: 0

Fixed submission:

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

const int nm = 1e5;
int n, a, b[nm], s[nm], t, c, r;
string k;

int main() {
  cin >> n >> k;
  a = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i]--;
  }
  if (k.size() <= 6) {
    int kInt = stoi(k);
    for (int i = 0; i < kInt; i++)
      a = b[a];
    cout << a + 1 << endl;
    return 0;
  }
  fill(s, s + n, -1);
  c = a;
  for (int i = 0;; i++, c = b[c])
    if (s[c] < 0)
      s[c] = i;
    else {
      r = -s[c];
      t = i - s[c];
      break;
    }
  for (int i = 0, b = 1; i < (int)k.size(); i++, b = b * 10 % t)
    r += (k[k.size() - 1 - i] - '0') * b;
  r %= t;
  if (r < 0)
    r += t;
  for (int i = 0; i < r; i++)
    c = b[c];
  cout << c + 1 << endl;
}