Problem p02684 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mod 1000000007 // 998244353 // 1e9 + 7
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
#define mt make_tuple
#define eps 1e-6
#define endl '\\n'
#define test                                                                   \\
  int tttt;                                                                    \\
  cin >> tttt;                                                                 \\
  while (tttt--)
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

set<int> par;
set<int> x, y;
map<int, int> mp;
void dfs(vector<vector<int>> &adj, int v, int par, int dis) {}

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n + 4];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  set<int> s;
  vector<int> v;
  int len;
  s.insert(1);
  v.pb(1);
  int beg;
  while (1) {
    int to = a[v.back()];
    if (s.count(to) != 0) {
      beg = to;
      break;
    }
    s.insert(to);
    v.pb(to);
  }
  int idx;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == beg) {
      idx = i;
      break;
    }
  }
  if (k <= idx) {
    cout << v[k] << endl;
    return;
  }
  k -= idx;
  int z = k % (v.size() - idx);
  cout << v[z + idx] << endl;
}
signed main() {
  fast_io
  // test
  {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:


// Seek knowledge from the Cradle to the Grave !!
// https://www.linkedin.com/in/syed-mujtaba

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mod 1000000007 // 998244353 // 1e9 + 7
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
#define mt make_tuple
#define eps 1e-6
#define endl '\\n'
#define test                                                                   \\
  int tttt;                                                                    \\
  cin >> tttt;                                                                 \\
  while (tttt--)
#define fast_io                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

set<int> par;
set<int> x, y;
map<int, int> mp;
void dfs(vector<vector<int>> &adj, int v, int par, int dis) {}

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n + 4];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  set<int> s;
  vector<int> v;
  int len;
  s.insert(1);
  v.pb(1);
  int beg;
  while (1) {
    int to = a[v.back()];
    if (s.count(to) != 0) {
      beg = to;
      break;
    }
    s.insert(to);
    v.pb(to);
  }
  int idx;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == beg) {
      idx = i;
      break;
    }
  }
  if (k <= idx) {
    cout << v[k] << endl;
    return;
  }
  k -= idx;
  int z = k % (n - idx);
  cout << a[v[z]] << endl;
}
signed main() {
  fast_io
  // test
  {
    solve();
  }
  return 0;
}