#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 < 2 * n) {
u = a[u];
ans += 1;
f += 1;
}
if(u == v) {
std::cout << ans << "\n";
}
else {
std::cout << - 1 << "\n";
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |