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

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

#define pi 3.1415926536
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\\n"
#define ws " "
#define sz size()
#define len length()
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define print1(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define print2(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i;                                                                 \\
  cout << "\\n"
#define print3(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << nl;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

/*############################# DSU ##############################*/
vll parent(1e5), Size(1e5), Rank(1e5);

void make_set(ll v) {
  parent[v] = v;
  Size[v] = 1;
  // Rank[v] = 0;
}

int find_set(ll v) {
  return (parent[v] == v) ? v : parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (Size[a] < Size[b])
      swap(a, b);
    // if(Rank[a] < Rank[b])
    //     swap(a, b);
    parent[b] = a;
    Size[a] += Size[b];
    // if(Rank[a] == Rank[b])
    //     Rank[a]++;
  }
}
/*############################# DSU ##############################*/

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

vll pwx(32);
void cal(ll x) {
  pwx[0] = 1;
  rep(i, 1, 32) pwx[i] = x * pwx[i - 1];
}

/* ################################################################ */

ll n, k, comp = 0, x, startnode = -1, endnode = -1;

vector<vector<ll>> adj(2e5 + 5);
vector<bool> visited(2e5 + 5);
vector<ll> nodes;
bool cycel = 0;

void dfs(ll u, ll p) {
  visited[u] = true;
  nodes.PB(u);
  for (auto v : adj[u]) {
    if (!visited[v]) {
      dfs(v, u);
    } else {
      startnode = v;
      endnode = u;
      return;
    }
  }
}

void solve() {
  cin >> n >> k;
  rep(i, 1, n + 1) {
    cin >> x;
    adj[i].PB(x);
  }
  visited.assign(n + 1, false);
  dfs(1, -1);
  if ((startnode == nodes.front() && endnode == nodes.back()) || k < nodes.sz ||
      nodes.sz == 1)
    cout << nodes[k % nodes.sz] << nl;
  else {
    ll startind = -1, endind = -1;
    for (int i = 0; i < nodes.sz; i++) {
      if (nodes[i] == startnode) {
        startind = i;
      }
      if (nodes[i] == endnode) {
        endind = i;
      }
    }
    k -= startind;
    cout << nodes[startind + k % (endind - startind + 1)] << nl;
  }
}

int main() {
  // boost;
  cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}

Error: 0

Fixed submission:

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

#define pi 3.1415926536
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\\n"
#define ws " "
#define sz size()
#define len length()
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define print1(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define print2(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i;                                                                 \\
  cout << "\\n"
#define print3(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << nl;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

/*############################# DSU ##############################*/
vll parent(1e5), Size(1e5), Rank(1e5);

void make_set(ll v) {
  parent[v] = v;
  Size[v] = 1;
  // Rank[v] = 0;
}

int find_set(ll v) {
  return (parent[v] == v) ? v : parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (Size[a] < Size[b])
      swap(a, b);
    // if(Rank[a] < Rank[b])
    //     swap(a, b);
    parent[b] = a;
    Size[a] += Size[b];
    // if(Rank[a] == Rank[b])
    //     Rank[a]++;
  }
}
/*############################# DSU ##############################*/

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

vll pwx(32);
void cal(ll x) {
  pwx[0] = 1;
  rep(i, 1, 32) pwx[i] = x * pwx[i - 1];
}

/* ################################################################ */

ll n, k, comp = 0, x, startnode = -1, endnode = -1;

vector<vector<ll>> adj(2e5 + 5);
vector<bool> visited(2e5 + 5);
vector<ll> nodes;

void dfs(ll u, ll p) {
  visited[u] = true;
  nodes.PB(u);
  for (auto v : adj[u]) {
    if (!visited[v]) {
      dfs(v, u);
    } else if (v != u) {
      startnode = v;
      endnode = u;
      return;
    }
  }
}

void solve() {
  cin >> n >> k;
  rep(i, 1, n + 1) {
    cin >> x;
    adj[i].PB(x);
  }
  visited.assign(n + 1, false);
  dfs(1, -1);
  if ((startnode == nodes.front() && endnode == nodes.back()) || k < nodes.sz)
    cout << nodes[k % nodes.sz] << nl;
  else {
    ll startind = -1, endind = -1;
    for (auto i : nodes) {
      if (i == startnode) {
        startind = i;
      }
      if (i == endnode) {
        endind = i;
      }
    }
    k -= startind;
    cout << nodes[startind + k % (endind - startind)] << nl;
  }
}

int main() {
  // boost;
  cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}