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

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 2000000
#define MOD 1000000007
#define endl "\\n"
map<lli, lli> cycle;
lli N, K;
lli arr[MAX];
lli cp;
lli modul;
lli powa(lli k) {
  lli mask = 1;
  for (lli i = 0; i < k; i++)
    mask *= 2;
  return mask;
}
void DFS1(lli n, lli k) {
  if (cycle[n]) {
    cp = n;
    modul = k - cycle[n];
  } else {
    cycle[n] = k;
    DFS1(arr[n], k + 1);
  }
}
void DFS(lli n, lli k) {
  // cout<<n<<endl;
  if (n == cp) {
    k %= modul;
    //  cout<<"Now K is "<<k<<" "<<modul<<endl;
  }

  if (k == 0) {
    cout << n;
    return;
  } else {
    DFS(arr[n], k - 1);
  }
}
int main() {
  //  ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
  lli t;
  t = 1;

  while (t--) {
    cin >> N >> K;
    for (lli i = 1; i <= N; i++)
      cin >> arr[i];
    DFS1(1, 1);
    // cout<<cp<<" "<<modul<<endl;
    DFS(1, K);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define MAX 2000000
#define MOD 1000000007
#define endl "\\n"
map<lli, lli> cycle;
lli N, K;
lli arr[MAX];
lli cp;
lli modul;
lli powa(lli k) {
  lli mask = 1;
  for (lli i = 0; i < k; i++)
    mask *= 2;
  return mask;
}
lli DFS1(lli n, lli k) {
  if (cycle[n]) {
    cp = n;
    return k - cycle[n];
  } else {
    cycle[n] = k;
    DFS1(arr[n], k + 1);
  }
}
void DFS(lli n, lli k) {
  // cout<<n<<endl;
  if (n == cp) {
    k %= modul;
    //  cout<<"Now K is "<<k<<" "<<modul<<endl;
  }

  if (k == 0) {
    cout << n;
    return;
  } else {
    DFS(arr[n], k - 1);
  }
}
int main() {
  //  ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
  lli t;
  t = 1;

  while (t--) {
    cin >> N >> K;
    for (lli i = 1; i <= N; i++)
      cin >> arr[i];
    modul = DFS1(1, 1);
    // cout<<cp<<" "<<modul<<endl;
    DFS(1, K);
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long int N, K, a, f, start = 1, n = 0;
  vector<long long int> A;
  map<long long int, long long int> mp;
  cin >> N >> K;
  n = K;
  for (int i = 0; i < N; i++) {
    cin >> a;
    A.push_back(a);
  }
  int next = A[0];
  int prev = 1;
  for (long long int i = 1; i <= K; i++) {
    if (next != 1 && mp[next] == 0) {
      mp[next] = i;
      prev = next;
    } else {
      f = i - mp[next];
      // cout << f << endl;
      // cout << mp[next] << endl;
      n = (K - mp[next]) % f;
      // cout << K - mp[next] << endl;
      if (n == 0) {
        start = next;
      } else {
        start = next;
      }
      // cout << n << endl;
      break;
    }
    next = A[next - 1];
  }
  // cout << start << " " << f << endl;
  for (int i = 0; i < n; i++) {
    start = A[start - 1];
  }
  cout << start << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  long long int N, K, a, f, start = 0, n = K;
  vector<long long int> A;
  map<long long int, long long int> mp;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a;
    A.push_back(a);
  }
  int next = A[0];
  int prev = 1;
  for (long long int i = 1; i < K; i++) {
    if (next != 1 && mp[next] == 0) {
      mp[next] = i;
      prev = next;
    } else {
      f = i - mp[next];
      // cout << f << endl;
      // cout << mp[next] << endl;
      n = (K - mp[next]) % f;
      // cout << K - mp[next] << endl;
      if (n == 0) {
        start = next;
      } else {
        start = next;
      }
      // cout << n << endl;
      break;
    }
    next = A[next - 1];
  }
  // cout << start << " " << f << endl;
  for (int i = 0; i < n; i++) {
    start = A[start - 1];
  }
  cout << start << 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;
using ll = long long;
using P = pair<int, int>;

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: 0

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 INF = 1001001001;
using P = pair<int, int>;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<bool> visit(n);
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    a[i] = tmp - 1;
  }

  ll pos = 0;
  ll ans = 0;
  ll cntA = 0;
  rep(i, n) {
    if (visit[pos]) {
      break;
    }
    visit[pos] = true;
    ans = a[pos];
    pos = ans;
    cntA++;
    // cout << ans + 1 << endl;
  }
  if (cntA == n) {
    cout << ans + 1 << endl;
    return 0;
  }
  ll cntb = 0;
  vector<ll> ansT;
  rep(i, n) visit[i] = false;
  rep(i, n) {
    if (visit[pos]) {
      break;
    }
    visit[pos] = true;
    ans = a[pos];
    pos = ans;
    ansT.push_back(pos);
    cntb++;
  }
  k = k - (cntA - cntb) - 1;
  int mo = k % cntb;
  // reverse(ansT.begin(), ansT.end());
  //  cout << mo << " " << n << " "<< cntA << "" << endl;
  //  rep(i, cntb) cout << ansT[i] + 1;
  //  cout << endl;
  cout << ansT[mo] + 1 << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int a[maxn];
bool b[maxn];
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = 0;
  }
  int now = 1;
  queue<int> q;
  while (b[now] == 0) {
    b[now] = 1;
    q.push(now);
    now = a[now];
  }
  while (q.front() != now && k > 0)
    k--, q.pop();
  if (k == 0) {
    printf("%d\\n", q.front());
    return 0;
  }
  vector<int> v;
  while (!q.empty()) {
    v.push_back(q.front());
    q.pop();
  }
  int len = v.size();
  k = k % len;
  printf("%d\\n", v[k]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int a[maxn];
bool b[maxn];
int main() {
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = 0;
  }
  int now = 1;
  queue<int> q;
  while (b[now] == 0) {
    b[now] = 1;
    q.push(now);
    now = a[now];
  }
  while (q.front() != now)
    k--, q.pop();
  vector<int> v;
  while (!q.empty()) {
    v.push_back(q.front());
    q.pop();
  }
  int len = v.size();
  k = k % len;
  printf("%d\\n", v[k]);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef long double ld;

int main() {
  ll n, k, t;
  cin >> n >> k;
  t = k;
  set<ll> s;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }

  ll g = LLONG_MAX;
  ll a = arr[0];
  s.insert(a);
  vector<ll> path;
  path.push_back(a);

  while (g--) {
    a = arr[a - 1];
    if (s.find(a) != s.end()) {
      path.push_back(a);
      break;
    }
    path.push_back(a);
    s.insert(a);
  }
  for (ll i = 0; i < path.size(); i++) {
    if (path[i] == a) {
      vector<ll> rep;
      for (ll z = i + 1; z < path.size(); z++) {
        rep.push_back(path[z]);
      }
      if (k <= (i + 1)) {
        cout << path[k - 1];
      } else {
        ll temp = ((k - i - 1) % rep.size());
        if (temp == 0) {
          cout << rep[rep.size() - 1];
        } else {
          cout << rep[temp - 1];
        }
      }

      break;
    }
  }
}

Error: WA

Fixed submission:

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

typedef long long ll;
typedef long double ld;

int main() {
  ll n, k, t;
  cin >> n >> k;
  t = k;
  set<ll> s;
  vector<ll> arr(n);
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }

  ll a = arr[0];
  s.insert(a);
  vector<ll> path;
  path.push_back(a);

  while (t--) {
    a = arr[a - 1];
    if ((s.find(a) != s.end()) || (a == 1)) {
      path.push_back(a);
      break;
    }
    path.push_back(a);
    s.insert(a);
  }
  if (a == 1) {
    cout << path[(k % path.size()) - 1];
  } else {
    for (ll i = 0; i < path.size(); i++) {
      if (path[i] == a) {
        vector<ll> rep;
        for (ll z = i + 1; z < path.size(); z++) {
          rep.push_back(path[z]);
        }
        ll temp = ((k - i - 1) % rep.size());
        if (temp == 0) {
          cout << rep[rep.size() - 1];
        } else {
          cout << rep[temp - 1];
        }
        break;
      }
    }
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

ll N, K;
vector<ll> A;

void solve() {
  vector<ll> r;
  map<ll, ll> seen;
  rep(i, N) { seen[i] = -1; }

  ll idx = 0;
  ll len = 0;

  while (true) {
    if (seen[idx] != -1) {
      break;
    }
    if (K <= len) {
      cout << idx + 1 << endl;
      return;
    }

    seen[idx] = len;
    r.push_back(idx);
    idx = A.at(idx);
    len++;
  }

  ll d;
  if (K - seen[idx] >= 0) {
    if (len - seen[idx] <= 0) {
      d = seen[idx] + (K - seen[idx]);
    } else {
      d = seen[idx] + (K - seen[idx]) % (len - seen[idx]);
    }
  } else {
    d = K;
  }
  cout << r.at(d) + 1 << endl;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--; // 0-indexed
  }

  solve();

  return 0;
}


Error: 0

Fixed submission:

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

ll N, K;
vector<ll> A;

void solve() {
  vector<ll> r;
  map<ll, ll> seen;
  rep(i, N) { seen[i] = -1; }

  ll idx = 0;
  ll len = 0;

  while (true) {
    if (seen[idx] != -1) {
      break;
    }
    if (K <= len) {
      cout << idx + 1 << endl;
      return;
    }

    seen[idx] = len;
    r.push_back(idx);
    idx = A.at(idx);
    len++;
  }

  ll d;
  if (K - idx > 0) {
    d = (K - idx) % (len - idx);
  } else {
    d = K;
  }
  cout << r.at(d) + 1 << endl;
}

int main() {
  cin >> N >> K;
  A.resize(N);
  rep(i, N) {
    cin >> A.at(i);
    A.at(i)--; // 0-indexed
  }

  solve();

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int n, k, a;
  cin >> n >> k;
  std::deque<pair<long long int, int>> deq;
  for (int i = 0; i < n; i++) {
    cin >> a;
    deq.emplace_back(pair(a, 0));
  }
  long long int tmp = 0;
  std::deque<long long int> loop;
  while (1) {
    deq[tmp].second++;
    if (deq[tmp].second == 2) {
      loop.emplace_back(deq[tmp].first);
    } else if (deq[tmp].second == 3) {
      break;
    }
    tmp = deq[tmp].first - 1;
  }
  std::deque<long long int> not_loop;
  tmp = 0;
  for (int i = 0; i < n; i++) {
    if (deq[tmp].second == 1) {
      not_loop.emplace_back(deq[tmp].first);
    } else {
      break;
    }
    tmp = deq[tmp].first - 1;
  }
  loop.emplace_front(loop.back());
  loop.pop_back();
  tmp = 0;
  if (k <= not_loop.size()) {
    for (int i = 0; i < k; i++) {
      tmp = deq[tmp].first - 1;
    }
    cout << tmp + 1 << endl;
  } else {
    k -= not_loop.size();
    cout << loop[k % loop.size()] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, a;
  cin >> n;
  long long int k;
  cin >> k;
  std::deque<int> origin;
  for (int i = 0; i < n; i++) {
    cin >> a;
    origin.emplace_back(a);
  }
  if (n < k) {
    std::deque<int> check(n + 1, 0);
    std::deque<int> numbers;
    long long int count = 0;
    int j = 0, flag = 0;
    while (1) {
      if (check[origin[j]] == 0) {
        check[origin[j]] = 1;
        count++;
      } else if (check[origin[j]] == 1) {
        check[origin[j]] = 2;
        if (origin[j] != 1) {
          numbers.emplace_back(origin[j]);
        } else {
          numbers.emplace_front(origin[j]);
          flag = 1;
        }
        // cout<<origin[j]<<endl;
        count--;
      } else {
        break;
      }
      j = origin[j] - 1;
    }
    // cout<<count<<endl;
    k -= count;
    long long int size = numbers.size();
    if (flag == 0) {
      cout << numbers[k % size - 1] << endl;
    } else {
      cout << numbers[k % size] << endl;
    }
  } else {
    int hoge = 0;
    while (1) {
      k--;
      hoge = origin[hoge] - 1;
      // cout<<hoge<<endl;
      if (k == 0) {
        cout << origin[hoge] << endl;
        break;
      }
    }
  }
}
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;
using ll = long long;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  // ll
  // n,m,x,k=0,sum=0,a=0,b=0,c=0,d=0,ans=10000000,index=1,index_0=1,index_ans=1;
  ll MOD = 1000000007;
  ll INF = 1LL << 60;
  string S = "", T = "", U = "";
  ll n, 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;
  }
  // cout << ans << endl;
}

Error: WA

Fixed submission:

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

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;

  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v);
  }
}

int main() {
  LL n, m, x, k = 0, sum = 0, a = 0, b = 0, c = 0, d = 0, ans = 10000000,
              index = 1, index_0 = 1, index_ans = 1;
  LL MOD = 1000000007;
  LL INF = 1LL << 60;
  LL C[100001], A[200002], R[200002] = {0}, O[100001];
  string S = "", T = "", U = "";
  cin >> n >> k;
  for (LL i = 1; i <= n; i++) {
    cin >> A[i];
  }
  while (true) {
    if (R[A[index]] == 0) {
      R[A[index]] = 1;
      a++;
      index = A[index];
    } else {
      index_0 = A[index];
      while (index_0 != index) {
        index_0 = A[index_0];
        c++;
        a++;
      }
      a = a - c;
      b = a - c;
      d = (k - b) % c;
      break;
    }
  }
  if (k <= b) {
    for (LL i = 0; i < b; i++) {
      index_ans = A[index_ans];
    }
  } else {
    index_ans = index_0;
    for (LL i = 0; i <= d; i++) {
      index_ans = A[index_ans];
    }
  }
  cout << index_ans << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long int
#define vi vector<int>
#define vvi vector<vi>
#define read(a)                                                                \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(a)                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";
#define pb push_back
#define ins insert
#define pql priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define pqlv priority_queue<vi>
#define pqsv priority_queue<vi, vvi, greater<vi>>
#define endl '\\n'

int n, k, val;
vi a, vis, v;

void dfs(int i) {
  if (vis[i]) {
    val = i;
    return;
  }
  v.pb(i);
  vis[i] = 1;
  dfs(a[i - 1]);
}

signed main() {
  int n, k;
  cin >> n >> k;
  a.resize(n);
  read(a);
  vis.assign(n + 1, 0);
  dfs(1);
  vi x;
  bool has = false;
  for (int i : v) {
    if (i == val)
      has = true;
    if (has) {
      x.pb(i);
    }
  }
  int diff = v.size() - x.size();
  if (k <= diff) {
    cout << v[k];
  } else {
    k -= diff;
    int ans = x[k % x.size()];
    cout << ans;
  }
}


Error: WA

Fixed submission:

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

#define mem(dp, a) memset(dp, a, sizeof dp)
#define ninja                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb(x) push_back(x)
#define all(c) c.begin(), c.end()
#define int long long
#define ff first
#define ss second
#define nl '\\n'

set<int> adj[200005];
bool vis[200005];
vector<int> path;

bool dfs(int s, int p) {
  vis[s] = 1;
  path.pb(s);
  for (auto i : adj[s]) {
    if (!vis[i]) {
      if (dfs(i, s))
        return true;
    }
    if (vis[i] and i != p) {
      path.pb(i);
      return true;
    }
  }
  return false;
}
int32_t main() {
  ninja int n, k;
  cin >> n >> k;
  int x;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    adj[i].insert(x);
  }
  mem(vis, false);
  bool cycle = dfs(1, -1);
  //   for(auto i:path)
  //    cout<<i<<" ";
  // cout<<endl;

  int i = 0, j = path.size() - 1;
  while (i < j) {
    if (path[i] == path[j]) {
      break;
    }
    i++;
  }
  int temp = k;
  k = k - i;
  if (k < n) {
    cout << path[k];
    return 0;
  }
  // cout<<j<<' '<<i<<nl;
  int final_dest = (k) % (j - i) + i;
  // if(i>0)
  //    path.erase(path.begin(),path.begin()+i-1);

  // cout<<final_dest<<endl;
  cout << path[final_dest];
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

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

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

  vector<int> per(1);
  vector<bool> flag(n);
  int now = 0;
  int start_t = 0;
  flag[0] = true;
  rep(i, n) {
    now = a[now];
    if (flag[now]) {
      start_t = now;
      break;
    }
    per.push_back(now);
    flag[now] = true;
  }

  int start = 0;
  rep(i, per.size()) {
    if (per[i] == start_t)
      start = i;
  }

  vector<int> roop;
  for (int i = start; i < per.size(); ++i) {
    roop.push_back(per[i]);
  }

  if (k <= start) {
    cout << per[k] + 1 << endl;
  } else {
    k -= start;
    k %= roop.size();
    cout << roop[k] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

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

  vector<int> roop(1);
  int now = 0;
  int start = 0;
  rep(i, n) {
    if (flag[a[now]]) {
      start = now;
      break;
    }
    roop.push_back(a[now]);
    flag[a[now]] = true;
    now = a[now];
  }

  vector<int> troop;
  for (int i = start; i < roop.size(); ++i) {
    troop.push_back(roop[i]);
  }

  int dif = roop.size() - troop.size();
  if (troop[troop.size() - 1] == 0) {
    troop.pop_back();
    dif = 0;
  }

  if (k <= dif) {
    cout << roop[k] + 1 << endl;
  } else {
    k -= dif;
    k %= troop.size();
    cout << troop[k] + 1 << endl;
  }

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

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

int main() {
  int N,
      i = 1, roop = 0, current,
      a = 0; // N:町の数 K:テレポートする回数 roop:ループしているグラフの要素数
  long long K;
  cin >> N >> K;
  N++;
  vector<int> tel(N); // 町関係を保存する
  vector<int> check(N, 0);
  for (int i = 1; i < N; i++) {
    cin >> tel.at(i);
  }
  current = 1;
  while (1) {
    if (check.at(current) != 0) {
      roop = i - check.at(current);
      break;
    } else if (check.at(current) == 0) {
      a++;
      check.at(current) = i;
    }
    current = tel.at(current);
    i++;
  }
  if (K > a) {
    K = (K - a) % roop;
    for (int i = 0; i < K; i++) {
      current = tel.at(current);
    }
  } else {
    current = 1;
    for (i = 0; i < K; i++) {
      current = tel.at(current);
    }
  }
  cout << current << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long N,
      roop = 0, current,
      a = 0; // N:町の数 K:テレポートする回数 roop:ループしているグラフの要素数
  long long K;
  cin >> N >> K;
  N++;
  vector<long> tel(N); // 町関係を保存する
  vector<int> check(N, 0);
  for (int i = 1; i < N; i++) {
    cin >> tel.at(i);
  }
  current = 1;
  for (int i = 0; i < K; i++) {
    if (check.at(current) == 1) {
      roop++;
    } else if (check.at(current) == 2) {
      break;
    } else if (check.at(current) == 0) {
      a++;
    }
    check.at(current)++;
    current = tel.at(current);
  }
  K = (K - a) % roop;
  for (int i = 1; i < K; i++) {
    current = tel.at(current);
  }
  cout << current << endl;
}