-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
55 lines (53 loc) · 1.32 KB
/
Copy path2644.cpp
File metadata and controls
55 lines (53 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
void bfs(int start,int end, int* count,bool* flag, vector<int> graph[], bool check[]){
queue<int> q;
q.push(start);
check[start] = true;
while(!q.empty()){
int size = q.size();
*count = *count+1;
while(size--){
int tmp = q.front();
q.pop();
for(int i=0; i<graph[tmp].size(); i++){
// 방문하지 않았다면
if(check[graph[tmp][i]] == false){
// 큐에 넣어주고 방문했음을 표시한다.
if(graph[tmp][i] == end){
*flag =true;
return;
}
q.push(graph[tmp][i]);
check[graph[tmp][i]] = true;
}
}
}
}
}
int main(){
int n,a,b,m,x,y;
cin >> n;
cin >> a >> b;
cin >> m;
vector<int> family[n+1];
bool check[n+1];
fill(check,check+n+1,false);
for(int i =0; i< m;i++){
cin >> x >> y;
family[x].push_back(y);
family[y].push_back(x);
}
int count =0;
bool flag = false;
bfs(a,b,&count, &flag,family,check);
if(flag == false){
cout << -1;
return 0;
}
cout<<count;
return 0;
}