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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define inf 0x7f7f7f
#define p(x) (x) * (x)
#define PI 3.14159265358979
using namespace std;

typedef vector<vector<int>> vtype;
typedef long long ll;
ll *a = nullptr, *istrue = nullptr;
vector<ll> v1, v;

int main() {
  // ios::sync_with_stdio(false);
  ll n = 0, k = 0, loc = 1, num = 0;
  cin >> n >> k;
  a = new ll[n + 5]{0};
  istrue = new ll[n + 5]{0};
  for (ll i = 1; i <= n; ++i)
    cin >> a[i];
  while (!istrue[loc]) {
    istrue[loc] = 1;
    v1.push_back(loc);
    loc = a[loc];
    num++;
    if (k > 0)
      k--;
    else {
      cout << v1[v1.size() - 1] << endl;
      return 0;
    }
  }
  ll mid = 0;
  for (ll i = 0; i < v1.size(); ++i)
    if (v1[i] == loc) {
      mid = i;
      break;
    }
  for (ll i = mid; i < num; ++i)
    v.push_back(v1[i]);
  mid = num - mid;
  k %= mid;
  // cout << k << endl;
  cout << v[k] << endl;
  delete[] a;
  delete[] istrue;
  a = nullptr;
  istrue = nullptr;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define inf 0x7f7f7f
#define p(x) (x) * (x)
#define PI 3.14159265358979
using namespace std;

typedef vector<vector<int>> vtype;
typedef long long ll;
ll *a = nullptr, *istrue = nullptr;
vector<ll> v1, v;

int main() {
  // ios::sync_with_stdio(false);
  ll n = 0, k = 0, loc = 1, num = 0;
  cin >> n >> k;
  a = new ll[n + 5]{0};
  istrue = new ll[n + 5]{0};
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  while (!istrue[loc]) {
    istrue[loc] = 1;
    v1.push_back(loc);
    loc = a[loc];
    num++;
    k--;
  }
  ll mid = 0;
  for (int i = 0; i < v1.size(); ++i)
    if (v1[i] == loc) {
      mid = i;
      break;
    }
  for (int i = mid; i < num; ++i)
    v.push_back(v1[i]);
  mid = num - mid;
  k %= mid;
  // cout << k << endl;
  cout << v[k] << endl;
  delete[] a;
  delete[] istrue;
  a = nullptr;
  istrue = nullptr;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\\n"
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef pair<int, int> P;
typedef pair<string, int> Psi;

bool check[200010];
vector<int> A[200010];
vector<ll> B[200010];
ll n, k;

ll cnt = 0;
int start = 1;
void inite() {
  for (int i = 0; i < 200010; i++) {
    check[i] = false;
  }
}

void dfs(int s) {

  int next = A[s][0];

  if (!check[next]) {
    cnt++;
    check[next] = true;
    B[next].push_back(cnt);
    dfs(next);
  } else {
    cnt++;
    start = next;
    return;
  }
}
int calc(int s, int p) {

  if (p == k)
    return s;
  int next = A[s][0];

  int ans = calc(next, p + 1);

  return ans;
}

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

  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    A[i].push_back(tmp);
  }
  check[1] = true;
  B[1].push_back(0);

  dfs(1);
  /*	writeln(start);
          writeln(cnt);
          writeln(B[start][0]);
          writeln(k);*/

  if (k < B[start][0]) {
    writeln(calc(1, 0));
    return 0;
  }
  k -= max(0ll, B[start][0]);
  /*	 writeln(k);*/

  k %= (cnt - B[start][0]);
  /*	 writeln(k);*/

  writeln(calc(start, 0));
}


Error: 0

Fixed submission:

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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\\n"
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef pair<int, int> P;
typedef pair<string, int> Psi;

bool check[100010];
vector<int> A[100010];
vector<ll> B[100010];
ll n, k;

ll cnt = 0;
int start = 1;
void inite() {
  for (int i = 0; i < 100010; i++) {
    check[i] = false;
  }
}

void dfs(int s) {

  int next = A[s][0];

  if (!check[next]) {
    cnt++;
    check[next] = true;
    B[next].push_back(cnt);
    dfs(next);
  } else {
    cnt++;
    start = next;
    return;
  }
}
int calc(int s, int p) {

  if (p == k)
    return s;
  int next = A[s][0];

  int ans = calc(next, p + 1);

  return ans;
}

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

  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    A[i].push_back(tmp);
  }
  check[1] = true;
  B[1].push_back(0);

  dfs(1);
  /*	writeln(start);
          writeln(cnt);
          writeln(B[start][0]);*/
  k -= max(0ll, B[start][0] - 1);

  k %= cnt;

  /*	 writeln(k);*/

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

/*
 *       ~ AtCoder  Botay ~
 *        code by kazalika
 */

#include <bits/stdc++.h>

using namespace std;

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

  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  if (a[0] == 0) {
    cout << 1;
    return 0;
  }
  vector<long long> timer(n);
  long long nn = 0, t = 0;
  while (!timer[nn]) {
    timer[nn] = t;
    if (timer[nn] == k || a[nn] == nn) {
      cout << nn + 1;
      return 0;
    }
    nn = a[nn];
    t++;
  }
  k -= timer[nn];
  assert(k >= 0);
  if (!k || a[nn] == nn) {
    cout << nn + 1;
    return 0;
  }
  long long st = nn, cn = 1;
  nn = a[nn];
  vector<long long> v = {st};
  while (st != nn) {
    v.push_back(nn);
    nn = a[nn];
    cn++;
  }
  assert(v.size() == cn);
  cout << v[k % cn] + 1;
}

Error: TLE

Fixed submission:

/*
 *       ~ AtCoder  Botay ~
 *        code by kazalika
 */

#include <bits/stdc++.h>

using namespace std;

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

  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
  }
  if (a[0] == 0) {
    cout << 1;
    return 0;
  }
  vector<long long> timer(n);
  long long nn = 0, t = 0;
  while (timer[nn] < k || !timer[nn]) {
    timer[nn] = t;
    if (timer[nn] == k || a[nn] == nn) {
      cout << nn + 1;
      return 0;
    }
    nn = a[nn];
    t++;
  }
  k -= timer[nn];
  assert(k >= 0);
  if (!k || a[nn] == nn) {
    cout << nn + 1;
    return 0;
  }
  long long st = nn, cn = 1;
  nn = a[nn];
  vector<long long> v = {st};
  while (st != nn) {
    v.push_back(nn);
    nn = a[nn];
    cn++;
  }
  assert(v.size() == cn);
  cout << v[k % cn] + 1;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

ll n, k;
vector<ll> a;
map<ll, ll> mp;
ll l, r;
void dfs(int j, ll count) {
  if (mp[a[j]] != -1) {
    r = count;
    l = mp[a[j]];
    return;
  }
  mp[a[j]] = count;
  dfs(a[j], count + 1);
}

int main() {
  cin >> n >> k;
  a.resize(n + 1);
  REP(i, n) mp[i] = -1;
  REP(i, n) cin >> a[i];
  mp[1] = 0;
  dfs(1, 1);
  if (k <= l) {
    REP(i, n) {
      if (mp[i] == k) {
        cout << i << endl;
        return 0;
      }
    }
  } else {
    k -= l;
    ll temp = k % (r - l);
    temp += l;
    REP(i, n) {
      if (mp[i] == temp) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

ll n, k;
vector<ll> a(n + 1);
map<ll, ll> mp;
ll l, r;
void dfs(int j, int count) {
  if (mp[a[j]] != 0) {
    r = count;
    l = mp[a[j]];
    return;
  }
  mp[a[j]] = count;
  dfs(a[a[j]], count + 1);
}

int main() {
  cin >> n >> k;
  REP(i, n) cin >> a[i];
  dfs(1, 0);
  if (k <= l) {
    REP(i, n) {
      if (mp[i] == k) {
        cout << i << endl;
        return 0;
      }
    }
  } else {
    k -= l;
    ll temp = k % (r - l);
    REP(i, n) {
      if (mp[i] == l + temp) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int maxn = 2e5 + 10;

ll arr[maxn], vis[maxn];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int acout = 0, cur = 1;
  vector<int> v;
  while (1) {
    if (vis[cur])
      break;
    else {
      acout++, vis[cur] = 1;
      v.push_back(cur);
      cur = arr[cur];
    }
  }
  // for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
  // cout<<"debug "<<cur<<endl;
  int cal = 0, i = 0;
  if (v.size() > k) {
    cout << v[k] << endl;
    return 0;
  }
  while (1) {
    if (v[i] != cur)
      cal++, acout--, v.erase(v.begin());
    else
      break;
  }
  if (cal > 0)
    k -= cal;
  // for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
  // cout<<endl;
  if (acout == 0) {
    cout << v[v.size() - 1] << endl;
    return 0;
  }
  cout << v[k % acout] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int maxn = 1e5 + 10;

ll arr[maxn], vis[maxn];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int acout = 0, cur = 1;
  vector<int> v;
  while (1) {
    if (vis[cur])
      break;
    else {
      acout++, vis[cur] = 1;
      v.push_back(cur);
      cur = arr[cur];
    }
  }
  // for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
  // cout<<"debug "<<cur<<endl;
  int cal = 0, i = 0;
  if (v.size() > k) {
    cout << v[k] << endl;
    return 0;
  }
  while (1) {
    if (v[i] != cur)
      cal++, acout--, v.erase(v.begin());
    else
      break;
  }
  if (cal > 0)
    k -= cal;
  // for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
  // cout<<endl;
  if (acout == 0) {
    cout << v[v.size() - 1] << endl;
    return 0;
  }
  cout << v[k % acout] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define f first
#define s second
#define pb push_back
signed main() {
  int n, p;
  cin >> n >> p;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> v;
  set<int> s;
  int k = 1, r = -1, idx;
  while (1) {
    if (s.find(k) == s.end()) {
      v.pb(k);
    } else {
      r = k;
      break;
    }

    s.insert(k);
    k = a[k];
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == r) {
      idx = i;
      break;
    }
  }
  if (p < idx)
    cout << v[p] << endl;
  else {
    r = v.size() - idx;
    p -= idx;
    cout << v[idx + p % r] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define f first
#define s second
#define pb push_back
signed main() {
  int n, p;
  cin >> n >> p;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> v;
  v.pb(1);
  int k = 1;
  while (a[k] != 1) {
    v.pb(a[k]);
    k = a[k];
  }
  cout << v[p % v.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
}

int tel(vector<ll> &a, ll k, int temp) {
  if (k == 0) {
    return a[temp];
  }
  int ans = a[tel(a, k - 1, temp)];
  return ans;
}

int main(void) {
  init();
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  vector<ll> label(n + 1);
  rep(i, n) { cin >> a[i + 1]; }
  int temp = 1, m1 = 0;
  while (1) {
    if (label[temp] == 1)
      break;
    label[temp] = 1;
    int next = a[temp];
    temp = next;
    m1++;
  }
  int temp2 = 1, m2 = 0;
  while (1) {
    if (temp2 == temp)
      break;
    int next = a[temp2];
    temp2 = next;
    m2++;
  }

  if (k <= m2) {
    cout << tel(a, k - 1, 1) << endl;
  } else {
    k = m1 + (k - m2) % (m1 - m2);
    cout << tel(a, k - 1, 1) << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define INF 1e9

void init() {
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
}

int tel(vector<ll> &a, ll k) {
  if (k == 0) {
    return a[1];
  }
  int ans = a[tel(a, k - 1)];
  return ans;
}

int main(void) {
  init();
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) { cin >> a[i + 1]; }
  cout << tel(a, k - 1) << endl;
  ;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

long long n, k, now = 1, cnt, a[223456], pre[223456];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  while (k--) {
    now = a[now];
    if (pre[now])
      k %= cnt - pre[now];
    pre[now] = cnt++;
  }
  cout << now << endl;
}


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>;

const int INF = 1001001001;

int a[12][12];

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<int> c(n);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) cin >> a[i][j];
  }

  int ans = INF;
  rep(s, 1 << n) {
    int cost = 0;
    vector<int> d(m);
    rep(i, n) {
      if (s >> i & 1) {
        cost += c[i];
        rep(j, m) d[j] += a[i][j];
      }
    }
    bool ok = true;
    rep(j, m) if (d[j] < x) ok = false;
    if (ok)
      ans = min(ans, cost);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  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<int> p(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    a--;
    p.at(i) = a;
  }
  vector<int> Z(N, -1);
  vector<int> q(N);
  int b = 0;
  Z.at(0) = 0;
  int64_t c = 1;
  q.at(0) = 0;
  while (true) {
    if (Z.at(p.at(b)) == -1) {
      Z.at(p.at(b)) = c;
      q.at(c) = p.at(b);
      b = p.at(b);
      c++;
    } else {
      break;
    }
  }
  if (K < c) {
    cout << q.at(K) + 1 << endl;
  } else {
    cout << q.at((K - c) % (c - Z.at(p.at(b))) + Z.at(p.at(b))) + 1 << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    a--;
    p.at(i) = a;
  }
  vector<int> Z(N, -1);
  vector<int> q(N);
  int b = 0;
  Z.at(0) = 0;
  int c = 1;
  q.at(0) = 0;
  while (true) {
    if (Z.at(p.at(b)) == -1) {
      Z.at(p.at(b)) = c;
      q.at(c) = p.at(b);
      b = p.at(b);
      c++;
    } else {
      break;
    }
  }
  if (K < c) {
    cout << q.at(K) + 1 << endl;
  } else {
    cout << q.at((K - c) % (c + 1 - q.at(b)) + q.at(b)) + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18, MOD = 1e9 + 7;
const int INT_INF = 1e9, INT_MOD = 1e9 + 7;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef pair<int, int> Pi;
typedef pair<double, double> P;
signed main() {
  ll n, k, c = 0;
  cin >> n >> k;
  ll a[n], num[n + 5], ch[n + 5];
  rep(i, n) { cin >> a[i]; }
  rep(i, n + 5) {
    num[i] = 0;
    ch[i] = 0;
  }
  ll tmp = 1;
  ch[0] = 1;
  num[0] = 1;
  ll start = 0, finish = 0;
  for (ll j = 0; j < n; j++) {
    if (j == k) {
      cout << tmp << endl;
      return 0;
    }
    tmp = a[tmp - 1];
    num[j + 1] = tmp;
    if (ch[tmp - 1] == 1) {
      c = j + 1;
      break;
    } else {
      ch[tmp - 1] = 1;
    }
  }
  for (int p = 0;; p++) {
    if (num[p] == tmp) {
      start = p;
      break;
    }
  }
  finish = c;
  cout << num[start + (k - start) % (finish - start)] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18, MOD = 1e9 + 7;
const int INT_INF = 1e9, INT_MOD = 1e9 + 7;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef pair<int, int> Pi;
typedef pair<double, double> P;
signed main() {
  ll n, k;
  cin >> n >> k;
  ll a[n], num[n + 5], ch[n + 5];
  rep(i, n) { cin >> a[i]; }
  rep(i, n + 5) {
    num[i] = 0;
    ch[i] = 0;
  }
  ll tmp = 1;
  ch[0] = 1;
  num[0] = 1;
  ll start = 0, finish = 0;
  for (ll j = 0; j < n; j++) {
    if (j == k) {
      cout << tmp << endl;
      return 0;
    }
    tmp = a[tmp - 1];
    num[j + 1] = tmp;
    if (ch[tmp - 1] == 1) {
      for (int p = 0;; p++) {
        if (num[p] == tmp) {
          start = p;
          break;
        }
      }
      finish = j + 1;
    } else {
      ch[tmp - 1] = 1;
    }
  }
  cout << num[start + (k - start) % (finish - start)] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359;
#define inf (1 << 21);
#define mod 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<bool> J(N, false); // 町iを訪れたかどうか
  ll now = 1, next, cnt = 0;
  J.at(0) = true;
  while (true) {
    next = A.at(now - 1);
    now = next;
    cnt++;
    if (J.at(next - 1) == false) {
      J.at(next - 1) = true;
    } else {
      break;
    }
    if (cnt == K) {
      break;
    }
  }
  ll loop = 0;
  J.at(now - 1) = false;
  while (cnt < K) {
    next = A.at(now - 1);
    now = next;
    loop++;
    if (J.at(next - 1) == true) {
      J.at(next - 1) = false;
    } else {
      break;
    }
  }
  K -= cnt;
  ll n;
  if (loop != 0) {
    n = K % loop;
  } else {
    n = K;
  }
  for (int i = 0; i < n; i++) {
    next = A.at(now - 1);
    now = next;
  }
  cout << now << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define ld long double
#define pi 3.14159265359

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int cnt = 1;
  vector<bool> J(N);
  int now = A.at(0);
  int next = A.at(now - 1);
  J.at(0) = true;
  J.at(next - 1) = true;
  bool flag = false;
  int save;
  int n = 0;
  ll m;
  while (true) {
    if (flag = false) {
      now = next;
      next = A.at(now - 1);
      if (J.at(next - 1) == false) {
        cnt++;
        J.at(now - 1) = true;
      } else {
        cnt++;
        flag = true;
        save = next;
      }
    } else {
      now = next;
      next = A.at(now - 1);
      n++;
      if (next == save) {
        break;
      }
    }
  }
  m = K - cnt;
  ll x = m % n;
  for (int i = 0; i < x; i++) {
    now = next;
    next = A.at(now - 1);
  }
  cout << now << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

vi cities;
vi a;
vector<bool> visited;
bool blocked = false;

int dfs(int v) {
  // cout << v << endl;
  if (visited[v])
    return v;
  visited[v] = true;
  cities.push_back(v);
  return dfs(a[v]);
}

int main() {
  ll n;
  cin >> n;
  unsigned long long k;
  cin >> k;
  a = vi(n);
  visited = vector<bool>(n, false);
  rep(i, n) {
    int t;
    cin >> t;
    a[i] = t - 1;
  }
  int end = dfs(0);
  ll leng = cities.size();
  int pos = find(all(cities), end) - cities.begin();
  // rep(i, cities.size()) cout << cities[i] << " ";
  // cout << endl;
  // cout << "pos" <<pos << "end" << end <<endl;
  if (k < leng) {
    cout << cities[k] + 1 << endl;
  } else {
    int loop = leng - pos;
    k -= pos;
    k = k % loop;
    cout << cities[pos + k] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

vi cities;
vi a;
vector<bool> visited;
bool blocked = false;

int dfs(int v, int p = -1) {
  if (visited[v])
    return v;
  visited[v] = true;
  if (v == p) {
    blocked = true;
    return v;
  }
  if (p != -1 && v == 0)
    return 0;
  cities.push_back(v);
  dfs(a[v], v);
}

int main() {
  ll n, k;
  cin >> n >> k;
  a = vi(n);
  visited = vector<bool>(n, false);
  rep(i, n) {
    int t;
    cin >> t;
    a[i] = t - 1;
  }
  int end = dfs(0);
  ll leng = cities.size();
  if (blocked && k >= leng) {
    cout << cities[leng - 1] + 1 << endl;
    return 0;
  }
  if (end == 0) {
    k = k % leng;
    // rep(i, cities.size()) cout << cities[i] <<endl;
    cout << cities[k] + 1 << endl;
  } else {
    if (k < leng) {
      cout << cities[k] + 1 << endl;
    } else {
      int pos = cities.end() - find(all(cities), end);
      k -= (leng - pos);
      k = k % pos;
      cout << cities[k + (leng - pos)] + 1 << endl;
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<int> vis;
vector<int> a;
vector<int> cycle;
long long k;

int main() {
  int n;
  cin >> n >> k;
  vis.resize(n);
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int s = 0;
  while (!vis[s]) {
    vis[s] = 1;
    if (!k) {
      cout << s + 1 << endl;
      return 0;
    }
    s = a[s];
    k--;
  }
  int f = s;
  cycle.push_back(f);
  int d = a[s];
  while (d != f) {
    cycle.push_back(d);
    d = a[d];
  }
  int r = cycle.size();
  k += r;
  long long ans = k % r;
  cout << cycle[ans] + 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> vis;
vector<int> a;
vector<int> cycle;
long long k;
int f = -1;
void dfs(int u) {
  k--;
  if (vis[u]) {
    k++;
    f = u;
    int d = a[u];
    cycle.push_back(u);
    while (d != u) {
      cycle.push_back(d);
      d = a[d];
    }
    k += cycle.size();
    return;
  }
  vis[u] = 1;
  dfs(a[u]);
  return;
}

int main() {
  int n;

  cin >> n >> k;
  vis.resize(n);
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  dfs(0);
  long long ans;
  int r = cycle.size();
  ans = k % r;
  cout << cycle[ans] + 1 << endl;
  return 0;
}