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

#include <bits/stdc++.h>

using namespace std;

#define tie()                                                                  \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long

const int N = 2e5 + 20;

bool vis[N];

int cnt = 0;

vector<int> g[N];
int last[N];

int dfs(int x, int k) {
  if (k == 0 or g[x].empty())
    return x;
  cnt++;
  if (vis[x]) {
    if (k > (cnt - last[x]))
      k %= (cnt - last[x]);
    if (k == 0 or g[x].empty())
      return x;
    return dfs(g[x][0], k - 1);
  }
  vis[x] = true;
  last[x] = cnt;
  return dfs(g[x][0], k - 1);
}

int32_t main() {
  tie();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int to;

    for (int i = 1; i <= n; i++) {
      cin >> to;
      g[i].push_back(to);
    }

    memset(vis, false, sizeof vis);
    cout << dfs(1, k) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define tie()                                                                  \\
  ios_base ::sync_with_stdio(false);                                           \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long

const int N = 1e5 + 20;

bool vis[N];

int cnt = 0;

vector<int> g[N];
int last[N];

int dfs(int x, int k) {
  if (k == 0 or g[x].empty())
    return x;
  cnt++;
  if (vis[x]) {
    if (k > (cnt - last[x]))
      k %= (cnt - last[x]);
    if (k == 0 or g[x].empty())
      return x;
    return dfs(g[x][0], k - 1);
  }
  vis[x] = true;
  last[x] = cnt;
  return dfs(g[x][0], k - 1);
}

int32_t main() {
  tie();
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int to;

    for (int i = 1; i <= n; i++) {
      cin >> to;
      g[i].push_back(to);
    }

    memset(vis, false, sizeof vis);
    cout << dfs(1, k) << 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;

int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<int> narabi;
  vector<int> order(n + 1, -1);
  int c = 1;
  int l = 0;
  int v = 1; // vが移動していく
  while (
      order[v] ==
      -1) { // order[v]はa[v-1]での行き先（既に来た場合，何番目に来たかが入っている）
    order[v] = narabi.size(); // 1,2,3...番目
    narabi.push_back(v);      // 訪れたところをメモっていく
    v = a[v - 1];
  }
  c = narabi.size() - order[v];
  l = order[v];

  if (k < l)
    cout << narabi[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << narabi[l + k] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  long long k;
  vector<int> a(n);
  cin >> n >> k;
  rep(i, n) { cin >> a[i]; }
  vector<int> narabi;
  vector<int> order(n + 1, -1);
  int c = 1;
  int l = 0;
  int v = 1; // vが移動していく
  while (
      order[v] ==
      -1) { // order[v]はa[v-1]での行き先（既に来た場合，何番目に来たかが入っている）
    order[v] = narabi.size(); // 1,2,3...番目
    narabi.push_back(v);      // 訪れたところをメモっていく
    v = a[v - 1];
  }
  c = narabi.size() - order[v];
  l = order[v];

  if (k < l)
    cout << narabi[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << narabi[l + k] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  // ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int a[n], b[n];
  rep(i, n) b[i] = 0;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    b[a[i]]++;
  }

  queue<int> qq;
  rep(i, n) {
    if (b[i] == 0) {
      qq.push(i);
    }
  }
  while (!qq.empty()) {
    int num = qq.front();
    qq.pop();
    b[a[num]]--;
    if (b[a[num]] == 0)
      qq.push(a[num]);
  }
  int now = 0;
  int nex = 0;
  while (b[nex] == 0) {
    now++;
    nex = a[nex];
    if (now == k) {
      cout << ++nex << endl;
      return 0;
    }
  }

  int st = nex;
  int roop = 1;
  nex = a[nex];
  while (nex != st) {
    roop++;
    nex = a[nex];
  }
  k -= now;
  k %= roop;
  rep(i, k) { st = a[st]; }
  cout << ++st << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  cin.tie(0);
  // ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int a[n], b[n];
  rep(i, n) b[i] = 0;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    b[a[i]]++;
  }

  int now = 0;
  int nex = 0;
  int ans = -1;
  while (b[nex] == 0) {
    now++;
    nex = a[nex];
    b[nex]--;
    if (now == k) {
      cout << ++nex << endl;
      return 0;
    }
  }

  int st = nex;
  int roop = 1;
  nex = a[nex];
  while (nex != st) {
    roop++;
    nex = a[nex];
  }
  k -= now;
  k %= roop;
  rep(i, k) { st = a[st]; }
  cout << ++st << endl;

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

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

int32_t main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i], v[i]--;

  if (k <= n) {
    int curr = 0;
    if (k == 0) {
      cout << curr + 1 << "\\n";
      exit(0);
    }
    int ans = v[curr];
    for (int i = 0; i < k - 1; i++) {
      ans = v[ans];
    }
    cout << ans + 1 << "\\n";
    exit(0);
  }

  if (v[0] == 0) {
    cout << 1 << "\\n";
    exit(0);
  }

  map<int, int> mp;
  int curr = v[0];
  int len_of_cycle = 0;
  mp[0] = 0;
  while (mp.count(curr) == 0) {
    mp[curr] = ++len_of_cycle;
    curr = v[curr];
  }
  k -= mp[curr];
  len_of_cycle = len_of_cycle + 1 - mp[curr];
  k %= len_of_cycle;
  // cout<<curr+1<<" "<<mp[curr]<<" "<<len_of_cycle<<" "<<k<<"\\n";
  if (k == 0) {
    cout << curr + 1 << "\\n";
    exit(0);
  }
  int ans = v[curr];
  for (int i = 0; i < k - 1; i++) {
    ans = v[ans];
  }
  cout << ans + 1 << "\\n";
  return 0;
}

Error: WA

Fixed submission:

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

int32_t main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<vector<int>> v(n, vector<int>(m));
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (auto &j : v[i])
      cin >> j;
  }

  int mn = LLONG_MAX;
  for (int i = 0; i < (1 << n); i++) {
    vector<int> curr(m, 0);
    int cost = 0;
    for (int j = 0; j < 12; j++) {
      if (i & (1 << j)) {
        cost += c[j];
        for (int k = 0; k < m; k++)
          curr[k] += v[j][k];
      }
    }
    if (*min_element(curr.begin(), curr.end()) >= x)
      mn = min(mn, cost);
  }
  if (mn == LLONG_MAX)
    cout << "-1\\n";
  else
    cout << mn << "\\n";
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

const int N = 200010;

int a[N];
bool st[N];
int ans[N];

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  st[1] = true;
  int x = 1;
  int num = 0;
  ans[1] = 0;
  int kk = 0;
  for (int i = 1; i <= k; i++) {
    x = a[x];
    num++;
    if (st[x]) {
      kk = num - ans[x];
      break;
    } else {
      ans[x] = num;
      st[x] = true;
    }
  }

  if (num >= k) {
    int p = 1;
    for (int i = 1; i <= k; i++) {
      p = a[p];
    }
    printf("%d\\n", p);
    return 0;
  }

  long long res = k - num;

  res = res % kk;
  if (res == 0) {
    printf("%d\\n", x);
    return 0;
  }
  int y = x;

  for (int i = 1; i <= res; i++) {
    y = a[y];
  }
  printf("%d\\n", y);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

const int N = 200010;

int a[N];
bool st[N];
int ans[N];

int main() {
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  st[1] = true;
  int x = 1;
  int num = 0;
  ans[1] = 0;
  int kk = 0;
  for (int i = 1; i <= k; i++) {
    x = a[x];
    num++;

    if (st[x]) {
      kk = num - ans[x];
      break;
    } else {
      ans[x] = num;
      st[x] = true;
    }
  }
  int res = k - num;

  res = (res + kk) % kk;
  if (res < 0)
    res = (res + kk) % kk;
  if (res == 0) {
    printf("%d\\n", x);
    return 0;
  }
  int y = 1;

  for (int i = 1; i <= res; i++) {
    y = a[y];
  }
  printf("%d\\n", y);
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// Author: Pakhomovee
#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using namespace std::chrono;

#define int long long

bool found = false;
vector<int> cycle;
int lol = 0;

void dfs(int v, vector<vector<int>> &g, vector<bool> &used, vector<int> &s,
         int k) {
  if (k == 0) {
    cout << v + 1 << endl;
    exit(0);
  }
  used[v] = true;
  s.push_back(v);
  for (int u : g[v]) {
    if (found) {
      return;
    }
    if (!used[u]) {
      dfs(u, g, used, s, k - 1);
    } else {
      while (s.back() != u) {
        cycle.push_back(s.back());
        s.pop_back();
      }
      cycle.push_back(u);
      lol = s.size() - 1;
      found = true;
    }
  }
  s.pop_back();
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    --x;
    g[i].push_back(x);
  }
  vector<bool> used(n, false);
  vector<int> x;
  dfs(0, g, used, x, k);
  reverse(cycle.begin(), cycle.end());
  k -= lol;
  int gg = k % (long long)(cycle.size());
  cout << cycle[gg] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  auto start = system_clock::now();

  solve();

  auto end = system_clock::now();
  cerr << "Time: " << duration_cast<milliseconds>(end - start).count() << "ms"
       << endl;
}

Error: 0
Time: 0ms


Fixed submission:

// Author: Pakhomovee
#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using namespace std::chrono;

#define int long long

bool found = false;
vector<int> cycle;
int lol = 0;

void dfs(int v, vector<vector<int>> &g, vector<bool> &used, vector<int> &s) {
  used[v] = true;
  s.push_back(v);
  for (int u : g[v]) {
    if (found) {
      return;
    }
    if (!used[u]) {
      dfs(u, g, used, s);
    } else {
      while (s.back() != u) {
        cycle.push_back(s.back());
        s.pop_back();
      }
      cycle.push_back(u);
      lol = s.size() - 1;
    }
  }
  s.pop_back();
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    --x;
    g[i].push_back(x);
  }
  vector<bool> used(n, false);
  vector<int> x;
  dfs(0, g, used, x);
  reverse(cycle.begin(), cycle.end());
  k -= lol;
  int gg = k % (long long)(cycle.size());
  cout << cycle[gg] + 1 << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  auto start = system_clock::now();

  solve();

  auto end = system_clock::now();
  cerr << "Time: " << duration_cast<milliseconds>(end - start).count() << "ms"
       << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++)
    cin >> A[i + 1];
  vector<ll> arrive(N + 1, -1);
  int cur = 1;
  ll t = 0;
  while (K > 0) {
    if (arrive[cur] != -1) {
      ll cycle = t - arrive[cur];
      if (cycle != 0)
        K %= cycle;
    } else {
      arrive[cur] = t;
    }
    if (K > 0) {
      t++;
      K--;
      cur = A[cur];
    }
  }
  cout << cur << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++)
    cin >> A[i + 1];
  vector<ll> arrive(N + 1, -1);
  int cur = 1;
  ll t = 0;
  while (K > 0) {
    if (arrive[cur] != -1) {
      ll cycle = t - arrive[cur];
      K %= cycle;
      t += cycle;
    } else {
      arrive[cur] = t;
    }
    if (K > 0) {
      t++;
      K--;
      cur = A[cur];
    }
  }
  cout << cur << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define pb push_back
#define nn "\\n"
#define all(p) p.begin(), p.end()
#define zz(v) (ll) v.size()
#define ss ' '
#define pii pair<ll, ll>
const double eps = 1e-8;
vll v;

int main() {

  //     ios::sync_with_stdio(0);
  //	cin.tie(0);
  ll n, k;
  cin >> n >> k;
  ll ar[n + 10];
  for (ll i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  map<ll, ll> mp;
  // mp[1]=1;
  ll st, pk = 1;
  for (ll i = 1;; i++) {
    pk = ar[pk];
    if (mp[pk] == 0) {
      mp[pk] = 1;
      v.pb(pk);
    } else {
      st = pk;
      break;
    }
  }

  vll rep;
  ll f = 0, df = 0;
  for (ll i = 0; i < zz(v); i++) {
    if (v[i] == st) {
      f = 1;
    }
    if (f) {
      rep.pb(v[i]);

    } else
      df++;
  }

  // cout<<st<<nn;

  if (k <= df)
    cout << v[k - 1] << nn;
  else {
    ll md = (k - df) % zz(rep);
    ll sz = zz(rep);
    if (md == 0)
      cout << rep[sz - 1] << nn;
    else
      cout << rep[md - 1] << nn;
  }

  // for(ll i=0;i<zz(rep);i++) cout<<rep[i]<<ss;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define pb push_back
#define nn "\\n"
#define all(p) p.begin(), p.end()
#define zz(v) (ll) v.size()
#define ss ' '
#define pii pair<ll, ll>
const double eps = 1e-8;

ll ar[200010];
ll cm[200010];
int main() {

  //     ios::sync_with_stdio(0);
  //	cin.tie(0);

  ll n, m;
  cin >> n >> m;

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

  ll s = 1;
  vll v;
  vll pk;
  ll c = 0;
  for (ll i = 1; i <= 1e5; i++) {

    s = ar[s];

    cm[s]++;
    if (cm[s] == 3 && zz(v)) {
      break;
    }
    if (cm[s] == 2)
      v.pb(s);
    else
      v.clear();
    pk.pb(s);
    c++;
  }

  ll df = c - zz(v);
  if (m <= df)
    cout << pk[m - 1] << nn;
  else {
    ll md = (m - df) % zz(v);
    if (md == 0)
      cout << v[zz(v) - 1] << nn;
    else
      cout << v[md - 1] << nn;
  }
  //  cout<<" sp\\n";

  //   cout<<df<<ss<<zz(v)<<nn;

  // for(ll i=0;i<zz(pk);i++) cout<<pk[i]<<ss;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n;
  int64_t k;
  cin >> n >> k;
  int a[n], aa[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    aa[i] = 0;
  }
  int present = 1;
  aa[present] = 0;
  for (int i = 1; i <= k; i++) {
    present = a[present - 1];
    if ((aa[present - 1] > 0) || (present == 1)) {
      k = (k - aa[present - 1]) % (i - aa[present - 1]);
      for (int j = 0; j < k; j++) {
        present = a[present - 1];
      }
      cout << present;
      break;
    } else if (i == k) {
      cout << present;
    } else {
      aa[present - 1] = i;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  int64_t k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int present = 1;
  for (int64_t i = 0; i < k; i++) {
    present = a[present - 1];
  }
  cout << present;

  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, S;
  cin >> N >> S;
  vector<ll> tele(N);
  for (ll i = 0; i < N; i++) {
    cin >> tele[i];
    tele[i]--;
  }
  vector<ll> loop(N, 0);
  vector<ll> order;
  ll now = 0;
  ll cnt = 0;
  while (loop[now] != 1) {
    loop[now] = 1;
    order.push_back(now);
    cnt++;
    now = tele[now];
  }
  ll looptime = 0;
  ll i;
  for (i = 0; i < cnt; i++) {
    if (order[i] == now) {
      looptime = cnt - i;
      break;
    }
  }
  ll mod = (S - i) % looptime;
  if (S - i < 0) {
    cout << order[S] + 1 << endl;
  } else {
    if (mod + i == order.size())
      cout << order[i] + 1 << endl;
    else
      cout << order[mod + i] + 1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> tele(N);
  for (ll i = 0; i < N; i++) {
    cin >> tele[i];
    tele[i]--;
  }
  vector<ll> loop(N, 0);
  vector<ll> order;
  ll now = 0;
  ll cnt = 0;
  while (loop[now] != 1) {
    loop[now] = 1;
    order.push_back(now);
    cnt++;
    now = tele[now];
  }
  ll looptime = 0;
  ll i;
  for (i = 0; i < cnt; i++) {
    if (order[i] == now) {
      looptime = cnt - i;
      break;
    }
  }
  ll mod = (atoi(S.c_str()) - i) % looptime;
  if (atoi(S.c_str()) - i < 0) {
    cout << order[atoi(S.c_str())] + 1 << endl;
  } else {
    if (mod + i == order.size())
      cout << order[i] + 1 << endl;
    else
      cout << order[mod + i] + 1 << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  int point = 1;
  ll k;
  cin >> n >> k;
  vector<int> v(n + 1);
  map<int, int> m;
  bool ok = false;
  for (int i = 1; i <= n; i++) {
    cin >> v.at(i);
  }
  ll loop = 0;
  int cur = 1;
  int fir, end = 0;
  int p = 0;
  for (int i = 1; i <= k; i++) {
    m[cur]++;
    if (m[cur] == 2) {
      point = cur;
      break;
    }
    cur = v.at(cur);
  }
  cur = 1;
  for (int i = 1; i <= k; i++) {
    cur = v.at(cur);
    if (cur == point) {
      p++;
      if (p == 1)
        fir = i;
      if (p == 2) {
        end = i;
        loop = end - fir;
        break;
      }
    }
    if (i == k) {
      cout << cur << endl;
      return 0;
    }
  }
  cur = point;
  for (int i = 0; i < ((k - fir) % loop); i++) {
    cur = v.at(cur);
  }
  cout << cur << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  int point = 1;
  ll k;
  cin >> n >> k;
  vector<int> v(n + 1);
  map<int, int> m;
  bool ok = false;
  for (int i = 1; i <= n; i++) {
    cin >> v.at(i);
    m[v.at(i)]++;
    if (!ok && m[v.at(i)] == 2) {
      point = v.at(i);
      ok = true;
    }
  }
  ll loop = 0;
  int cur = 1;
  int fir, end = 0;
  int p = 0;
  for (int i = 1; i <= k; i++) {
    cur = v.at(cur);
    if (cur == point) {
      p++;
      if (p == 1)
        fir = i;
      if (p == 2) {
        end = i;
        loop = end - fir;
        break;
      }
    }
    if (i == k) {
      cout << cur << endl;
      return 0;
    }
  }
  cur = point;
  for (int i = 0; i < ((k - fir) % loop); i++) {
    cur = v.at(cur);
  }
  cout << cur << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
int main() {

  int n;
  long long k;
  cin >> n >> k;
  vector<int> ports(n + 1);
  for (int i = 1; i < n + 1; ++i) {
    cin >> ports[i];
  }
  int tor = ports[1];
  int hare = ports[ports[1]];
  while (true) {
    tor = ports[tor];
    hare = ports[ports[hare]];
    if (tor == hare) {
      break;
    }
  }
  tor = 1;
  int head = 0;
  while (tor != hare) {
    tor = ports[tor];
    hare = ports[hare];

    head++;
  }
  // cycle length
  tor = ports[tor];
  int cycle = 1;
  while (tor != hare) {
    tor = ports[tor];
    cycle++;
  }
  tor = 1;
  while (k > 0 && head > 0) {
    tor = ports[tor];
    k--;
    head--;
  }
  k = k % (ll)cycle;
  while (k > 0) {
    tor = ports[tor];
    k--;
  }
  cout << tor << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(int x, int y, int z) {}

int main() {
  long long n, k, x;
  vector<int> a;
  cin >> n >> k;
  a.push_back(-1);
  for (long long i = 1; i <= n; i++) {
    cin >> x;
    a.push_back(x);
  }
  x = 1;
  while (--k) {
    x = a[x];
  }
  cout << a[x];
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

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

  vector<int> town(N);
  for (int i = 0; i < N; i++) {
    cin >> town.at(i);
    town.at(i) = town.at(i) - 1;
  }

  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;

  for (int i = 0; i < K; i++) {
    now = town.at(now);
    if (count.at(now) != 0) {
      flag = true;
      roop_town = i - count.at(now);
      rest = K - i - 1;
      break;
    }
    count.at(now) = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++)
      now = town.at(now);
  }
  cout << now + 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

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

  vector<int> town(N);
  for (int i = 0; i < N; i++) {
    cin >> town.at(i);
    town.at(i) = town.at(i) - 1;
  }

  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;

  for (int i = 0; i < K; i++) {
    now = town.at(now);
    if (count.at(i) != 0) {
      flag = true;
      roop_town = i - count.at(now);
      rest = K - i - 1;
      break;
    }
    count.at(now) = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++)
      now = town.at(now);
  }
  cout << now + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;
  for (int i = 0; i < K; i++) {
    now = A[now];
    if (count[now] != 0) {
      roop_town = i - count[now];
      rest = K - i - 1;
      flag = true;
      break;
    }
    count[now] = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++) {
      now = A[now];
    }
  }
  cout << now + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

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

  vector<int> town(N);
  for (int i = 0; i < N; i++) {
    cin >> town.at(i);
    town.at(i)--;
  }

  int now = 0;
  vector<int> count(N, 0);
  long long roop_town, rest;
  bool flag = false;

  for (int i = 0; i < K; i++) {
    now = town.at(now);
    if (count.at(i) != 0) {
      flag = true;
      roop_town = i - count.at(now);
      rest = K - i - 1;
      break;
    }
    count[now] = i;
  }
  if (flag) {
    rest = rest % roop_town;
    for (int j = 0; j < rest; j++)
      now = town.at(now);
  }
  cout << now + 1 << endl;
}