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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;
using Graph = vector<vector<ll>>;

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

  vector<ll> visit(n, -1);
  visit[0] = 0;
  ll now = 0;
  ll cnt = 0;
  ll loop = 0;
  while (cnt <= k) {
    cnt++;
    now = A[now];
    if (visit[now] != -1) {
      loop = cnt - visit[now];
      break;
    } else {
      visit[now] = cnt;
    }
  }

  if (loop) {
    ll nk = (k - visit[now]) % loop;
    rep(i, nk) now = A[now];
  } else {
    now = 0;
    rep(i, k) now = A[now];
  }

  cout << now + 1 << endl;
}

Error: 0

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> A[i]; // インデックスを合わせる

  ll now = 1, loop = 0, p;
  map<ll, ll> kita;
  for (ll i = 0; i < k; i++) {
    if (kita[now]) {
      p = now;
      loop = i - kita[p];
      break;
    }
    kita[now] = i;
    now = A[now];
  }
  if (kita.size() == 0)
    cout << now << endl;
  else {
    ll re = (k - kita[p]) % loop;
    now = p;
    rep(i, re) now = A[now];
    cout << now << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  long long K;
  vector<int> A;
  int temp;

  cin >> N >> K;

  // N = 4;
  // K = 5;
  // int a[] = {3,2,4,1};

  for (int i = 0; i < N; i++) {
    cin >> temp;
    A.push_back(temp);
    // A.push_back(a[i]);
  }

  int point = 0;
  /*
      while(K > 0){
          point = A.at(point) - 1;
          K --;
      }
      */

  vector<int> r(N, -1);
  r.at(0) = 0;
  int k = 0;
  int check = 0;
  while (K > k) {
    k++;
    point = A.at(point) - 1;
    if (r.at(point) != -1) {
      check = 1;
      break;
    }
    r.at(point) = k;
  }

  int loop;

  if (check == 1) {
    loop = k - r.at(point);
    temp = (K - r.at(point)) % loop;
    while (temp > 0) {
      point = A.at(point) - 1;
      temp--;
    }
    cout << point + 1 << endl;
  } else {
    cout << point + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  long long K;
  vector<int> A;
  int temp;

  cin >> N >> K;

  // N = 4;
  // K = 5;
  // int a[] = {3,2,4,1};

  for (int i = 0; i < N; i++) {
    cin >> temp;
    A.push_back(temp);
    // A.push_back(a[i]);
  }

  int point = 0;

  while (K > 0) {
    point = A.at(point) - 1;
    K--;
  }
  cout << point + 1 << endl;

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

#include <stdio.h>

using namespace std;
typedef long long llong;

#define MAX 200001

int main() {
  int N;
  int A[MAX], B[MAX];
  llong K;
  scanf("%d", &N);
  scanf("%lld", &K);

  // 配列の初期化
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    B[i] = 0;
  }

  // サイクルのスタートを見つける
  // Biがすでに訪れられていたらそれがサイクルのスタート
  llong startIndex = 0;
  llong i = 1;
  while (1) {
    if (B[i - 1]) {
      startIndex = i;
      break;
    }
    B[i - 1] = 1;
    i = A[i - 1];
  }

  // startにいたるまでの手数を見つける
  llong I = 1;
  llong startCnt = 0;
  while (startIndex != I) {
    I = A[I - 1];
    startCnt++;
  }

  // サイクルの長さを見つける
  // サイクルの始まりからスタートして戻るまでの回数
  llong cnt = 1;
  while (1) {
    i = A[i - 1];
    if (startIndex == i) {
      i = startIndex;
      break;
    }
    cnt++;
  }
  if (startCnt > K) {
    i = 1;
    for (int j = 0; j < K; j++) {
      i = A[i - 1];
    }
    printf("%d\\n", i);
    return 0;
  }

  K -= startCnt;
  int mod = K % cnt;

  for (int j = 0; j < mod; j++) {
    i = A[i - 1];
  }

  printf("%d", i);

  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>

using namespace std;
typedef long long llong;

#define MAX 100010

int main() {
  int N;
  int A[MAX], B[MAX];
  llong K;
  scanf("%d", &N);
  scanf("%lld", &K);

  // 　配列の初期化
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    B[i] = 0;
  }

  // サイクルのスタートを見つける
  // Biがすでに訪れられていたらそれがサイクルのスタート
  llong startIndex = 0;
  llong i = 1;
  while (1) {
    if (B[i - 1]) {
      startIndex = i;
      break;
    }
    B[i - 1] = 1;
    i = A[i - 1];
  }

  // サイクルの長さを見つける
  // サイクルの始まりからスタートして戻るまでの回数
  llong cnt = 1;
  while (1) {
    i = A[i - 1];
    if (startIndex == i) {
      i = startIndex;
      break;
    }
    cnt++;
  }

  // startIndexに到達するまでの回数分Kから引く
  int I = 1;
  while (I != startIndex) {
    I = A[I - 1];
    K--;
  }

  int mod = K % cnt;

  for (int j = 0; j < mod; j++) {
    i = A[i - 1];
  }

  printf("%d", i);

  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> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i], a[i]--;
  }
  vector<int> path;
  vector<int> visited(n);

  int aa = 0;
  for (; k > 0; k--) {
    if (visited[aa])
      break;
    visited[aa] = 1;
    path.push_back(aa);
    aa = a[aa];
  }

  if (k == 0) {
    cout << aa + 1 << endl;
    return 0;
  }

  int idx = find(begin(path), end(path), aa) - begin(path);
  k %= path.size() - idx;
  cout << path[idx + k] + 1 << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int64_t n, k;
  cin >> n >> k;
  vector<int> a(n);
  map<int, int> tele;
  int fst = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i], a[i]--;
    if (fst == -1) {
      tele[a[i]]++;
      if (tele[a[i]] == 2) {
        fst = a[i];
      }
    }
  }
  int ans;
  if (fst != -1) {
    int next = fst;
    int loop = 0;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt++;
      next = a[next];
      if (next == fst) {
        loop = cnt;
        break;
      }
    }
    ans = fst;
    if (k > tele.size()) {
      k -= tele.size();
      k--;
      k %= loop;
    } else {
      ans = 0;
    }
  } else {
    ans = 0;
    k--;
    k %= n;
  }
  for (; k >= 0; k--) {
    ans = a[ans];
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<pair<int, pair<int, int>>> Vlll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n << " "
const int Nx = 1e5 + 2;
const int inf = (int)(1e9 + 10LL);
void solve() {
  int n, k;
  sl(n);
  sl(k);
  Vl arr(n + 1);
  for (int i = 1; i <= n; i++)
    sl(arr[i]);
  unordered_map<int, int> freq;
  int i = 1;
  int pivot, start = 1;
  freq[1] = 0;
  int value;
  for (i = 1; i <= k; i++) {
    start = arr[start];
    if (freq[start] > 0) {
      pivot = i - freq[start];
      value = start;
      break;
    }
    freq[start] = i;
    // cout<<start<<" ";
  }
  if (i >= k) {
    cout << start << endl;
    return;
  }

  k = (k - freq[value]) % pivot;
  start = value;
  for (int i = 1; i <= k; i++) {
    start = arr[start];
  }
  cout << start << endl;
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
typedef vector<pair<int, pair<int, int>>> Vlll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) cin >> n
#define pl(n) cout << n << " "
const int Nx = 1e5 + 2;
const int inf = (int)(1e9 + 10LL);
void solve() {
  int n, k;
  sl(n);
  sl(k);
  Vl arr(n + 1);
  for (int i = 1; i <= n; i++)
    sl(arr[i]);
  unordered_map<int, int> freq;
  int i = 1;
  int pivot, start = 1;
  freq[1] = 0;
  int value;
  for (i = 1; i <= k; i++) {
    start = arr[start];
    if (freq[start] > 0) {
      pivot = i - freq[start];
      value = start;
      break;
    }
    freq[start] = i;
    // cout<<start<<" ";
  }
  k = (k - freq[value]) % pivot;
  start = value;
  for (int i = 1; i <= k; i++) {
    start = arr[start];
  }
  cout << start << endl;
}

signed main() {
  int test = 1;
  // sl(test);
  while (test--) {
    solve();
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define fastIO                                                                 \\
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),              \\
      cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 2e5 + 9;
ll adj[N], cycle, vis[N], req, ans;
ll k;

void dfs(ll node) {
  vis[node] = 1;
  if (!vis[adj[node]]) {
    ++cycle;
    if (cycle == k) {
      ans = adj[node];
    }
    dfs(adj[node]);
  } else
    req = adj[node];
}

int main() {
  fastIO;
#ifdef LOCAL
  freopen("input.in", "rt", stdin);
#endif
  ll n, a;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    adj[i] = a;
  }
  req = 1;
  dfs(1);
  if (k <= cycle) {
    cout << ans << '\\n';
    return 0;
  }
  k -= cycle + 1;
  memset(vis, 0, sizeof vis);
  cycle = 0;
  dfs(req);
  ++cycle;
  k %= cycle;
  ans = req;
  memset(vis, 0, sizeof vis);
  cycle = 0;
  dfs(req);
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fastIO                                                                 \\
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),              \\
      cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 1e5 + 9;
int adj[N], cycle, vis[N], req, ans;
ll k;

void dfs(int node) {
  vis[node] = 1;
  if (!vis[adj[node]]) {
    ++cycle;
    if (cycle == k) {
      ans = adj[node];
    }
    dfs(adj[node]);
  } else
    req = adj[node];
}

int main() {
  fastIO;
#ifdef LOCAL
  freopen("input.in", "rt", stdin);
#endif
  int n, a;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    adj[i] = a;
  }
  req = 1;
  dfs(1);
  if (k <= cycle) {
    cout << ans << '\\n';
    return 0;
  }
  k -= cycle + 1;
  memset(vis, 0, sizeof vis);
  cycle = 0;
  dfs(req);
  ++cycle;
  k %= cycle;
  ans = req;
  memset(vis, 0, sizeof vis);
  cycle = 0;
  dfs(req);
  cout << ans;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> s;
  vector<int> ord(n + 1, -1);
  int c = 1, l = 0;
  {
    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[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int i = 0, m = 0;
  vector<int> vec;
  vec.push_back(0);
  bool sp = true;
  while (k > 0) {
    i = a[i] - 1;
    k--;
    m++;
    // if(sp){
    for (int j = 0; j < vec.size(); j++) {
      if (i == vec[j]) {
        k %= (m - j);
        sp = false;
      }
    }
    // }
    vec.push_back(i);
  }
  cout << i + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * D - Teleporter
 * https://atcoder.jp/contests/abc167/tasks/abc167_d
 */
#include <bits/stdc++.h>
using namespace std;

int main() {
  // 高橋王は正の整数 K が好きです。わがままな高橋王は、町 1
  // から出発してテレポーターをちょうど K
  // 回使うと、どの町に到着するかが知りたいです。
  // 高橋王のために、これを求めるプログラムを作成してください。
  long long N, K;
  cin >> N >> K;

  vector<long long> A(N);
  // vector<long long>HND(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    // HND.at( A.at(i) )++;
  }

  // 最多出現の値を求める
  /*
  long long maxA = 0;
  long long maxHND = 0;
  for (int i = 0; i < N + 1; i++) {
      if (HND.at(i) > maxHND) {
          maxHND = HND.at(i);
          maxA = i;
      }
  }
  */

  long long place = 1;
  vector<long long> HND(N + 1, 0);
  long long kaisuu = 0;
  for (long long i = 1; i <= K; i++) {
    place = A.at(place - 1);
    kaisuu++;
    if (HND.at(place) == 0) {
      HND.at(place) = kaisuu;
    } else if (HND.at(place) > 0) {
      long long loopcnt = kaisuu - HND.at(place);
      i += ((K - i) / loopcnt) * loopcnt;
      // cout << "koko";
    }
  }
  cout << place << endl;

  /*
    long long place = 1;
    long long kaisuu = 0;
    long long maxAkai = 0;
    long long syokai = 0;
    for (long long i = 0; i < K; i++) {
        place = A.at(place - 1);
        if (place == maxA) {
            maxAkai++;
        }

        if (maxAkai == 0) {
            syokai++;
        } else if (maxAkai == 1) {
            kaisuu++;
        } else if (maxAkai == 2) {
            i = syokai + ((K - syokai) / kaisuu) * kaisuu;
            maxAkai++;
        }
    }
    cout << place << endl;
    */
  // cout << score << endl;
}


Error: TLE

Fixed submission:

/**
 * D - Teleporter
 * https://atcoder.jp/contests/abc167/tasks/abc167_d
 */
#include <bits/stdc++.h>
using namespace std;

int main() {
  // 高橋王は正の整数 K が好きです。わがままな高橋王は、町 1
  // から出発してテレポーターをちょうど K
  // 回使うと、どの町に到着するかが知りたいです。
  // 高橋王のために、これを求めるプログラムを作成してください。
  long long N, K;
  cin >> N >> K;

  vector<long long> A(N);
  vector<long long> HND(N + 1, 0);
  // long long minA = INT_MAX;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    HND.at(A.at(i))++;
    // if (A.at(i) < minA) {
    //     minA = A.at(i);
    // }
  }

  // 最多出現の値を求める
  long long maxA = 0;
  long long maxHND = 0;
  for (int i = 0; i < N + 1; i++) {
    if (HND.at(i) > maxHND) {
      maxHND = HND.at(i);
      maxA = i;
    }
  }

  long long place = 1;
  long long kaisuu = 0;
  long long maxAkai = 0;
  long long syokai = 0;
  for (long long i = 0; i < K; i++) {
    place = A.at(place - 1);
    if (place == maxA) {
      maxAkai++;
    }

    if (maxAkai == 0) {
      syokai++;
    } else if (maxAkai == 1) {
      kaisuu++;
    } else if (maxAkai == 2) {
      i = syokai + ((K - syokai) / kaisuu) * kaisuu;
      maxAkai++;
    }
  }
  cout << place << endl;
  // cout << score << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mod 1000000007

int main() {
  FIO;
  // ifstream cin("input");
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  ll i;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<ll> cyclePath;
  ll hashIt[200001];
  for (i = 0; i < 200001; i++) {
    hashIt[i] = 0;
  }
  hashIt[1]++;
  i = 1;
  while (1) {
    hashIt[a[i]]++;
    if (hashIt[a[i]] == 2) {
      cyclePath.push_back(a[i]);
    }
    if (hashIt[a[i]] == 3) {
      break;
    }
    i = a[i];
  }
  int destination = cyclePath[0];
  int ans = 0;
  i = 1;
  vector<int> linearPath;
  while (1) {
    if (destination == i) {
      break;
    }
    linearPath.push_back(i);
    i = a[i];
    ans++;
  }
  int temp1 = k;
  k -= ans;
  if (k < 0) {
    cout << linearPath[temp1] << endl;
    return 0;
  }
  int cycleSize = cyclePath.size();
  int temp = k % cycleSize;
  cout << cyclePath[temp] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mod 1000000007

int main() {
  FIO;
  // ifstream cin("input");
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  ll i;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<ll> cyclePath;
  ll hashIt[200001];
  for (i = 0; i < 200001; i++) {
    hashIt[i] = 0;
  }
  hashIt[1]++;
  i = 1;
  while (1) {
    hashIt[a[i]]++;
    if (hashIt[a[i]] == 2) {
      cyclePath.push_back(a[i]);
    }
    if (hashIt[a[i]] == 3) {
      break;
    }
    i = a[i];
  }
  int destination = cyclePath[0];
  int ans = 0;
  i = 1;
  while (1) {
    if (destination == i) {
      break;
    }
    i = a[i];
    ans++;
  }
  k -= ans;
  int cycleSize = cyclePath.size();
  int temp = k % cycleSize;
  cout << cyclePath[temp] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

#define MAX 1000000005
#define INF INT_MAX

typedef long long ll;
typedef pair<int, int> P;

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

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

  vector<bool> touch(n, false);
  int now, strt;

  now = 0;
  while (!touch[now]) {
    touch[now] = true;
    now = a[now];
  }
  strt = now;

  int c1 = 0, c2 = 0;

  now = 0;
  while (now != strt) {
    now = a[now];
    c1++;
  }
  while (a[now] != strt) {
    now = a[now];
    c2++;
  }
  c2++;

  if (k > c1)
    k = (k - c1) % c2 + c1;
  now = 0;
  rep(i, k) now = a[now];

  cout << now + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

#define MAX 1000000005
#define INF INT_MAX

typedef long long ll;
typedef pair<int, int> P;

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

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

  vector<bool> touch(n, false);
  int now = 0;
  int strt;
  while (!touch[now]) {
    touch[now] = true;
    now = a[now];
    if (touch[now]) {
      strt = now;
      break;
    }
  }
  int c1 = 0;
  now = 0;
  while (now != strt) {
    now = a[now];
    c1++;
  }
  int c2 = 0;
  now = strt;
  if (strt != a[now]) {
    now = a[now];
    c2++;
  }
  while (now != strt) {
    now = a[now];
    c2++;
  }

  k = (k - c1) % c2 + c1;

  now = 0;
  rep(i, k) now = a[now];

  cout << now + 1 << endl;

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

#define INF 100000000;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
#define eps 1.0e-14;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int A[200006];
  rep(i, n) {
    cin >> A[i];
    A[i]--;
  }
  int journey[500006];
  journey[0] = 0;
  for (int i = 1; i < n * 2; i++) {
    journey[i] = A[journey[i - 1]];
  }
  if (k < n) {
    cout << journey[k] + 1 << endl;
    return 0;
  }
  int loop_start = n - 2;
  int loop_end = n - 1;
  while (journey[loop_start] != journey[loop_end]) {
    loop_start--;
  }
  int period = loop_end - loop_start;
  k %= period;
  while (k < n) {
    k += period;
  }
  cout << journey[k] + 1 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

#define INF 100000000;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
#define eps 1.0e-14;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  int A[200006];
  rep(i, n) {
    cin >> A[i];
    A[i]--;
  }
  int journey[200006];
  journey[0] = 0;
  for (int i = 1; i < n * 2; i++) {
    journey[i] = A[journey[i - 1]];
  }
  if (k < n) {
    cout << journey[k - 1] + 1 << endl;
  }
  int loop_start = n - 2;
  int loop_end = n - 1;
  while (journey[loop_start] != journey[loop_end]) {
    loop_start--;
  }
  int period = loop_end - loop_start;
  int ans = k % period;
  k %= period;
  while (k < n) {
    k += period;
  }
  cout << journey[k] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  vector<long long> round;
  vector<long long> maps(N + 1, -1);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int now = 1;
  while (maps.at(now) == -1) {
    maps.at(now) = round.size();
    round.push_back(now);
    now = A.at(now - 1);
  }
  long long period = round.size() - maps.at(now);
  long long start = maps.at(now);
  if (K < start) {
    cout << round.at(K) << endl;
  } else {
    K -= start;
    K %= period;
    cout << round.at(start + K) << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  vector<int> round;
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  int now = 1;
  round.push_back(now);
  while (1) {
    now = A.at(now);
    round.push_back(now);
    if (find(round.begin(), round.end(), A.at(now)) != round.end()) {
      break;
    }
  }
  int home = 0;
  for (long i = 0; i < round.size(); i++) {
    if (A.at(now) == round.at(i)) {
      home = i;
      break;
    }
  }
  if (K < home) {
    cout << round.at(K) << endl;
  } else {
    K -= home;
    K = K % (round.size() - home);
    cout << round.at(home + K) << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n, k;
  bool b[200001];
  int a[200001];

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int c = 0;
  int p = 1;
  while (b[p] == false) {
    b[p] = true;
    p = a[p];
    c++;
  }
  //    cout << p << endl;
  int x = 1;
  int c0 = 0;
  while (x != p) {
    x = a[x];
    c0++;
  }
  //    cout << c0 << endl;
  //    cout << c << endl;

  if (k <= c) {
  } else {
    k = ((k - c0) % (c - c0)) + c0;
  }
  p = 1;
  while (k > 0) {
    p = a[p];
    k--;
  }
  cout << p << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n, k;
  bool b[200001];
  int a[200001];

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int c = 0;
  int p = 1;
  while (b[p] == false) {
    b[p] = true;
    p = a[p];
    c++;
  }
  //    cout << p << endl;
  int x = 1;
  int c0 = 1;
  if (p != 1) {
    while (a[x] != p) {
      x = a[x];
      c0++;
    }
  }
  //    cout << c0 << endl;
  //    cout << c << endl;

  if (k <= c) {
  } else {
    k = ((k - c0) % (c - c0)) + c0;
  }
  p = 1;
  while (k > 0) {
    p = a[p];
    k--;
  }
  cout << p << endl;

  return 0;
}