-
Notifications
You must be signed in to change notification settings - Fork 0
/
1389.cpp
68 lines (64 loc) · 1.28 KB
/
1389.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
61
62
63
64
65
66
67
68
#include <iostream>
#include <queue>
using namespace std;
int N,M;
int graph[101][101];
int visited[101][101];
int cal(int s){
int cnt=0;
for(int i=1; i<=N; i++){
cnt= cnt+visited[s][i];
}
return cnt;
}
void BFS(int s){
queue<int> q;
q.push(s);
while(!q.empty()){
int v=q.front();
q.pop();
for(int i=1; i<=N;i++){
if (i==v)
continue;
if(graph[v][i]==1){
if(visited[s][i]==0){
visited[s][i]=visited[s][v]+1;
q.push(i);
}
else if(visited[s][i]>visited[s][v]+1){
visited[s][i]=visited[s][v]+1;
q.push(i);
}
}
}
}
}
void init(){
for(int i=0; i<=N; i++){
fill_n(visited[i],N+1,0);
}
}
int main(){
cin >> N >> M;
int min=0;
int minv=0;
for(int i=0; i<M; i++){
int u,v; cin >> u >> v;
graph[u][v] = 1;
graph[v][u] = 1;
}
for(int i=1; i<=N; i++){
init();
BFS(i);
int bcal = cal(i);
if(min==0){
min=bcal;
minv=i;
}
else if(min>bcal){
min=bcal;
minv=i;
}
}
cout << minv;
}