# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1009197 | bornag | Special graph (IZhO13_specialg) | C++14 | 2 ms | 348 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, m;
int graph[maxn];
int dis[maxn];
int bfs(int s, int e){
for(int i = 0; i < n; i++)
dis[i] = -1;
queue<pair<int, int>> q;
q.push({s, 0});
while(!q.empty()){
int node = q.front().first;
int d = q.front().second;
q.pop();
if(dis[node] != -1) continue;
dis[node] = d;
if(graph[node] != -1 && dis[graph[node]] == -1) q.push({graph[node], d+1});
}
return dis[e];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
cin >> graph[i]; graph[i]--;
}
cin >> m;
while(m--){
int typ; cin >> typ;
if(typ == 1){
int x; cin >> x;
graph[x-1] = -1;
} else {
int a, b; cin >> a >> b;
cout << bfs(a-1, b-1);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |