#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> g(n);
for (int i = 0; i < n; ++i) {
int v1;
cin >> v1;
g[i] = v1 - 1;
}
int q;
cin >> q;
while (q--) {
int ind;
cin >> ind;
if (ind == 1) {
int a1;
cin >> a1;
a1--;
g[a1] = -1;
}
else {
int a, b;
cin >> a >> b;
--a, --b;
queue<int> q;
vector<int> d(n, 1e9);
d[a] = 0;
q.push(a);
while (!q.empty()) {
int v = q.front();
q.pop();
if (g[v] == -1) continue;
if (d[g[v]] == 1e9) {
d[g[v]] = d[v] + 1;
q.push(g[v]);
}
}
if (d[b] < 1e9) {
cout << d[b] << '\n';
}
else cout << "-1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
384 KB |
Output is correct |
2 |
Correct |
16 ms |
384 KB |
Output is correct |
3 |
Correct |
26 ms |
384 KB |
Output is correct |
4 |
Correct |
40 ms |
512 KB |
Output is correct |
5 |
Correct |
20 ms |
384 KB |
Output is correct |
6 |
Correct |
173 ms |
764 KB |
Output is correct |
7 |
Correct |
183 ms |
760 KB |
Output is correct |
8 |
Correct |
196 ms |
632 KB |
Output is correct |
9 |
Correct |
171 ms |
632 KB |
Output is correct |
10 |
Correct |
201 ms |
760 KB |
Output is correct |
11 |
Execution timed out |
1093 ms |
1932 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |