# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
440700 | parsabahrami | Schools (IZhO13_school) | C++17 | 2093 ms | 63876 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e6 + 10, MOD = 1e9 + 7;
set<int> adj[N]; int dp[N], nxt[N], n, q;
void BFS(int st) {
queue<int> Q;
fill(dp, dp + n + 1, MOD);
Q.push(st), dp[st] = 0;
while (SZ(Q)) {
int v = Q.front(); Q.pop();
for (int u : adj[v])
if (dp[u] > dp[v] + 1) dp[u] = dp[v] + 1, Q.push(u);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &nxt[i]);
for (int i = 1; i <= n; i++) {
if (nxt[i]) adj[i].insert(nxt[i]);
}
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int t, v, u; scanf("%d%d", &t, &v);
if (t < 2) adj[v].erase(nxt[v]);
else {
scanf("%d", &u);
BFS(v);
printf("%d\n", dp[u] > n ? -1 : dp[u]);
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |