-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
55 lines (50 loc) · 804 Bytes
/
2644.cpp
File metadata and controls
55 lines (50 loc) · 804 Bytes
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
// 2644. 촌수계산
// 2019.05.20
// BFS
#include<iostream>
#include<queue>
using namespace std;
int relation[101][101];
bool visit[101];
int dist[101];
int main()
{
int n, a, b, m;
cin >> n >> a >> b >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
relation[x][y] = 1;
relation[y][x] = 1;
}
queue<int> q;
q.push(a);
visit[a] = true;
dist[a] = 0;
while (!q.empty())
{
int front = q.front();
q.pop();
for (int i = 1; i <= n; i++)
{
// 관계가 있고 방문하지 않았을 때
if (relation[front][i] == 1 && !visit[i])
{
visit[i] = true;
q.push(i);
dist[i] = dist[front] + 1;
}
}
}
// 촌수를 계산할 수 없을 때 -1 출력
if (dist[b] == 0)
{
cout << -1 << endl;
}
else
{
cout << dist[b] << endl;
}
return 0;
}