Submission #88971

#TimeUsernameProblemLanguageResultExecution timeMemory
88971LkvatashidzeSpecial graph (IZhO13_specialg)C++17
0 / 100
1035 ms1548 KiB
#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 timeMemoryGrader output
Fetching results...