답안 #1009206

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1009206 2024-06-27T10:02:07 Z bornag 특수한 그래프 (IZhO13_specialg) C++14
0 / 100
1000 ms 1112 KB
#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;
			
			if(graph[a-1] == -1) {
				cout << -1 << '\n';
				continue;
			}
			cout << bfs(a-1, b-1) << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 3 ms 500 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 101 ms 484 KB Output is correct
7 Correct 111 ms 496 KB Output is correct
8 Correct 123 ms 480 KB Output is correct
9 Correct 100 ms 344 KB Output is correct
10 Correct 129 ms 476 KB Output is correct
11 Execution timed out 1039 ms 1112 KB Time limit exceeded
12 Halted 0 ms 0 KB -