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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> l(n);
  vector<long> lb(n, -1);
  vector<long> m;
  for (long i = 0; i < n; i++) {
    cin >> l[i];
  }
  long cnt = 0, next = 0;
  while (lb[next] == -1 && cnt <= k) {
    lb[next] = cnt;
    m.push_back(next + 1);
    next = l[next] - 1;
    cnt++;
  }

  m.erase(m.begin(), m.begin() + lb[next]);
  long remain = k - lb[next], hoge = remain % (cnt - lb[next]);
  cout << m[hoge] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n, k;
  cin >> n >> k;
  vector<long> l(n);
  vector<long> lb(n, -1);
  vector<long> m;
  for (long i = 0; i < n; i++) {
    cin >> l[i];
  }
  long cnt = 0, next = 0, loop = -1;
  while (lb[next] == -1) {
    lb[next] = cnt;
    m.push_back(next + 1);
    next = l[next] - 1;
    cnt++;
  }

  m.erase(m.begin(), m.begin() + lb[next]);
  long remain = k - lb[next], hoge = remain % (cnt - lb[next]);
  cout << m[hoge] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define endl "\\n"
#define sz(u) (int)(u.size())
#define L(x) (2 * x)
#define R(x) (2 * x + 1)
#define M(x, y) ((x + y) / 2)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  ull t[n + 1];
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++)
    cin >> t[i];
  ull pos = 1;
  vis[1] = 1;
  bool hh = false;
  while (k > 0) {
    k--;
    pos = t[pos];
    if (vis[pos]) {
      hh = true;
      break;
    }
    vis[pos] = 1;
  }
  if (hh) {
    memset(vis, 0, sizeof(vis));
    ull cnt = 1, temp_pos = pos;
    while (t[temp_pos] != pos) {
      cnt++;
      temp_pos = t[temp_pos];
    }
    k %= cnt;
    while (k > 0) {
      k--;
      pos = t[pos];
    }
    cout << pos;
  } else {
    cout << pos << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // int t;
  // cin>>t;
  // while(t--){
  solve();
  // cout<<endl;
  //}
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define endl "\\n"
#define sz(u) (int)(u.size())
#define L(x) (2 * x)
#define R(x) (2 * x + 1)
#define M(x, y) ((x + y) / 2)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  ll t[n + 1];
  bool vis[n + 1];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++)
    cin >> t[i];
  ll pos = 1;
  vis[1] = 1;
  int xx = 0;
  while (k > 0) {
    k--;
    pos = t[pos];
    if (vis[pos]) {
      break;
    }
    vis[pos] = 1;
    xx++;
  }
  memset(vis, 0, sizeof(vis));
  ll cnt = 1, temp_pos = pos;
  while (t[temp_pos] != pos) {
    cnt++;
    temp_pos = t[temp_pos];
  }
  k %= cnt;
  while (k > 0) {
    k--;
    pos = t[pos];
  }
  cout << pos;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // int t;
  // cin>>t;
  // while(t--){
  solve();
  // cout<<endl;
  //}
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

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

  vector<int> path;
  vector<int> checker(n, -1);
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    if (checker[cur] != -1) {
      break;
    }
    checker[cur] = i;
    path.push_back(cur);
    cur = vec[cur];
  }

  int l = checker[vec[*path.rbegin()]];
  int loop = (int)path.size() - l;

  if (k <= (int)path.size() - 1) {
    cout << path[k] + 1 << endl;
    return 0;
  }

  int target = (k - l) % loop;
  cout << path[l + target] + 1 << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

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

  vector<int> path;
  vector<int> checker(n, -1);
  int cur = 0;
  int loop = 0;
  int loopStart = 0;
  for (int i = 0; i <= n; ++i) {
    if (checker[cur] != -1) {
      loop = i - checker[cur];
      loopStart = (int)path.size() - loop;
      break;
    }
    checker[cur] = i;
    path.push_back(cur);
    cur = vec[cur];
  }

  if (loop == 0) {
    cout << (*path.rbegin()) + 1 << endl;
  } else {
    int target = (k - (int)path.size()) % loop;
    cout << path[loopStart + target] + 1 << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

// const int N = 10000, MAXI = 10000;

// int main() {
//     freopen("output.txt", "r", stdin);
//     freopen("input.txt", "w", stdout);
//     srand(time(nullptr));
//     int tc = 10;
//     cout << tc << "\\n";
//     while (tc--)
//     {
//         int n = rand()%N+1;
//         cout << n << " ";
//         cout << rand()%n + 1 << "\\n";
//         for(int i=1;i<=n;i++)
//             cout << rand()%MAXI + 1 << " ";
//         cout << "\\n";
//         cout << "~\\n";
//     }

//     return 0;
// }

//---------------------------------------------//

//  /* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 9)
#define endl '\\n'
#define MAXI (int)(3e17 + 10)
#define N 200005

int vis[N], a[N], last[N];
int n, k, ti = 0, ans;
vector<int> v;

void fun(int cur) {
  if (k == 0) {
    ans = cur;
    return;
  }
  if (vis[cur] == 1) {
    vector<int> x;
    for (int i = last[cur]; i < v.size(); i++)
      x.push_back(v[i]);

    int len = x.size();
    int pos = k % len;
    ans = x[pos];
    return;
  }
  v.push_back(cur);
  vis[cur] = 1;
  last[cur] = ti++;
  k--;
  fun(a[cur]);
}

// Driver code to test above functions
int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> k;

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

  fun(1);

  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

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

// const int N = 10000, MAXI = 10000;

// int main() {
//     freopen("output.txt", "r", stdin);
//     freopen("input.txt", "w", stdout);
//     srand(time(nullptr));
//     int tc = 10;
//     cout << tc << "\\n";
//     while (tc--)
//     {
//         int n = rand()%N+1;
//         cout << n << " ";
//         cout << rand()%n + 1 << "\\n";
//         for(int i=1;i<=n;i++)
//             cout << rand()%MAXI + 1 << " ";
//         cout << "\\n";
//         cout << "~\\n";
//     }

//     return 0;
// }

//---------------------------------------------//

//  /* while(!cin.eof()) */

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9 + 9)
#define endl '\\n'
#define MAXI (int)(3e17 + 10)
#define N 100005

int vis[N], a[N], last[N];
int n, k, ti = 0, ans;
vector<int> v;

void fun(int cur) {
  if (k == 0) {
    ans = cur;
    return;
  }
  if (vis[cur] == 1) {
    vector<int> x;
    for (int i = last[cur]; i < v.size(); i++)
      x.push_back(v[i]);

    int len = x.size();
    int pos = k % len;
    ans = x[pos];
    return;
  }
  v.push_back(cur);
  vis[cur] = 1;
  last[cur] = ti++;
  k--;
  fun(a[cur]);
}

// Driver code to test above functions
int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> k;

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

  fun(1);

  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  int A[200001];
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int temp = 1;
  if (K <= 200000) {
    for (int i = 1; i <= K; i++) {
      temp = A[temp];
      // cout<<i<<" "<<temp<<endl;;
    }
    cout << temp << endl;
    return 0;
  }
  map<int, int> mp;
  vector<int> v;
  int cnt = 1;
  int flag1 = -1;
  int flag2 = -1;
  while (1) {
    mp[temp]++;
    v.push_back(temp);
    if ((flag1 == -1) && (mp[temp] == 2)) {
      flag1 = cnt;
    }
    if (mp[temp] == 3) {
      flag2 = cnt;
      break;
    }
    temp = A[temp];
    cnt++;
  }
  // cnt-v.size()+1 繰り返しはじめまでのカウント
  // v.size()-1 繰り返し数

  cout << v[(K - flag1 - (flag2 - flag1)) % (flag2 - flag1) + flag1 -
            (flag2 - flag1)]
       << endl;

  /*cout<<cnt<<" "<<temp<<" "<<v.size()<<endl;
  cout<<flag1<<" "<<flag2<<endl;*/
  /*for(auto a: v){
    cout<<a<<" "<<endl;
    }*/

  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(200010, -1);
  vector<long long> B(200010, -1);
  map<int, int> mp;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  long long temp = A[1];
  long long cnt = 1;
  long long pnt1 = -1;
  long long pnt2;
  if (K < 200001) {
    while (1) {
      // cout<<A[temp];
      if (cnt == K) {
        cout << temp;
        return 0;
      }
      temp = A[temp];
      cnt++;
    }
  }

  while (1) {
    B[cnt] = temp;
    temp = A[temp];
    mp[temp]++;
    if ((pnt1 == -1) && (mp[temp] == 2)) {
      pnt1 = cnt;
    }
    if (mp[temp] == 3) {
      pnt2 = cnt;
    }
    if (mp[temp] == 4)
      break;
    cnt++;
  }
  cout << B[K % (pnt2 - pnt1) + pnt1] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

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

  ll n, k;
  cin >> n >> k;

  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<int> b(n, -1);
  int idx = 0;
  b[idx] = 0;
  rep(i, n) {
    idx = a[idx];
    b[idx] = 1;
    if (b[a[idx]] != -1) {
      idx = a[idx];
      break;
    }
  }

  int s = 0;
  int visited = 0;
  vector<int> d;
  int v = 0;
  while (visited <= n) {
    if (s == idx) {
      break;
    }
    d.push_back(s);
    s = a[s];
    visited++;
  }

  int t = idx;
  int sum = 1;
  vector<int> c;
  c.push_back(idx);
  while (sum <= n) {
    t = a[t];
    if (t == idx) {
      break;
    }
    c.push_back(t);
    sum++;
  }

  if (k >= visited) {
    k -= visited;
    k %= sum;
    cout << c[k] + 1 << endl;
  } else {
    cout << d[k] + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const int MOD = 1e9 + 7;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;

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

  ll n, k;
  cin >> n >> k;

  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  vector<int> b(n, -1);
  int idx;
  rep(i, n) {
    if (b[a[i]] != -1) {
      idx = a[i];
      break;
    }
    b[a[i]] = i;
  }

  int s = 0;
  int visited = 0;
  vector<int> d;
  while (true) {
    if (s == idx) {
      break;
    }
    d.push_back(s);
    s = a[s];
    visited++;
  }

  int t = idx;
  int sum = 1;
  vector<int> c;
  c.push_back(idx);
  while (true) {
    t = a[t];
    if (t == idx) {
      break;
    }
    c.push_back(t);
    sum++;
  }

  if (k >= visited) {
    k -= visited;
    k %= sum;
    cout << c[k] + 1 << endl;
  } else {
    cout << d[k] + 1 << endl;
  }

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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n, m) for (int i = n; i < m; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using ll = long long;

ll mod = 1e9;

const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int dp[200005][60];
  rep(i, n) {
    cin >> dp[i][0];
    dp[i][0]--;
  }
  for (int j = 1; j < 60; j++)
    rep(i, n) dp[i][j] = dp[dp[i][j - 1]][j - 1];
  int ans = 0;

  rep(i, 60) if (k & (1LL << i)) { ans = dp[ans][i]; }
  cout << ans + 1;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n, m) for (int i = n; i < m; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using ll = long long;

ll mod = 1e9;

const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int dp[20005][100];
  rep(i, n) {
    int a;
    cin >> a;
    dp[i][0] = a - 1;
  }
  for (int j = 1; j < 100; j++)
    rep(i, n) dp[i][j] = dp[dp[i][j - 1]][j - 1];
  int ans = 0;
  int lim = 0;
  ll f = k;
  while (f > 1) {
    f = (f + 1) / 2;
    lim++;
  }
  rep(i, lim) if ((k >> i) & 1) { ans = dp[ans][i]; }
  cout << ans + 1;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n), cnt(n, 0);
  rep(i, n) cin >> a[i];
  int i = 0, w = 0, r = 0, cnk = 0,
      str = 0; // iは行った町のNo、wはワープした回数、rはループに入るまで
  if (n < k) {
    while (cnt[i] < 2) { // 二回ループさせる
      cnt[i]++;
      if (cnt[i] == 2) {
        cnk++;
        w++;
        if (cnk == 1)
          str = i;
      }
      i = a[i] - 1;
      r++;
    }
    r = r - 2 * w;
    ll q = (ll)(k - r) % w;
    i = str;
    while (q > 0) {
      q--;
      i = a[i] - 1;
    }
  } else {
    while (k > 0) {
      k--;
      i = a[i] - 1;
    }
  }
  cout << i + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n), cnt(n, 0);
  rep(i, n) cin >> a[i];
  int i = 0, w = 0, r = 0, cnk = 0, str = 0,
      x = 0; // iは行った町のNo、wはワープした回数、rはループに入るまで
  while (cnt[i] < 2) { // 二回ループさせる
    cnt[i]++;
    if (cnt[i] == 2) {
      cnk++;
      w++;
      if (cnk == 1)
        str = i;
    }
    i = a[i] - 1;
    r++;
    if (r == k) {
      x = 1;
      break;
    }
  }
  r = r - 2 * w;
  ll q = (ll)(k - r) % w;
  i = str;
  if (q == 0 && x == 1)
    q = k;

  while (q > 0) {
    q--;
    i = a[i] - 1;
  }
  cout << i + 1 << endl;

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

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

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define ALL(v) ((v).begin(), (v).end())
#define COUT(x) cout << (x) << "\\n"

int a[200010];

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> vec(n);
  REP(i, n) cin >> vec[i];
  int count = 1, num = 0, start = vec[0];
  a[1]++;
  vector<int> test;
  test.push_back(vec[0]);
  while (num < 2) {
    start = vec[start - 1];
    a[start]++;
    num = a[start];
    test.push_back(start);
    count++;
  }
  int count1 = 0;
  set<int> set1;
  vector<int> vec1;
  while (1) {
    vec1.push_back(start);
    set1.insert(start);
    start = vec[start - 1];
    a[start]++;
    num = a[start];
    count1++;
    if (set1.size() != count1)
      break;
  }
  // COUT(count-set1.size());//サイクルが開始するまでの個数
  // COUT(set1.size());//サイクルの中の個数
  vec1.pop_back();
  if (k < count - set1.size()) {
    COUT(test[k - 1]);
  } else {
    k -= count - set1.size();
    k %= set1.size();
    int count12 = 0;
    for (int x : vec1) {
      if (count12 == k) {
        COUT(x);
      }
      count12++;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define ALL(v) ((v).begin(), (v).end())
#define COUT(x) cout << (x) << "\\n"

int a[200010];

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

  vector<int> vec(n);
  REP(i, n) cin >> vec[i];
  int count = 1, num = 0, start = vec[0];
  a[1]++;
  vector<int> test;
  test.push_back(vec[0]);
  while (num < 2) {
    start = vec[start - 1];
    a[start]++;
    num = a[start];
    test.push_back(start);
    count++;
  }
  int count1 = 0;
  set<int> set1;
  while (1) {
    set1.insert(start);
    start = vec[start - 1];
    a[start]++;
    num = a[start];
    count1++;
    if (set1.size() != count1)
      break;
  }
  // COUT(count-set1.size());//サイクルが開始するまでの個数
  // COUT(set1.size());//サイクルの中の個数
  k -= count - set1.size();
  k %= set1.size();
  if (count - set1.size() == 0) {
    int count12 = 0;
    for (int x : test) {
      if (count12 == k) {
        COUT(test[x]);
        return 0;
      }
      count12++;
    }
  }
  int num11 = 0;
  for (int x : set1) {
    if (num11 == k)
      COUT(vec[x]);
    num11++;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;

int a[maxn], vis[maxn];
int ans, pos;
vector<int> v;

void dfs(int u) {
  ans++;
  vis[u] = ans;
  v.push_back(u);
  if (!vis[a[u]])
    dfs(a[u]);
  else {
    pos = vis[a[u]];
    v.push_back(a[u]);
    return;
  }
}

int main() {
  int n;
  long long k;
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  if (k < v.size()) {
    cout << v[k] << "\\n";
  } else {
    k -= v.size();
    int len = v.size() - pos;
    cout << v[pos + k % len] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;

int a[maxn], vis[maxn];
int ans, pos;
vector<int> v;

void dfs(int u) {
  ans++;
  vis[u] = ans;
  v.push_back(u);
  if (!vis[a[u]])
    dfs(a[u]);
  else {
    pos = vis[a[u]];
    v.push_back(a[u]);
    return;
  }
}

int main() {
  int n;
  long long k;
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  if (k < v.size()) {
    cout << v[k] << "\\n";
  } else {
    k -= v.size();
    long long len = v.size() - pos;
    cout << v[pos + k % len] << "\\n";
  }
  return 0;
}
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 ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  rep(v, N) cin >> next[0][v], next[0][v]--;
  rep(d, 59) {
    rep(v, N) { next[d + 1][v] = next[d][next[d][v]]; }
  }
  int v = 0;
  for (int d = 0; d < 60; d++) {
    if (K & (1LL << d))
      v = next[d][v];
  }
  cout << v + 1 << endl;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<vector<int>> next(60, vector<int>(N));
  rep(v, 60) cin >> next[0][v], next[0][v]--;
  rep(d, 59) {
    rep(v, N) { next[d + 1][v] = next[d][next[d][v]]; }
  }
  int v = 0;
  for (int d = 0; d < 60; d++) {
    if (K & (1LL << d))
      v = next[d][v];
  }
  cout << v << endl;
}