-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1389.cpp
More file actions
70 lines (60 loc) · 1.38 KB
/
1389.cpp
File metadata and controls
70 lines (60 loc) · 1.38 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
#define SETTING ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define MAX 10000
using namespace std ;
vector< vector<int> > graph(101) ;
bool visited[101] ;
int answer[101] ;
int N, M ;
void InputSetting()
{
int start, end ;
cin >> N >> M ;
for(int i = 1 ; i <= M ; i++)
{
cin >> start >> end ;
graph[start].push_back(end) ;
graph[end].push_back(start) ;
}
}
int KevinBacon(int idx)
{
memset(visited, 0, sizeof(bool) * 101) ;
visited[idx] = 1 ;
int bacon_num = 0;
queue <int> que ;
for(int it : graph[idx])
{
if(!visited[it]) que.push(it) ;
visited[it] = 1 ;
}
int loop = 1 ;
while(!que.empty())
{
int size = que.size() ;
for(int j = 0 ; j < size ; j++)
{
int curr = que.front() ;
que.pop() ;
bacon_num += loop ;
for(int it : graph[curr])
if(!visited[it])
{
que.push(it) ;
visited[it] = 1 ;
}
}
loop++ ;
}
return bacon_num ;
}
int main()
{
SETTING ;
InputSetting() ;
fill(&answer[0], &answer[101], MAX) ;
for(int i = 1 ; i <= N ; i++)
answer[i] = KevinBacon(i) ;
cout << min_element(&answer[1], &answer[N+1])-&answer[0] << endl ;
}