alexjercan/bugnet huggingface dataset:
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n;
  ll k;
  cin >> n >> k;

  vector<int> a(n);

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

  // 頂点に何番目に訪れるか記録していく
  // すでに訪れていれば記録せずおわり

  vector<int> s;              // 訪れた頂点のメモ
  vector<int> ord(n + 1, -1); // 何番目に訪れたか

  int c = 1, l = 0;

  {
    int v = 1; // 最初は1
    while (ord[v] == -1) {
      ord[v] = s.size();
      s.push_back(v);
      v = a[v - 1];
    }
    // whileから抜けたとき, vはループの頭を指す
    // s.size()は ひげ+周期

    // ord[v]はヒゲの長さ
    //
    //  0, 1, 2, 3, 4, ...
    // 0始まりは、その要素より前に含まれる要素数を表す

    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>
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];
  }

  long long len = 0;
  long long mod = 0;
  int next = a[1];

  vector<int> where(1, 1);

  // where.push_back(1);
  // cout << where[where.size()-1] << endl;

  bool ok = true;

  while (!len && ok) {
    where.push_back(a[where[where.size() - 1]]);

    // cout << where[where.size()-1] << endl;

    for (int j = where.size() - 2; j > -1; j--) {

      if (where[where.size() - 1] == where[j]) {

        len = where.size() - 1 - j;
        mod = j;
        ok = false;
        break;
      }
    }
  }

  /*
   while(next != 1){
   next = a[next];
   len++;
   }
   */
  // cout << endl;
  // cout << len << endl;
  // cout << mod << endl;

  if (k < mod) {
    cout << where[k] << endl;
    return 0;
  }

  k -= mod;
  k %= len;
  /*
  next = where[mod];

  for(int i=0; i<k; i++){
      next = a[next];
  }
  */
  cout << where[mod + k] << endl;

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

#include <bits/stdc++.h>

using 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[i];
  }

  vector<ll> root(n);
  root[0] = 1;

  ll t = a[root[0] - 1];
  ll ix = 0;

  for (ll i = 0; i < n; i++) {
    t = a[root[i] - 1];
    if (t == 0) {
      t = root[i] - 1;
      ix = i;
      break;
    }
    a[root[i] - 1] = 0;
    root[i + 1] = t;
  }

  ll x;
  for (ll i = 0; i < ix + 1; i++) {
    if (root[i] == root[ix]) {
      x = i;
      break;
    }
  }
  // cout << x << endl;
  ll bef = x;
  ll loop = ix - x;
  // cout << bef << " " << loop << endl;

  if (k < bef) {
    cout << root[k] << endl;
  } else {
    ll p = ((k - bef) % loop) + bef;
    cout << root[p] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using 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[i];
  }

  vector<ll> root(n);
  root[0] = 1;

  ll t = a[root[0] - 1];
  ll ix = 0;

  for (ll i = 0; i < n; i++) {
    t = a[root[i] - 1];
    if (t == 0) {
      t = root[i];
      ix = i;
      break;
    }
    a[root[i] - 1] = 0;
    root[i + 1] = t;
  }
  ll bef = t;
  ll loop = ix - bef;
  // cout << bef << " " << loop << endl;

  if (k < bef) {
    cout << root[k] << endl;
  } else {
    ll p = ((k - bef) % loop) + bef;
    cout << root[p] << endl;
  }
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int D = 110;
const int N = 2000000;
int to[D][N];
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[0][i];
  }
  rep(i, D - 1) {
    for (int j = 1; j <= n; j++) {
      to[i + 1][j] = to[i][to[i][j]];
    }
  }
  int now_town = 1;
  int tmp = -1;
  do {
    tmp++;
    if (k % 2 == 1) {
      now_town = to[tmp][now_town];
    }
    k /= 2;
  } while (k != 0);
  cout << now_town << endl;
}

Error: -11

Fixed submission:

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

const int D = 110;
const int N = 2000000;
int to[D][N];
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> to[0][i];
  }
  rep(i, D) {
    for (int j = 1; j <= n; j++) {
      to[i + 1][j] = to[i][to[i][j]];
    }
  }
  int now_town = 1;
  int tmp = -1;
  do {
    tmp++;
    if (k % 2 == 1) {
      now_town = to[tmp][now_town];
    }
    k /= 2;
  } while (k != 0);
  cout << now_town << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, deque<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i], --A[i];

  deque<int> a;                // 繰り返しの部分
  vector<bool> seen(N, false); // 一度見たかどうか
  int cur = 0;
  while (true) {
    // 一度通った頂点を見つけたときの処理
    if (seen[cur]) {
      while (a[0] != cur) {
        // 最初の余計な数手分を除去する
        --K;
        a.pop_front();

        // 繰り返す前に K が限界になったらリターン
        if (K == 0) {
          cout << a[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    // 最初は愚直にシミュレーションしつつ、履歴をメモしていく
    a.push_back(cur);
    seen[cur] = true;
    cur = A[cur];
  }
  cout << a[K % a.size()] + 1 << endl;
}

Error: 0

Fixed submission:

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

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, deque<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}

int main() {
  int N;
  long long K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i], --A[i];

  deque<int> a;                // 繰り返しの部分
  vector<bool> seen(N, false); // 一度見たかどうか
  int cur = 0;
  while (true) {
    // 一度通った頂点を見つけたときの処理
    if (seen[cur]) {
      while (a[0] != cur) {
        // 最初の余計な数手分を除去する
        --K;
        a.pop_front();

        // 繰り返す前に K が限界になったらリターン
        if (K == 0)
          return a[0] + 1;
      }
      break;
    }
    // 最初は愚直にシミュレーションしつつ、履歴をメモしていく
    a.push_back(cur);
    seen[cur] = true;
    cur = A[cur];
  }
  cout << a[K % a.size()] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

const int N_MAX = 2000005;
const int NIL = -1;

int TEL[N_MAX + 5];
int A[N_MAX + 5];
int town[N_MAX + 5];

int main() {
  int n;
  long long int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= n; i++)
    TEL[i] = NIL;
  TEL[1] = 0;
  int nowp = 1;
  int loopcount;
  int loopstart;
  town[0] = 1;
  int ansp = -1;
  for (int i = 1; i <= n + 5; i++) {
    int nextp = A[nowp];
    town[i] = nextp;
    if (TEL[nextp] != NIL) {
      loopcount = i - TEL[nextp];
      loopstart = TEL[nextp];
      break;
    }
    TEL[nextp] = i;
    nowp = nextp;
  }
  if (k < loopstart)
    ansp = town[k];
  else {
    long long int kl = k - loopstart;
    kl %= loopcount;
    ansp = town[loopstart + kl];
  }
  cout << ansp << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const int N_MAX = 200005;
const int NIL = -1;

int TEL[N_MAX + 5];
int A[N_MAX + 5];
int town[N_MAX + 5];

int main() {
  int n;
  long long int k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= n; i++)
    TEL[i] = NIL;
  TEL[1] = 0;
  int nowp = 1;
  int loopcount;
  int loopstart;
  town[0] = 1;
  int ansp = -1;
  for (int i = 1; i <= n + 5; i++) {
    int nextp = A[nowp];
    town[i] = nextp;
    if (TEL[nextp] != NIL) {
      loopcount = i - TEL[nextp];
      loopstart = TEL[nextp];
      break;
    }
    TEL[nextp] = i;
    nowp = nextp;
  }
  if (k < loopstart)
    ansp = town[k];
  else {
    int kl = k - loopstart;
    kl %= loopcount;
    ansp = town[loopstart + kl];
  }
  cout << ansp << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define sz(s) (long long int)(s).size()
#define pb push_back
#define mp make_pair
const ll inf = 1000000000;
const ll MOD = 1000000007;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  register ll i, j, k, x, y, m, n, t, q, temp, sum = 0, count = 0, f = 1;
  cin >> n >> k;
  vector<ll> a(n + 1, 0);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  set<ll> s;
  x = a[1];
  vector<ll> v;
  do {
    s.insert(x);
    v.pb(x);
    x = a[x];
  } while (s.find(x) == s.end());

  for (i = 0; i < v.size(); i++) {
    if (v[i] == x) {
      break;
    }
  }
  if (k <= i) {
    cout << v[k - 1];
  } else {
    ll loop = v.size() - i;
    ll index = (k - i - 1 + loop) % loop;
    // if(index == 0)index = v.size();
    // index = i + index - 1;
    // cout<<index<<"---\\n";
    cout << v[index + i];
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define sz(s) (long long int)(s).size()
#define pb push_back
#define mp make_pair
const ll inf = 1000000000;
const ll MOD = 1000000007;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  register ll i, j, k, x, y, m, n, t, q, temp, sum = 0, count = 0, f = 1;
  cin >> n >> k;
  vector<ll> a(n + 1, 0);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  set<ll> s;
  x = a[1];
  vector<ll> v;
  do {
    // cout<<x<<"\\n";
    s.insert(x);
    v.pb(x);
    x = a[x];
  } while (s.find(x) == s.end());

  for (i = 0; i < v.size(); i++) {
    // cout<<v[i]<<" ";
    if (v[i] == x) {
      break;
    }
  }
  if (k <= i + 1) {
    cout << v[k - 1];
  } else {
    ll loop = v.size() - i;
    // cout<<loop<<"\\n";
    k -= i;
    // cout<<k<<"-\\n";
    ll index = k % loop;
    if (index == 0)
      index = v.size();
    index = i + index - 1;
    // cout<<index<<"---\\n";
    cout << v[index];
  }
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded


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

int main() {

  int N;
  long K;

  cin >> N >> K;

  vector<int> A(N + 1, 0);
  vector<long> past(N + 1, -1);
  // map<int, long> past{};

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int tmp = 1;
  long warp;
  past[tmp] = 0;
  long warped = 0;

  for (long i = 1; i <= K; i++) {
    tmp = A[tmp];

    if (past[tmp] > 0) {
      warp = (K - i) / (i - past[tmp] - warped) * (i - past[tmp] - warped);
      i += warp;
      warped += warp;
    } else {
      past[tmp] = i;
    }
  }
  cout << tmp << endl;

  return 0;
}


Error: TLE

Fixed submission:


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

int main() {

  int N;
  long K;

  cin >> N >> K;

  vector<int> A(N + 1, 0);
  // vector<long> past(N+1,-1);
  map<int, long> past{};

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int tmp = 1;
  long warp;
  past[tmp] = 1;
  K++;

  for (long i = 2; i <= K; i++) {
    tmp = A[tmp];

    if (past[tmp] > 0) {
      warp = (K - i) / (i - past[tmp]) * (i - past[tmp]);

      // for(int j=1;j<=N;j++){
      //     if(past[j]>=0) past[j] += warp;
      // }
      for (auto itr = past.begin(); itr != past.end(); ++itr) {
        past[itr->first] += warp;
      }
      i += warp;
    } else {
      past[tmp] = i;
    }
  }
  cout << tmp << endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> g;
bool seen[200001];
vector<int> go;

int dfs(int s) {
  if (seen[s])
    return s;
  seen[s] = true;
  go.push_back(s);
  return dfs(g[s]);
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  g.resize(n);

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

  int loop;
  loop = dfs(0);
  int start;
  int mod;
  int len;
  len = go.size();

  for (int i = 0; i < len; i++) {
    if (go[i] == loop)
      start = i;
    mod = len - start;
  }

  vector<int> to(mod);
  for (int j = start; j < len; j++) {
    to[j % mod] = go[j];
  }

  if (k < len)
    cout << go[int(k)] + 1 << endl;
  else {
    k %= (ll)mod;
    cout << to[int(k)] + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> g;
bool seen[200001];
vector<int> go;

int dfs(int s) {
  if (seen[s])
    return s;
  seen[s] = true;
  go.push_back(s);
  dfs(g[s]);
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  g.resize(n);

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

  int loop;
  loop = dfs(0);
  int start;
  int mod;
  int len;
  len = go.size();

  for (int i = 0; i < len; i++) {
    if (go[i] == loop)
      start = i;
    mod = len - start;
  }

  vector<int> to(mod);
  for (int j = start; j < len; j++) {
    to[j % mod] = go[j];
  }

  if (k < len)
    cout << go[int(k)] + 1 << endl;
  else {
    k %= (ll)mod;
    cout << to[int(k)] + 1 << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;

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

  vector<int> ord(n, -1);
  vector<int> s;
  int now = 0;
  while (ord[now] == -1) {
    s.push_back(now);
    ord[now] = s.size() - 1;
    now = a[now];
  }

  int repLen = s.size() - ord[now];
  int toLen = ord[now];

  // cout << toLen << " " << repLen << endl;
  if (k < toLen)
    cout << (s[k] + 1) << endl;
  else {
    k -= toLen;
    k %= repLen;
    cout << (s[toLen + k] + 1) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    --a[i];
  }
  set<int> st;
  st.insert(0);
  ll d = 0;
  rep(i, n) {
    auto iter = st.find(a[i]);
    if (iter == st.end()) {
      ++d;
      st.insert(a[i]);
    } else
      break;
  }
  ll mv = k;
  if (k > d)
    mv = k % (ll)d;
  int now = 0;
  while (mv > 0) {
    now = a[now];
    --mv;
  }
  cout << (now + 1) << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>

using namespace std;
using LL = long long;
const int N = 200005;
int n, a[N];
int mp[N];
int main() {
  LL k;
  cin >> n >> k;

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

  int now = 1;
  map<int, int> v;

  int count = 1;
  if (a[1] == 1) {
    cout << 1 << endl;
    return 0;
  }

  int len, k1;

  while (1) {
    if (v[now] > 0) {
      len = count - v[now]; // 环的长度
      k1 = v[now] - 1;      // 多余路径长度
      break;
    }
    v[now] = count;
    now = mp[now]; // 跳到新节点
    ++count;       // 路径增加
  }

  int res = 0;

  if (k > k1) {
    res = k1 + (k - k1) % len;
  } else
    res = k;

  now = 1;
  for (int i = 1; i <= res; ++i) {
    now = mp[now];
  }

  cout << now << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using LL = long long;
const int N = 200005;
int h[2 * N], e[2 * N], ne[2 * N], idx;
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
int n;
LL k;
// dfn:当前的时间戳 low:可以达到的最小时间戳 timestamp:累计时间戳
int dfn[2 * N], low[2 * N], timestamp;
// in_stk:存储u能回到的祖先的点
int stk[2 * N], in_stk[2 * N], top;
// 强连通分量编号
int scc_cnt;
// 一个点所在强连通分量的编号
int id[2 * N];
// 强连通分量内点的数量
int sz[2 * N];
vector<int> block[2 * N];
void tarjan(int u) {
  dfn[u] = low[u] = ++timestamp;
  stk[++top] = u, in_stk[u] = true;
  for (int i = h[u]; i != -1; i = ne[i]) {
    int j = e[i];
    // 当前点没有遍历过
    if (!dfn[j]) {
      tarjan(j);
      low[u] = min(low[u], low[j]);
    } // u所能达到的祖先节点，即可以达到的时间戳
    else if (in_stk[j])
      low[u] = min(low[u], dfn[j]);
  }
  // 回溯完毕，并且当前节点u的时间戳等于强连通分量最早的时间戳
  // u是其所在的强连通分量的最高点，即dfn[u] = low[u]
  if (dfn[u] == low[u]) {
    int y;
    ++scc_cnt;
    do {
      y = stk[top--];
      in_stk[y] = false;
      id[y] = scc_cnt;
      ++sz[scc_cnt]; // 统计该强连通所有点的数量
    } while (y != u);
  }
}

int now;

vector<int> ublock;

int g;
bool flag;
bool dfs2(int u) {
  if (u == g && flag)
    return true;
  for (int i = h[u]; i != -1; i = ne[i]) {
    flag = true;
    int j = e[i];
    ublock.push_back(j);
    if (dfs2(j))
      return true;
  }
}

int mod;

bool dfs(int u) {
  int b = id[u];
  if (sz[b] > 1) {
    mod = sz[b];
    g = u;
    dfs2(u);
    return true;
  }

  for (int i = h[u]; i != -1; i = ne[i]) {
    k -= 1;
    int j = e[i];
    if (dfs(j))
      return true;
  }
}

int main() {

  scanf("%d%lld", &n, &k);

  memset(h, -1, sizeof h);
  int t;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &t);
    add(i, t);
  }

  for (int i = 1; i <= n; ++i) {
    if (!dfn[i])
      tarjan(i);
  }

  dfs(1);

  // 找到g的位置
  int start = g;
  int pos = find(ublock.begin(), ublock.end(), g) - ublock.begin();
  mod = ublock.size();
  cout << ublock[((k + pos) % mod + mod) % mod] << endl;

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

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

using namespace std;
int a[2000002], t[2000002] = {0};
long long k, n;
vector<int> v;

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i + 1];
  int b = 1;
  t[1] = 1;
  v.push_back(1);

  for (long long i = k; i > 0; --i) {
    b = a[b];
    v.push_back(b);

    if (t[b] == 1 && i >= n + 1) {
      long long tt;
      rep(j, v.size()) {
        if (v[j] == b) {
          tt = v.size() - 1 - j;
          break;
        }
      }
      i = (i - 1) % tt + 1;
    }
    t[b] = 1;
  }

  cout << b;

  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;
int a[200002], t[200002];
long long k, n;
vector<int> v;

int main() {
  cin >> n >> k;
  ref(i, n) cin >> a[i + 1];
  int b = 1;
  t[1] = 1;
  v.push_back(1);

  for (long long i = k; i > 0; i--) {
    v.push_back(b);
    b = a[b];

    if (t[b] == 1 && i >= n + 1) {
      int tt;
      ref(j, v.size()) {
        if (v[j] == b) {
          tt = v.size() - 1 - j;
          break;
        }
      }
      i = i % tt;
    }
    t[b] = 1;
  }

  cout << b;

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll c = 1, l = 0;
  {
    ll 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] << "\\n";
  } else {
    k -= l;
    k %= c;
    cout << s[l + k] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, k, cun = 1, i = 1, z;
  ll a[100000], b[100000] = {};
  bool f = false;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  while (b[i] < 2) {
    if (b[i] == 1) {
      f = true;
    }
    if (f) {
      cun++;
    }
    b[i]++;
    i = a[i];
  }
  z = (cun - 1) % k;
  cout << a[z] << "\\n";
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define all(v) v.begin(), v.end()
#define V vector
#define P pair
#define M map
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;

int main() {
  ll n, k;
  cin >> n >> k;
  V<ll> a(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> a[i];

  V<bool> visit(n + 1);
  V<ll> root;
  ll now = 1;
  while (1) {
    if (visit[now])
      break;
    root.push_back(now);
    visit[now] = true;
    now = a[now];
  }

  // ループのスタート地点
  int s = find(all(root), now) - root.begin();
  // ループの周期
  int r = (int)root.size() - s;

  if (k < s)
    cout << root[k] << endl; // ループに到達しない場合
  else
    cout << root[(k - s) % r + s]
         << endl; // スタート地点までを引いて剰余で求める
}


Error: 0

Fixed submission:

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

#define all(v) v.begin(), v.end()
#define V vector
#define P pair
#define M map
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;

int main() {
  ll n, k;
  cin >> n >> k;
  V<ll> a(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> a[i];

  V<ll> tmp(n + 1);
  V<ll> b(200020);
  ll next = a[1];
  tmp[1]++;
  tmp[next]++;
  ll cnt = 0, p = 1;
  while (1) {
    next = a[next];
    tmp[next]++;
    if (tmp[next] > 2)
      break;
    cnt++;
    if (tmp[next] == 2) {
      b[p] = next;
      p++;
    }
  }
  ll y = k - (cnt - p);

  ll x = y % (p - 1);
  cout << b[x + 1] << endl;
}