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

/*input
6 727202214173249351
6 5 2 5 3 2


*/
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>

using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gc getchar_unlocked
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define ms(a, x) memset(a, x, sizeof(a));
#define endl "\\n"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
// void dfs(int u, int par);
const int mod = 1000000007;
const int N = 1000000001;
const int MAX = 1e5 + 1;

vector<bool> visited(2 * MAX, false);
int32_t main() {
  IOS;
  int n, k;
  cin >> n >> k;
  int arr[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  vector<int> ans;
  set<int> s;
  int in = 1;
  // ans.push_back(1);
  // s.insert(in);
  int x = k;
  // if(s.find(arr[in])!=s.end())
  // cout<<arr[in]<<endl;
  // while(x>0){
  // 	cout<<in<<" ";
  // 	in = arr[in];
  // 	x--;
  // }
  // cout<<endl;
  in = 1;
  while (s.find(in) == s.end()) {
    // cout<<arr[in]<<" ";
    ans.push_back(in);
    s.insert(in);
    in = arr[in];
  }
  // for(auto &i:ans)cout<<i<<" ";
  //  	cout<<endl;
  // cout<<in<<endl;
  in = find(ans.begin(), ans.end(), in) - ans.begin();
  int p = ans.size() - in;
  // if(in>1)
  //     p = (ans.size())-in;
  // cout<<p<<endl;
  // int r = k%p;
  if (k < in) {
    cout << ans[k] << endl;
    return 0;
  }
  k -= in;
  // deb(k);
  int r = k % p;
  // cout<<r<<endl;
  // deb(in);
  cout << ans[r + in] << endl;
  return 0;
}


Error: 0

Fixed submission:

/*input
4 5
3 2 4 1

*/
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>

using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gc getchar_unlocked
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define ms(a, x) memset(a, x, sizeof(a));
#define endl "\\n"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
// void dfs(int u, int par);
const int mod = 1000000007;
const int N = 1000000001;
const int MAX = 1e5 + 1;

vector<bool> visited(2 * MAX, false);
int32_t main() {
  IOS;
  int n, k;
  cin >> n >> k;
  int arr[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  vector<int> ans;
  set<int> s;
  int in = 1;
  // ans.push_back(1);
  // s.insert(in);
  int x = k;
  // if(s.find(arr[in])!=s.end())
  // cout<<arr[in]<<endl;
  // while(x>0){
  // 	cout<<in<<" ";
  // 	in = arr[in];
  // 	x--;
  // }
  // cout<<endl;
  in = 1;
  while (s.find(in) == s.end()) {
    // cout<<arr[in]<<" ";
    ans.push_back(in);
    s.insert(in);
    in = arr[in];
  }
  // for(auto &i:ans)cout<<i<<" ";
  //  	cout<<endl;
  // cout<<in<<endl;
  int p = ans.size();
  if (in > 1)
    p = (ans.size()) - in;
  // cout<<p<<endl;
  // int r = k%p;
  if (k < in) {
    cout << ans[k] << endl;
    return 0;
  }
  k -= in;
  // deb(k);
  int r = k % p;
  // cout<<r<<endl;
  // deb(in);
  cout << ans[r + in] << endl;
  return 0;
}