#include <iostream>
#include <vector>
#include <unordered_set>
int path(std::vector<int> &vec) {
int x, y, c = 0; std::cin >> x >> y;
std::unordered_set<int> visited;
while (x != y) {
x = vec[x];
if (visited.find(x) != visited.end() || x == -1) return -1;
visited.insert(x);
c++;
}
return c;
}
int main() {
std::ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
int n; std::cin >> n;
std::vector<int> vec(n+1); for (int i = 1; i <= n; i++) std::cin >> vec[i];
int m; std::cin >> m;
bool ok = (n <= 2e3) && (m <= 2e4);
for (int i = 0; i < m; i++) {
int act; std::cin >> act;
if (act == 1) {
int x; std::cin >> x;
vec[x] = -1;
continue;
}
if (ok)
std::cout << path(vec) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
211 ms |
504 KB |
Output is correct |
7 |
Correct |
243 ms |
344 KB |
Output is correct |
8 |
Correct |
248 ms |
500 KB |
Output is correct |
9 |
Correct |
209 ms |
344 KB |
Output is correct |
10 |
Correct |
282 ms |
504 KB |
Output is correct |
11 |
Incorrect |
9 ms |
604 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |