-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj1389.cpp
62 lines (54 loc) · 1.08 KB
/
boj1389.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
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define MAX 1e9
using namespace std;
int N, M;
vector<int> map[101];
int bacon[101];
bool visited[101];
queue<int> q;
void bfs(int st) {
q.push(st);
visited[st] = true;
while (!q.empty()) {
int f = q.front();
q.pop();
for (int i = 0; i < map[f].size(); i++) {
int next = map[f][i];
if (!visited[next]) {
bacon[next] = bacon[f] + 1;
q.push(next);
visited[next] = true;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> M;
int a, b;
for (int i = 0; i < M; i++) {
cin >> a >> b;
map[a].push_back(b);
map[b].push_back(a);
}
int min = MAX;
int res = -1;
for (int i = 1; i <= N; i++) {
bfs(i);
for (int j = 1; j <= N; j++) cout << bacon[j] << ' ';
cout << endl;
int sum = 0;
for (int j = 1; j <= N; j++) sum += bacon[j];
if (min > sum) {
min = sum;
res = i;
}
memset(visited, false, sizeof(visited));
memset(bacon, 0, sizeof(bacon));
}
cout << res;
return 0;
}