-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1238.cpp
60 lines (55 loc) · 976 Bytes
/
1238.cpp
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
56
57
58
59
60
// 1238. Contact
// 2019.07.22
#include<iostream>
#include<queue>
using namespace std;
int relation[101][101]; // 관계 저장
int visit[101]; // visit[i] : i가 연락받은 시간
int main()
{
for (int testCase = 1; testCase <= 10; testCase++)
{
fill(visit, visit + 101, 0);
for (int i = 0; i <= 100; i++)
{
fill(relation[i], relation[i] + 101, 0);
}
int a, b;
cin >> a >> b;
for (int i = 0; i < a; i += 2)
{
int x, y;
cin >> x >> y;
relation[x][y] = 1;
}
// BFS
queue<int> q;
q.push(b);
visit[b] = 0;
while (!q.empty())
{
int front = q.front();
q.pop();
for (int i = 1; i <= 100; i++)
{
if (relation[front][i] == 1 && visit[i] == 0)
{
q.push(i);
visit[i] = visit[front] + 1;
}
}
}
int max = 0;
int ans = 0;
for (int i = 1; i <= 100; i++)
{
if (max <= visit[i])
{
max = visit[i];
ans = i;
}
}
cout << "#" << testCase << " " << ans << endl;
}
return 0;
}