-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_1389
More file actions
82 lines (61 loc) · 1.87 KB
/
BAEKJOON_1389
File metadata and controls
82 lines (61 loc) · 1.87 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
71
72
73
74
75
76
77
78
79
80
81
import org.w3c.dom.Node;
import java.io.*;
import java.math.BigInteger;
import java.sql.Array;
import java.util.*;
public class Main {
int num;
int value;
public Main(int num, int value) {
this.num = num;
this.value = value;
}
static int N;
static int M;
static int[][] board;
static boolean[] visit;
static int max = Integer.MAX_VALUE;
static int result=0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new int[N+1][N+1];
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
board[x][y] = board[y][x]=1;
}
for(int i=1; i<=N; i++){
visit = new boolean[N + 1];
BFS(i);
}
bw.write(Integer.toString(result));
bw.flush();
bw.close();
}
public static void BFS(int start){
Queue<Main> q = new LinkedList<>();
q.add(new Main(start, 0));
visit[start]=true;
int count =0;
while (!q.isEmpty()) {
Main m =q.poll();
count = count + m.value;
for(int i=1; i<=N; i++){
int num =board[m.num][i];
if (num == 1 && !visit[i]) {
visit[i]=true;
q.add(new Main(i, m.value + 1));
}
}
}
if(max > count){
max = count;
result = start;
}
}
}