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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using plpii = std::pair<long long, std::pair<int, int>>;

using namespace std;

const long long _MOD = 1000000007;
const long long MOD = 998244353;

pii findRoop(int i, pii *a) {
  if (a[a[i].first].second >= 0) {
    return pii(a[i].first, a[i].second + 1);
  } else {
    a[a[i].first].second = a[i].second + 1;
    return findRoop(a[i].first, a);
  }
}

int solve(int i, int k, pii *a) {
  if (k) {
    return solve(a[i].first, k - 1, a);
  } else {
    return i;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  ll k;
  cin >> n >> k;
  pii *a = new pii[n];
  rep(i, n) {
    cin >> a[i].first;
    --a[i].first;
    a[i].second = -1;
  }

  a[0].second = 0;
  pii t = findRoop(0, a);

  if (k <= t.second) {
    cout << solve(0, k, a) + 1 << "\\n";
  } else {
    k -= a[t.first].second;
    k %= t.second - a[t.first].second;

    cout << solve(t.first, k, a) + 1 << "\\n";
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using plpii = std::pair<long long, std::pair<int, int>>;

using namespace std;

const long long _MOD = 1000000007;
const long long MOD = 998244353;

pii findRoop(int i, pii *a) {
  if (a[a[i].first].second >= 0) {
    return pii(a[i].first, a[i].second + 1);
  } else {
    a[a[i].first].second = a[i].second + 1;
    findRoop(a[i].first, a);
  }
}

int solve(int i, int k, pii *a) {
  if (k) {
    solve(a[i].first, k - 1, a);
  } else {
    return i;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  ll k;
  cin >> n >> k;
  pii *a = new pii[n];
  rep(i, n) {
    cin >> a[i].first;
    --a[i].first;
    a[i].second = -1;
  }

  a[0].second = 0;
  pii t = findRoop(0, a);

  if (k <= t.second) {
    cout << solve(0, k, a) << "\\n";
  } else {
    k -= a[t.first].second;
    k %= t.second - a[t.first].second;

    cout << solve(t.first, k, a) << "\\n";
  }

  return 0;
}