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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define mem(x) memset(x, 0, sizeof(x))
#define setbits(x) __builtin_popcount(x)
#define ull unsigned long long int
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << endl;
#define cps CLOCKS_PER_SEC
#define complex                                                                \\
  cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / cps << '\\n';
#define db double
#define endl "\\n"
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os2;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef pair<ll, int> li;
inline void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const ll pi = 31;
const ll MOD[2] = {1187278649, 1149736019}; // for double hashing
const ll mod = (ll)(1e9) + 7;
ll pmod(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1LL)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return r;
}

// think recursively also
const int mxn = (int)(2e5);
vector<int> adj[mxn + 1];
bool vis[mxn + 1];
ll l = 0;
ll a[mxn + 1];
ll n, k;
vector<int> v;
vector<int> cyc;
map<ll, ll> id;
bool found = 0;
void dfs(int u, int p) {
  vis[u] = 1;

  v.pb(u);
  for (auto it : adj[u]) {

    if (vis[it]) {
      if (!found) {
        found = 1;
        cyc.pb(it);
        cyc.pb(u);
      }
      continue;
    }
    dfs(it, u);
  }
}
void solve() {

  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    adj[i].pb(a[i]);
  }
  dfs(1, 0);

  for (int i = 0; i < v.size(); i++) {

    id[v[i]] = i;
  }
  // debug(cyc.size());
  if (found) {
    int a1 = cyc[0];
    int a2 = cyc[1];
    cyc.clear();
    while (a1 != a2) {
      cyc.pb(a1);
      a1 = a[a1];
    }
    cyc.pb(a2);
    l = cyc.size();
  }
  // debug2(l,v.size());
  if (k < (ll)v.size()) {
    cout << v[k] << endl;
    return;
  }
  // debug(v.size());
  if (found) {
    k -= ((ll)v.size());
    // debug(k);
    k = k % l;
    cout << cyc[k] << endl;
  }
  return;
}
int main() {
  fast();

  // int t;
  // cin>>t;
  // for(int casee=1;casee<=t;casee++)
  { // cout<<"Case #"<<casee<<": ";
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define mem(x) memset(x, 0, sizeof(x))
#define setbits(x) __builtin_popcount(x)
#define ull unsigned long long int
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << endl;
#define cps CLOCKS_PER_SEC
#define complex                                                                \\
  cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / cps << '\\n';
#define db double
#define endl "\\n"
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os2;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef pair<ll, int> li;
inline void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const ll pi = 31;
const ll MOD[2] = {1187278649, 1149736019}; // for double hashing
const ll mod = (ll)(1e9) + 7;
ll pmod(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1LL)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return r;
}

// think recursively also
const int mxn = (int)(2e5);
vector<int> adj[mxn + 1];
bool vis[mxn + 1];
stack<int> st;
ll l = 0;
vector<int> v;
vector<int> cyc;
bool found = 0;
void dfs(int u, int p) {
  vis[u] = 1;
  st.push(u);
  v.pb(u);
  for (auto it : adj[u]) {
    if (it == p)
      continue;
    if (vis[it]) {
      if (!found) {
        found = 1;
        cyc.pb(it);
        cyc.pb(u);
      }
      continue;
    }
    dfs(it, u);
  }
  st.pop();
}
void solve() {

  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    adj[i].pb(a[i]);
  }
  dfs(1, 0);
  map<ll, ll> id;
  for (int i = 0; i < v.size(); i++) {

    id[v[i]] = i;
  }
  if (found) {
    l = id[cyc[1]] - id[cyc[0]] + 1;

    int a1 = cyc[0];
    int a2 = cyc[1];
    cyc.clear();
    while (a1 != a2) {
      cyc.pb(a1);
      a1 = a[a1];
    }
    cyc.pb(a2);
  }
  // debug(l);
  k--;
  if (k < (ll)v.size()) {
    cout << v[k] << endl;
    return;
  }
  // debug(v.size());
  k -= ((ll)v.size() - 1);
  // debug(k);
  k = k % l;
  cout << cyc[k] << endl;
  return;
}
int main() {
  fast();

  // int t;
  // cin>>t;
  // for(int casee=1;casee<=t;casee++)
  { // cout<<"Case #"<<casee<<": ";
    solve();
  }
}