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

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

int main() {
  int N;
  long long K;
  vector<int> vecA;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    vecA.push_back(tmp - 1);
  }

  map<int, int> posCheck;
  posCheck[0] = 0;

  int nowPos = 0;
  int roop = 1;
  int roopStart;
  bool roopFind = false;
  for (long long i = 0; i < K; i++) {
    // cout << (nowPos+1) << endl;
    nowPos = vecA[nowPos];
    if (posCheck.find(nowPos) == posCheck.end()) {
      posCheck[nowPos] = i + 1;
    } else {
      roopStart = nowPos;
      roop = i + 1 - posCheck[nowPos];
      roopFind = true;
      break;
    }
  }

  if (roopFind) {
    long long y = (K - posCheck[nowPos]) % roop;

    // cout << (roopStart+1) <<":" << roop << endl;

    for (long long i = 0; i < y; i++) {
      nowPos = vecA[nowPos];
    }
  }

  cout << (nowPos + 1) << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  long long K;
  vector<int> vecA;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    vecA.push_back(tmp - 1);
  }

  map<int, int> posCheck;
  posCheck[0] = 0;

  int nowPos = 0;
  int roop;
  int roopStart;
  for (long long i = 0; i < K; i++) {
    // cout << (nowPos+1) << endl;
    nowPos = vecA[nowPos];
    if (posCheck.find(nowPos) == posCheck.end()) {
      posCheck[nowPos] = i + 1;
    } else {
      roopStart = nowPos;
      roop = i + 1 - posCheck[nowPos];
      break;
    }
  }

  long long y = (K - posCheck[nowPos]) % roop;

  // cout << (roopStart+1) <<":" << roop << endl;

  for (long long i = 0; i < y; i++) {
    nowPos = vecA[nowPos];
  }

  cout << (nowPos + 1) << endl;

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

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

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

  vector<int> b(0);
  vector<int> kanri(n, -1);
  int y = 0; // 現在地-1

  while (kanri.at(y) == -1) {
    kanri.at(y) = b.size();
    b.push_back(y + 1);
    y = a.at(y) - 1;
  }
  y = kanri.at(y); // yは例外の数

  if (k <= y)
    cout << b.at(k) << endl;
  else
    cout << b.at((k - y) % (b.size() - y) + y) << endl;
}

Error: WA

Fixed submission:

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

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

  vector<int> b(0);
  vector<int> kanri(n, -1);
  int y = 0; // 現在地-1

  while (kanri.at(y) == -1) {
    kanri.at(y) = b.size();
    b.push_back(y + 1);
    y = a.at(y) - 1;
  }
  y = kanri.at(y); // yは例外の数

  if (k < y)
    cout << b.at(k);
  else
    cout << b.at((k - y) % (b.size() - y - 1) + y) << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define SIZE_OF_ARRAY(array) sizeof(array) / sizeof(array[0])

int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 0;
  } else { // 発見できなかったとき
    return 1;
  }
}

int main() {
  int N;
  long long K;
  cin >> N >> K;
  std::vector<int> A;
  for (int i = 0; i < N; i++) {
    int l;
    cin >> l;
    A.push_back(l);
  }
  if (K >= N) {
    std::set<int> visited;
    std::vector<int> vlist;
    int visit = 1;
    while (!(visited.count(visit))) {
      visited.insert(visit);
      vlist.push_back(visit);
      visit = A.at(visit - 1);
    }

    auto itr = std::find(vlist.begin(), vlist.end(), visit);
    long long index = std::distance(vlist.begin(), itr);

    long long len = visited.size() - index;
    long long mod = ((K - index) % len);
    cout << vlist.at(mod + index);
  } else {
    int visit = 1;
    for (int i = 0; i < K; i++) {
      visit = A.at(visit - 1);
    }
    cout << visit;
  }
}

Error: TLE

Fixed submission:

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

#define SIZE_OF_ARRAY(array) sizeof(array) / sizeof(array[0])

int vector_finder(std::vector<int> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 0;
  } else { // 発見できなかったとき
    return 1;
  }
}

int main() {
  int N;
  long long K;
  cin >> N >> K;
  std::vector<int> A;
  for (int i = 0; i < N; i++) {
    int l;
    cin >> l;
    A.push_back(l);
  }
  if (K >= N) {
    std::vector<int> visited;
    int visit = 1;
    while (vector_finder(visited, visit)) {
      visited.push_back(visit);
      visit = A.at(visit - 1);
    }

    std::vector<int>::iterator itr;
    itr = std::find(visited.begin(), visited.end(), visit);
    long long index = std::distance(visited.begin(), itr);

    long long len = visited.size() - index;
    long long mod = ((K - index) % len);
    cout << visited.at(mod + index);
  } else {
    int visit = 1;
    for (int i = 0; i < K; i++) {
      visit = A.at(visit - 1);
    }
    cout << visit;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  long long int k;
  cin >> n >> k;
  int *route = (int *)malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    cin >> route[i];
    route[i]--;
  }

  int *h = (int *)malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++)
    h[i] = -1;

  int p = 0;
  int t = 0;
  while (true) {
    h[p] = t;
    p = route[p];
    t++;
    if (h[p] >= 0)
      break;
  }

  int sl = h[p];
  int l = t - h[p];
  if (k > sl) {
    int movet = (int)((k - sl) % l);
    for (int i = 0; i < movet; i++)
      p = route[p];
  } else {
    p = 0;
    for (int i = 0; i < k; i++)
      p = route[p];
  }

  cout << (p + 1);
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  long long A[N];
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  long long ans[N];
  ans[0] = 1;
  for (int i = 1; i <= N; i++) {
    ans[i] = A[ans[i - 1] - 1];
  }
  long long mod;
  for (long long i = 0; i < N; i++) {
    if (ans[N] == ans[N - i]) {
      mod = i;
      break;
    }
  }
  K = K % mod;
  cout << ans[K] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> to(n);
  rep(i, n) {
    cin >> to[i];
    to[i]--;
  }
  vector<bool> been(n, false); // n番目の街に行ったことがあるか
  been[0] = true;
  ll now = 0, step = 0;
  vector<ll> ever;
  ever.push_back(0);
  ll re = 0;
  while (true) {
    step++;
    now = to[now];
    if (!been[now]) {
      been[now] = true;
      ever.push_back(now);
    } else {
      rep(i, ever.size()) {
        if (ever[i] == now) {
          re = i;
          break;
        }
      }
      break;
    }
  }
  if (k < ever.size()) {
    dunk(ever[k] + 1);
  } else {
    ll a = (k - re) % (step - re) + re;
    dunk(ever[a] + 1);
  }
  return 0;
}

Error: WA

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> to(n);
  rep(i, n) {
    cin >> to[i];
    to[i]--;
  }
  vector<bool> been(n, false); // n番目の街に行ったことがあるか
  been[0] = true;
  ll now = 0, step = 0;
  vector<ll> ever;
  ever.push_back(0);
  ll re = 0;
  while (true) {
    step++;
    now = to[now];
    if (!been[now]) {
      been[now] = true;
      ever.push_back(now);
    } else {
      rep(i, ever.size()) {
        if (ever[i] == now) {
          re = i;
          break;
        }
      }
      break;
    }
  }
  if (k <= step) {
    dunk(ever[k] + 1);
  } else {
    ll a = (k - re - 1) % (step - re) + re;
    dunk(ever[a] + 1);
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <numeric>   // next_parmutation
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
using namespace std;

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c, l;
  {
    int v = 1;

    while (ord[v] == -1) {
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    c = s.size() - ord[v];
    l = ord[v];
  }

  if (k < l)
    cout << s.at(k) << endl;
  else {
    k -= l;
    k %= c;
    cout << s.at(k + l) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <numeric>   // next_parmutation
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
using namespace std;

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

  vector<vector<int>> vec(n, vector<int>(n, 0));
  vector<vector<int>> vec2(n, vector<int>(n, 0));

  int place = 0;
  ll cnt = 0;
  ll cnt2 = 0;
  for (ll i = 0; i < k; i++) {
    if (vec.at(place).at(a.at(place) - 1) == 0) {
      vec.at(place).at(a.at(place) - 1) = 1;
      place = a.at(place) - 1;
      cnt++;
    } else
      break;
  }

  int b = 0;
  while (b == 0) {
    if (vec2.at(place).at(a.at(place) - 1) == 0) {
      vec2.at(place).at(a.at(place) - 1) = 1;
      place = a.at(place) - 1;
      cnt2++;
    } else
      b = 1;
  }

  ll K = (k - cnt) % cnt2;
  for (ll i = 0; i < K; i++) {
    place = a.at(place) - 1;
  }

  cout << place + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, k, i, j, ans = 0;
  cin >> n >> k;
  vector<long> a(n), path;
  vector<bool> visited(n, false);
  for (i = 0; i < n; ++i)
    cin >> a[i];
  path.push_back(0);
  visited[0] = true;
  for (i = 1; i <= k; ++i) {
    ans = a[ans] - 1;
    if (visited[ans])
      break;
    path.push_back(ans);
    // cout<<ans<<' ';
    visited[ans] = true;
  }
  int l = path.size();
  for (j = 0; j < l; ++j)
    if (path[j] == ans)
      break;
  int l1 = l - j;
  k -= i;
  // cout<<'\\n'<<k<<'\\n';
  k %= l1;
  ans = path[j + k] + 1;
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, k, i, j, ans = 0;
  cin >> n >> k;
  vector<long> a(n);
  for (i = 0; i < n; ++i)
    cin >> a[i];
  for (i = 0; i < k; ++i) {
    ans = a[ans] - 1;
  }
  cout << ans + 1 << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int N;
ll K;
vector<bool> visited;
vector<int> dic;
vector<int> A;

int dfs(int cur) {
  K--;
  if (visited[cur])
    return cur;
  if (K == 0)
    return cur;
  visited[cur] = true;
  dic.push_back(cur);
  return dfs(A[cur]);
}

int main() {
  cin >> N >> K;
  A.resize(N);
  visited.resize(N);
  fill(visited.begin(), visited.end(), false);

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

  visited[0] = true;
  dic.push_back(0);

  int cur = dfs(A[0]);
  ll idx = 0;
  vector<int> last;

  int i;
  for (i = 0; i < (int)dic.size(); i++) {
    if (dic[i] == cur) {
      idx = i;
      last.push_back(dic[i]);
      break;
    }
  }

  for (int j = i + 1; j < (int)dic.size(); j++) {
    last.push_back(dic[j]);
  }

  idx = (ll)dic.size() - idx;

  if (K == 0)
    cout << (cur + 1) << endl;
  else
    cout << (last[K % idx] + 1) << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

int N;
ll K;
vector<bool> visited;
vector<int> dic;
vector<int> A;

int dfs(int cur) {
  K--;
  if (visited[cur])
    return cur;
  if (K == 0)
    return cur;
  visited[cur] = true;
  dic.push_back(cur);
  dfs(A[cur]);
}

int main() {
  cin >> N >> K;
  A.resize(N);
  visited.resize(N);
  fill(visited.begin(), visited.end(), false);

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

  visited[0] = true;
  dic.push_back(0);

  int cur = dfs(A[0]);
  ll idx = 0;
  vector<int> last;

  int i;
  for (i = 0; i < (int)dic.size(); i++) {
    if (dic[i] == cur) {
      idx = i;
      last.push_back(dic[i]);
      break;
    }
  }

  for (int j = i + 1; j < (int)dic.size(); j++) {
    last.push_back(dic[j]);
  }

  idx = (ll)dic.size() - idx;

  if (K == 0)
    cout << (cur + 1) << endl;
  else
    cout << (last[K % idx] + 1) << endl;

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

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  long long tmp2 = 0;
  vector<int> a(n + 1);
  vector<long long> t(n + 1, -1);
  // vector<int> A;
  int answer = 1;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i + 1);
  }
  // A.push_back(1);
  long long loop = 1;
  t.at(1) = 0;
  for (long long u = 0;; u++) {
    // A.push_back(a.at(A.at(u)));
    answer = a.at(answer);
    if (t.at(answer) != -1) {
      loop = u - t.at(answer) + 1;
      tmp2 = t.at(answer);
      break;
    }
    t.at(answer) = u + 1;
  }

  // for(long long t = 0;t<k;t++){
  //     A.push_back(a.at(A.at(t)-1));
  //     if(A.at(t+1) == 1){
  //         j = t;
  //         break;
  //     }
  // }

  long long tmp;
  if (k > tmp2 + loop) {
    tmp = tmp2 + (k - tmp2) % loop;
  } else {
    tmp = k;
  }
  answer = 1;
  for (long long i = 0; i < tmp; i++) {
    answer = a.at(answer);
  }
  cout << answer << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n;
  long long k;
  cin >> n >> k;
  long long tmp2 = 0;
  vector<int> a(n + 1);
  vector<long long> t(n + 1, -1);
  // vector<int> A;
  int answer = 1;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i + 1);
  }
  // A.push_back(1);
  long long loop = 0;
  t.at(1) = 0;
  for (long long u = 0; u < k; u++) {
    // A.push_back(a.at(A.at(u)));
    answer = a.at(answer);
    if (t.at(answer) != -1) {
      loop = u - t.at(answer) + 1;
      tmp2 = t.at(answer);
      break;
    }
    t.at(answer) = u + 1;
  }

  // for(long long t = 0;t<k;t++){
  //     A.push_back(a.at(A.at(t)-1));
  //     if(A.at(t+1) == 1){
  //         j = t;
  //         break;
  //     }
  // }

  long long tmp;
  if (k > tmp2 + loop) {
    tmp = tmp2 + (k - tmp2) % loop;
  } else {
    tmp = k;
  }
  answer = 1;
  for (long long i = 0; i < tmp; i++) {
    answer = a.at(answer);
  }
  cout << answer << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define ef(i, u) for (int i = b[u]; i; i = nxt[i])
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define gcd(a, b) __gcd(a, b)
#define MEM(a, x) memset(a, x, sizeof(a))
#define VI vector<int>
#define PII pair<int, int>
#define ull unsigned long long
#define ll long long
#define fi first
#define se second
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 5;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
int n, vis[N], a[N];
ll k;
VI Cir;
int main() {
  cin >> n >> k;
  rep(i, 1, n) read(a[i]);
  int now = 1, cnt = 0, pre = 0, len = 0;
  while (1) {
    if (vis[now]) {
      len = cnt - vis[now] + 1;
      pre = cnt - len;
      break;
    }
    Cir.pb(now);
    vis[now] = ++cnt;
    now = a[now];
  }
  // rep(i,0,cnt-1)cout<<Cir[i]<<' ';
  // cout<<'\\n';
  if (k <= pre) {
    cout << Cir[k] << '\\n';
    return 0;
  }
  k -= pre;
  cout << Cir[(int)(k % (ll)(len) + pre)] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define ef(i, u) for (int i = b[u]; i; i = nxt[i])
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define gcd(a, b) __gcd(a, b)
#define MEM(a, x) memset(a, x, sizeof(a))
#define VI vector<int>
#define PII pair<int, int>
#define ull unsigned long long
#define ll long long
#define fi first
#define se second
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5 + 5;
template <class T> inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - '0';
  res *= flag;
}
int n, vis[N], a[N];
ll k;
VI Cir;
int main() {
  cin >> n >> k;
  rep(i, 1, n) read(a[i]);
  int now = 1, cnt = 0, pre = 0, len = 0;
  while (1) {
    if (vis[now]) {
      len = cnt - vis[now] + 1;
      pre = cnt - len;
      break;
    }
    if (now != 1)
      Cir.pb(now);
    vis[now] = ++cnt;
    now = a[now];
  }
  // rep(i,0,cnt-1)cout<<Cir[i]<<' ';
  // cout<<'\\n';
  if (k <= pre) {
    cout << Cir[k - 1] << '\\n';
    return 0;
  }
  k -= pre;
  cout << Cir[(int)(k % (ll)(len) + pre - 1)] << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;

ll n, k, cur, num, l;
vector<ll> a(200005), visited(200005, -1);

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i], a[i]--;
  while (visited[cur] == -1 && num < k) {
    visited[cur] = num, cur = a[cur], num++;
  }
  k -= visited[cur], l = num - visited[cur];
  rep(i, k % l) cur = a[cur];
  cout << cur + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;

ll n, k, cur, num, l;
vector<ll> a(100005);

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i], a[i]--;
  vector<ll> visit(100005, -1);
  while (visit[cur] == -1) {
    visit[cur] = num, cur = a[cur], num++;
  }
  if (num <= k) {
    k -= visit[cur], l = num - visit[cur];
    rep(i, k % l) cur = a[cur];
  }
  cout << cur + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> ikisaki(N * 2 + 1);
  for (int i = 1; i <= N; i++) {
    cin >> ikisaki[i];
  }
  ikisaki[0] = 1;

  vector<int> visited(2 * N + 10, -1);
  vector<int> keiro(2 * N + 1);

  keiro[0] = 1;
  visited[1] = 0;

  int r = 1;
  int l = 1;
  if (K <= 2 * N) {
    for (int i = 1; i <= K; i++) {
      keiro[i] = ikisaki[keiro[i - 1]];
    }
    cout << keiro[K] << endl;
  } else {
    for (int i = 1; i <= 2 * N; i++) {
      keiro[i] = ikisaki[keiro[i - 1]];
      if (visited[keiro[i]] == -1) {
        visited[keiro[i]] = i;
      } else {
        r = i - visited[keiro[i]];
        l = visited[keiro[i]];
        // cout << i << " " << r  << " " << l;
        break;
      }
    }
    cout << keiro[(K - l) % r + l] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  long long int K;
  cin >> N >> K;
  vector<int> vec(N + 10);
  for (int i = 1; i <= N; i++) {
    cin >> vec[i];
  }
  vector<int> ikisaki(N + 100);
  ikisaki[0] = 1;

  for (int i = 1; i <= 20; i++) {
    ikisaki[i] = vec[ikisaki[i - 1]];
  }

  long long int roop;
  long long int hazime;
  bool decided = false;
  for (int i = 0; i < 7; i++) {
    for (int j = 1; j <= 6; j++) {
      if (ikisaki[i] == ikisaki[i + j]) {
        roop = j;
        hazime = i;
        decided = true;
        break;
      }
    }
    if (decided) {
      break;
    }
  }
  // cout << roop << " " << hazime << endl;
  long long int amari = (K - hazime) % roop;
  cout << ikisaki[hazime + amari] << endl;

  /*
  int issyuu = 1;
  if(K >= N){
      for(int i=1; i<N+10; i++){
          ikisaki[i] = vec[ikisaki[i-1]];
          if(ikisaki[i]==1){
              issyuu = i;
              break;
          }
      }
      long long int amari = K % issyuu;
      cout << ikisaki[amari] << endl;
  }

  else{
      for(long long int i=1; i<=K; i++){
          ikisaki[i] = vec[ikisaki[i-1]];
      }
      cout << ikisaki[K] << endl;
  }
  */
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  set<int> visited;
  vector<int> path;
  int now = 0;
  while (visited.count(now) == 0) {
    path.push_back(now);
    visited.insert(now);
    now = a[now];
  }

  int s = find(path.begin(), path.end(), now) - path.begin();
  int loop = (int)path.size() - s;
  if (k < s) {
    cout << path[k] + 1 << endl;
  } else {
    cout << path[(k - s) % loop + s] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  ll loop = 0;
  for (int i = 0; i < N; ++i) {
    if (a[i] == i + 1) {
      loop++;
    }
  }
  int count = K % (N - loop);
  int now = 1;
  while (count > 0) {
    now = a[now - 1];
    count--;
  }

  cout << now << endl;
  return 0;
}