# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
88971 | Lkvatashidze | Special graph (IZhO13_specialg) | C++17 | 1035 ms | 1548 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>
#define ll long long
using namespace std;
int n, m;
int g[100005];
int BFS(int,int);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int k=1; k<=n; k++) {
int x;
cin >> x;
g[k]=x;
}
cin >> m;
while (m--) {
int type;
cin >> type;
if (type==1) {
int x;
cin >> x;
g[x]=-1;
}
else{
int a, b;
cin >> a >> b;
int ans=BFS(a,b);
cout << ans << '\n';
}
}
return 0;
}
int BFS (int s, int fin) {
vector < bool > used(n+1,0);
vector < int > d(n+1,0);
queue < int > Q;
Q.push(s);
used[s]=true;
while (!Q.empty()) {
int v=Q.front();
Q.pop();
if (g[v]==-1) continue;
if (used[g[v]]) continue;
d[g[v]]=d[v]+1;
Q.push(g[v]);
used[g[v]]=true;
}
if (used[fin]==0)
return -1;
return d[fin];
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |