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

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

#define mp make_pair
#define fi first
#define se second
#define pb push_back

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

const int N = 2e5 + 2;
ll n, k;
ll ar[N], pos[N];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  memset(pos, -1, sizeof(pos));
  vector<ll> v;
  ll ls, sz, cyc, idx;
  for (ll i = 1, j = 0; pos[i] == -1; i = ar[i], j++) {
    v.pb(i);
    pos[i] = j;
    ls = ar[i];
    sz = j + 1;
  }
  cyc = pos[ls];
  sz -= cyc;
  if (cyc == 0)
    idx = k % sz;
  else {
    idx = (k - cyc + sz) % sz;
    idx += cyc;
    if (k <= cyc)
      idx = k;
  }
  // cout << "k   : " << k%sz << '\\n';
  // cout << "cyc : " << cyc << '\\n';
  // cout << "sz  : " << sz << '\\n';
  // cout << "idx : " << idx << '\\n';
  // cout << "seq : ";
  // for (ll i : v)
  // {
  //     cout << i << ' ';
  // }
  // cout << '\\n';
  cout << v[idx] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

#define mp make_pair
#define fi first
#define se second
#define pb push_back

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

const int N = 2e5 + 2;
ll n, k;
ll ar[N], pos[N];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  memset(pos, -1, sizeof(pos));
  vector<ll> v;
  ll ls, sz, cyc, idx;
  for (ll i = 1, j = 0; pos[i] == -1; i = ar[i], j++) {
    v.pb(i);
    pos[i] = j;
    ls = ar[i];
    sz = j + 1;
  }
  cyc = pos[ls];
  sz -= cyc;
  if (cyc == 0)
    idx = k % sz;
  else {
    if (k < cyc)
      idx = k;
    idx = (k - 1 - cyc + sz) % sz;
    if (idx == 0 && k != 1)
      idx = sz;
  }
  // cout << k%sz << '\\n';
  // cout << cyc << ' ' << sz << ' ' << idx << '\\n';
  // for (ll i : v)
  // {
  //     cout << i << ' ';
  // }
  // cout << '\\n';
  cout << v[idx] << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long t;
  map<long long, long long> teleporters;
  for (long long i = 0; i < n; i++) {
    cin >> t;
    teleporters[i + 1] = t;
  }
  long long present_location = 1;
  bool cycle = false;
  long long cycle_length = 0;
  vector<long long> past_positions(n + 1);
  set<long long> appeared_factors;
  for (long long i = 0; i < k; i++) {
    present_location = teleporters[present_location];
    if (appeared_factors.find(present_location) != appeared_factors.end()) {
      cycle = true;
      cycle_length = i -
                     distance(past_positions.begin(),
                              find(past_positions.begin(), past_positions.end(),
                                   present_location)) +
                     1;
      break;
    } else {
      past_positions[i + 1] = present_location;
      appeared_factors.insert(present_location);
    }
  }
  if (cycle) {
    long long cycle_start_point = distance(
        past_positions.begin(),
        find(past_positions.begin(), past_positions.end(), present_location));
    long long remain = k - cycle_start_point;
    cout << past_positions[cycle_start_point + (remain % cycle_length)] << endl;

  } else {
    cout << present_location << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n, k;
  cin >> n >> k;
  long long t;
  map<long long, long long> teleporters;
  for (long long i = 0; i < n; i++) {
    cin >> t;
    teleporters[i + 1] = t;
  }
  long long present_location = 1;
  bool cycle = false;
  long long cycle_length = 0;
  vector<long long> past_positions(n + 1);
  past_positions[0] = 1;
  for (long long i = 0; i < k; i++) {
    present_location = teleporters[present_location];
    auto iter_point =
        find(past_positions.begin(), past_positions.end(), present_location);
    if (iter_point != past_positions.end()) {
      cycle = true;
      cycle_length = i - distance(past_positions.begin(), iter_point) + 1;
      break;
    } else {
      past_positions[i + 1] = present_location;
    }
  }
  if (cycle) {
    long long cycle_start_point = distance(
        past_positions.begin(),
        find(past_positions.begin(), past_positions.end(), present_location));
    long long remain = k - cycle_start_point;
    cout << past_positions[cycle_start_point + (remain % cycle_length)] << endl;

  } else {
    cout << present_location << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  long long int n = 0, cur = 1, l = 0, h = 0;
  unsigned long long int k = 0;
  cin >> n >> k;
  h = n + 5;
  vector<tuple<long long int, long long int, int>> g(h);
  for (int i = 1; i <= n; i++) {
    long long int pq = 0;
    cin >> pq;
    get<0>(g[i]) = pq;
    get<1>(g[i]) = 0;
    get<2>(g[i]) = 0;
  }

  for (unsigned long long int i = 1; i <= k; i++) {
    cur = get<0>(g[cur]);
    if (get<2>(g[cur]) < 2) {
      get<1>(g[cur]) = i;
      get<2>(g[cur])++;
    } else {
      l = i - get<1>(g[cur]);
      k = k - get<1>(g[cur]);
      k = k % l;
      i = 0;
    }
  }
  cout << cur << endl;
  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

int main(void) {
  long long int n = 0, cur = 1, l = 0;
  unsigned long long int k = 0;
  cin >> n >> k;
  vector<pair<long long int, long long int>> g(n);
  for (int i = 1; i <= n; i++) {
    long long int pq = 0;
    cin >> pq;
    g[i] = {pq, 0};
  }

  for (int i = 1; i <= k; i++) {
    cur = g[cur].first;
    if (g[cur].second == 0) {
      g[cur].second = i;
    } else {
      l = i - g[cur].second;
      k = k - g[cur].second;
      k = k % l;
      i = 0;
    }
  }
  cout << cur << 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)
using namespace std;
#define double long double
#define ll long long

int main(void) {
  ll n, k;
  vector<bool> b(2000000);
  ll ans = 1;
  ll count = 0;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (ll i = 0; i < 2000000; i++) {
    b[i] = false;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // 代入および初期化
  for (ll i = 0; i <= k; i++) {
    if (ans == a[ans]) {
      cout << ans << endl;
      return 0;
    }
    if (b[ans]) {
      break;
    } else {
      b[ans] = true;
      ans = a[ans];
      count++;
    }
    if (i == (k - 1)) {
      cout << ans << endl;
      return 0;
    }
  }
  // 一週目探す
  for (ll i = 0; i <= 2000000; i++) {
    b[i] = false;
  }
  // 初期化
  ll ans2 = k - count + 1;
  count = 0;
  for (ll i = 0; i < k; i++) {
    if (b[ans]) {
      break;
    } else {
      count++;
      b[ans] = true;
      ans = a[ans];
    }
  }
  ans2 = ans2 % count;
  if (ans2 == 1) {
    cout << ans << endl;
  } else {
    if (ans2 == 0)
      ans2 = count;
    for (ll i = 1; i < ans2; i++) {
      ans = a[ans];
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main(void) {
  ll n, k;
  vector<bool> b(2000000);
  ll ans = 1;
  ll count = 0;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 1; i <= 2000000; i++) {
    b[i] = false;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // 代入および初期化
  for (ll i = 0; i < k; i++) {
    if (i == (k - 1)) {
      cout << ans << endl;
      return 0;
    }
    if (b[ans]) {
      break;
    } else {
      b[ans] = true;
      ans = a[ans];
      count++;
    }
  }
  // 一週目探す
  for (ll i = 1; i <= 2000000; i++) {
    b[i] = false;
  }
  // 初期化
  ll ans2 = k - count + 1;
  count = 0;
  for (ll i = 0; i < k; i++) {
    if (b[ans]) {
      break;
    } else {
      count++;
      b[ans] = true;
      ans = a[ans];
    }
  }
  ans2 = ans2 % count;
  if (ans2 == 1) {
    cout << ans << endl;
  } else {
    if (ans2 == 0)
      ans2 = count;
    for (ll i = 1; i < ans2; i++) {
      ans = a[ans];
    }
    cout << ans << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int now = 0, cnt = 0;
  vector<long long int> map(n, -1);
  long long int i = 0;
  while (i < k) {
    // cout<<i<<" "<<now<<endl;
    map[now] = i;         // 足跡をつける
    now = a[now] - 1;     // 次に遷移
    if (map[now] != -1) { // すでに訪れてたら
      // cout<<k-i<<" "<<i-map[now]<<endl;
      i = k - ((k - (i + 1)) % ((i + 1) - map[now]));
      // cout<<"##!"<<i<<endl;
    } else {
      i += 1;
    }
  }
  cout << now + 1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int now = 0, cnt = 0;
  vector<long long int> map(n, -1);
  for (long long int i = 0; i < k; i++) {
    // cout<<i<<" "<<now<<endl;
    map[now] = i;
    now = a[now] - 1;
    if (map[now] != -1) { // すでに訪れてたら
      // cout<<k-i<<" "<<i-map[now]<<endl;
      i = k - ((k - i) % (i - map[now]));
      if (i == k)
        break;
      // cout<<"##!"<<i<<endl;
    }
  }
  cout << now + 1 << endl;

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;

int main() {
  ll n, a, pos = 0, index = 0;
  ll k;
  cin >> n >> k;
  set<int> cycle;
  vector<int> city;
  vi tere;
  rep(i, n) {
    cin >> a;
    city.push_back(--a);
  }
  pos = 0;
  for (int s : city) {
    cycle.insert(pos);
    tere.push_back(pos);
    pos = city[pos];
    if (cycle.count(pos))
      break;
  }
  for (int s : tere) {
    if (s == pos)
      break;
    else
      index++;
  }
  if (k <= index) {
    cout << tere[k] + 1 << endl;
  } else {
    k -= index;
    cout << tere[index + (k % (tere.size() - index))] + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;

int main() {
  ll n, k, a;
  cin >> n >> k;
  vector<int> city(n);
  vi cycle;
  cycle.push_back(1);
  rep(i, n) cin >> city[i];
  int pos = 1;
  while (1) {
    pos = city[pos - 1];
    if (pos == 1)
      break;
    cycle.push_back(pos);
  }
  cout << cycle[(k % cycle.size())] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// https://atcoder.jp/contests/abc167/tasks/abc167_d

#include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\\n'
#define int long long
#define all(x) (x).begin(), (x).end()

const int maxn = 200500;

// seen when we had x left
int A[maxn], seen[maxn];

void timeOut() {
  while (1) {
  }
}

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

  for (int i = 0; i < maxn; i++) {
    seen[i] = -1;
  }

  int N, K;
  cin >> N >> K;

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

  bool modded = false;
  int cur = 1;

  while (K > 0) {
    seen[cur] = K;
    int next = A[cur];

    if (seen[next] != -1 and !modded) {
      int cycleLen = seen[next] - seen[cur] + 1;
      K %= cycleLen;
      modded = true;
      continue;
    }

    cur = next;
    K--;
  }

  cout << cur << endl;
}


Error: 0

Fixed submission:

// https://atcoder.jp/contests/abc167/tasks/abc167_d

#include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\\n'
#define int long long
#define all(x) (x).begin(), (x).end()

const int maxn = 100500;

// seen when we had x left
int A[maxn], seen[maxn];

void timeOut() {
  while (1) {
  }
}

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

  for (int i = 0; i < maxn; i++) {
    seen[i] = -1;
  }

  int N, K;
  cin >> N >> K;

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

  bool modded = false;
  int cur = 1;

  while (K > 0) {
    seen[cur] = K;
    int next = A[cur];

    if (seen[next] != -1 and !modded) {
      int cycleLen = seen[next] - seen[cur] + 1;
      if (cycleLen <= 0)
        timeOut();
      K %= cycleLen;
      modded = true;
      continue;
    }

    cur = next;
    K--;
  }

  cout << cur << endl;
}
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<int64_t> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
    A.at(i)--; // 0～にする。
  }

  vector<int64_t> visit(n);
  int64_t next = 0;
  int64_t loop_length = 1;
  bool loop_flg = false;
  for (int i = 0; i < n; i++) {
    //        cout<<next<<" ";
    visit.at(next) = i + 1;
    next = A.at(next);

    if (visit.at(next) != 0) {
      loop_length = i + 1 - visit.at(next) + 1;
      loop_flg = true;
      break;
    }
  }

  int64_t index;
  if (k > n)
    index = (k - visit.at(next)) % loop_length;
  else {
    index = k - 1;
    next = 0;
  }
  //    cout<<"loop_length="<<loop_length<<endl;
  //    cout<<"index="<<index<<endl;
  for (int i = 0; i < index; i++) {
    next = A.at(next);
  }

  //    cout<<endl;
  cout << ++A.at(next) << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

  vector<int64_t> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  vector<int64_t> teleport(n, 0);
  map<int64_t, int64_t> visited;

  teleport.at(0) = a.at(0);
  visited[a.at(0)] = 0;
  int64_t teleport_repeat_size = n;
  int64_t teleport_repeat_start;
  bool flg = false;
  int64_t teleport_before_repeat;
  for (int i = 1; i < n; i++) {
    teleport.at(i) = a.at(teleport.at(i - 1) - 1);
    if (k == i) {
      flg = true;
      teleport_before_repeat = teleport.at(i);
      break;
    } else if (visited.count(teleport.at(i))) {
      teleport_repeat_size = i - visited.at(teleport.at(i));
      teleport_repeat_start = visited.at(teleport.at(i));
      break;
    } else {
      visited[teleport.at(i)] = i;
    }
  }

  if (flg)
    cout << teleport_before_repeat << endl;
  else
    cout << teleport.at(teleport_repeat_start +
                        (k - teleport_repeat_start) % teleport_repeat_size - 1)
         << endl;

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

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

int main() {

  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  int now = 1;
  long long rest = K;
  int result;
  int loop_start = -1;
  int dist = 1;
  bool flags[N + 1];
  for (int i = 0; i <= N; i++)
    flags[i] = false;

  while (1) {
    int next = A[now];

    if (rest-- == 0) {
      result = now;
      break;
    }

    else {
      if (loop_start < 0) { // ループを探索
        if (flags[next])
          loop_start = next;
        else
          flags[next] = true;
      }

      else { // １ループの長さを探索
        if (next == loop_start)
          rest %= dist; // ループできるだけループする
        else
          dist++;
      }
      now = next;
    }
  }

  cout << result;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  vector<int> rireki;
  int now = 1;
  long long rest = K;
  int result;
  rireki.push_back(now);
  while (1) {
    int next = A[now];
    auto it = find(rireki.begin(), rireki.end(), next);
    if (--rest == 0) {
      result = next;
      break;
    } else {
      if (it != rireki.end()) {
        int dist = distance(it, rireki.end());
        rest %= dist;
        result = *(it + rest);
        break;
      } else {
        now = next;
        rireki.push_back(now);
      }
    }
  }

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

#include <iostream>
using namespace std;

int main() {
  long long n, k, i, j, flag1, flag2, flag3;
  cin >> n >> k;
  long a[n + 1], b[n + 1], c[n + 1];
  flag1 = 0;
  flag2 = 0;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  b[0] = 1;
  b[1] = a[1];
  for (i = 2; i <= n; i++)
    b[i] = a[b[i - 1]];
  if (k > n) {
    c[1] = 1;
    for (i = 1; i <= n; i++) {
      if (c[b[i]] == 1) {
        for (j = 0; j < i; j++) {
          if (b[j] == b[i]) {
            flag1 = j;
            flag2 = i - j;
            break;
          }
        }
        break;
      } else
        c[b[i]] = 1;
    }
    flag3 = (k - flag1) % flag2 + flag1;
    cout << b[flag3] << endl;
  } else
    cout << b[k] << endl;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long n, k, i, j, flag1, flag2, flag3;
  cin >> n >> k;
  long a[n + 1], b[n + 1], c[n + 1];
  flag1 = 0;
  flag2 = 0;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  b[0] = 1;
  b[1] = a[1];
  for (i = 2; i <= n; i++)
    b[i] = a[b[i - 1]];

  if (k > n) {
    c[1] = 1;
    for (i = 1; i <= n; i++) {
      if (c[b[i]] == 1) {
        for (j = 0; j < i; j++) {
          if (b[j] == b[i]) {
            flag1 = j;
            flag2 = i - j;
            break;
          }
        }
        break;
      } else
        c[b[i]] = 1;
      flag3 = (k - flag1) % flag2 + flag1;
      cout << b[flag3] << endl;
    }
  } else
    cout << b[k] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> visited(n);
  visited[0] = 1;
  int _new = 0;
  long long bar = 1;
  while (true) {
    _new = a[_new];
    if (visited[_new]) {
      break;
    }
    visited[_new] = 1;
    bar++;
  }
  int cur = _new;
  long long cnt = 0;
  vector<int> path;
  path.push_back(cur);
  while (true) {
    _new = a[_new];
    cnt++;
    if (_new == cur)
      break;
    path.push_back(_new);
  }
  long long foo = bar - cnt;
  if (foo > k) {
    _new = 0;
    for (int i = 0; i < k; i++) {
      _new = a[_new];
    }
    cout << _new + 1 << '\\n';
    return 0;
  }
  k -= (bar - cnt);
  k %= cnt;

  cout << path[k] + 1 << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> visited(n);
  visited[0] = 1;
  int _new = 0;
  int bar = 1;
  while (true) {
    _new = a[_new];
    if (visited[_new]) {
      break;
    }
    visited[_new] = 1;
    bar++;
  }
  int cur = _new;
  long long cnt = 0;
  vector<int> path;
  path.push_back(cur);
  while (true) {
    _new = a[_new];
    cnt++;
    if (_new == cur)
      break;
    path.push_back(_new);
  }
  k -= (bar - cnt);
  k %= cnt;
  cout << path[k] + 1 << '\\n';
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int ll;
const int maxn = 2e5 + 10;
int a[maxn];
void solved() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  map<int, bool> mp;
  vector<int> cyc, sta;
  for (int i = 1;; i = a[i]) {
    if (!mp[a[i]]) {
      mp[a[i]] = true;
      sta.push_back(a[i]);
    } else {
      bool flag = false;
      for (int k = 0; k < sta.size(); k++) {
        if (a[i] == sta[k])
          flag = true;
        if (flag) {
          cyc.push_back(sta[k]);
        }
      }
      break;
    }
  }
  if (k <= sta.size())
    cout << sta[k - 1] << endl;
  else {
    k -= sta.size();
    k %= cyc.size();
    cout << cyc[(k - 1 + (int)cyc.size()) % (int)cyc.size()] << endl;
  }
}
int main() {
  solved();
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
const int maxn = 2e5 + 10;
int a[maxn];
void solved() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  map<int, bool> mp;
  vector<int> cyc, sta;
  for (int i = 1;; i = a[i]) {
    if (!mp[a[i]]) {
      mp[a[i]] = true;
      sta.push_back(i);
    } else {
      bool flag = false;
      for (int k = 0; k < sta.size(); k++) {
        if (a[i] == sta[k])
          flag = true;
        if (flag) {
          cyc.push_back(sta[k]);
        }
      }
      break;
    }
  }
  // for(int i = 0; i < sta.size(); i++)cout<<sta[i]<<" ";cout<<endl;
  // for(int i = 0; i < cyc.size(); i++)cout<<cyc[i]<<" ";cout<<endl;
  if (k <= sta.size())
    cout << sta[k - 1] << endl;
  else {
    k -= sta.size();
    k %= cyc.size();
    cout << cyc[(k - 1 + (int)cyc.size()) % (int)cyc.size()] << endl;
  }
}
int main() {
  solved();
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    --A[i];
  }
  ll town = 0;
  vector<bool> flg(N, false);
  queue<ll> q;
  q.push(town);
  bool roop = false;
  flg[0] = true;
  for (ll i = 0; i < K; i++) {
    // cerr <<"i=" <<i << endl;
    town = A[town];
    // cerr <<"town" << endl;
    if (flg[town] && !roop) {
      ll count = 0;
      while (!q.empty()) {
        if (q.front() != town) {
          count++;
          q.pop();
        } else {
          break;
        }
      }
      if (K - i - 1 >= i - count + 1) {
        // cerr <<"i-count" << i-count << endl;
        i += (((K - 1 - i) / (i - count + 1)) * (i - count + 1));
        // cerr <<"b" << endl;
        roop = true;
      }

    } else {
      flg[town] = true;
      q.push(town);
    }
  }

  cout << town + 1 << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    --A[i];
  }
  ll town = 0;
  vector<bool> flg(N, false);
  queue<ll> q;
  bool roop = false;
  flg[0] = true;
  for (ll i = 0; i < K; i++) {
    town = A[town];
    if (flg[town] && !roop) {
      ll count = 0;
      while (!q.empty()) {
        if (q.front() != town) {
          count++;
          q.pop();
        } else {
          break;
        }
      }
      if (K - i >= i - count) {
        i += (((K - i) / (i - count)) * (i - count));
        roop = true;
      }

    } else {
      flg[town] = true;
      q.push(town);
    }
  }

  cout << town + 1 << endl;
  return 0;
}