Submission #1217258

#TimeUsernameProblemLanguageResultExecution timeMemory
1217258lopkusSpecial graph (IZhO13_specialg)C++20
0 / 100
1096 ms884 KiB
#include <bits/stdc++.h>

signed main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n + 1);
  for(int i = 1; i <= n; i++) {
    std::cin >> a[i];
  }
  int q;
  std::cin >> q;
  while(q--) {
    int type;
    std::cin >> type;
    if(type == 1) {
      int u;
      std::cin >> u;
      a[u] = 0;
    }
    else {
      int u, v;
      std::cin >> u >> v;
      int ans = 0;
      int f = 0;
      while(u != v && a[u] && f <= n) {
        u = a[u];
        ans += 1;
        f += 1;
      }
      if(u == v) {
        std::cout << ans << "\n";
      }
      else {
        std::cout << - 1 << "\n";
      }
    }
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...