1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+ import java .util .*;
5+
6+ public class Main {
7+
8+ private static int [][] arr ;
9+ private static int min = Integer .MAX_VALUE , ans = -1 ;
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
13+ StringTokenizer st = new StringTokenizer (br .readLine ());
14+
15+ int N = Integer .parseInt (st .nextToken ());
16+ int M = Integer .parseInt (st .nextToken ());
17+
18+ arr = new int [N + 1 ][N + 1 ];
19+
20+ for (int i = 0 ; i < M ; i ++) {
21+ st = new StringTokenizer (br .readLine ());
22+
23+ int a = Integer .parseInt (st .nextToken ());
24+ int b = Integer .parseInt (st .nextToken ());
25+
26+ arr [a ][b ] = arr [b ][a ] = 1 ;
27+ }
28+
29+ for (int i = 1 ; i <= N ; i ++) {
30+ bfs (i );
31+ }
32+
33+ System .out .println (ans );
34+ br .close ();
35+ }
36+
37+ private static void bfs (int index ) {
38+ Queue <Node > q = new LinkedList <>();
39+ boolean [] visited = new boolean [arr .length ];
40+ q .add (new Node (index , 0 ));
41+ visited [index ] = true ;
42+
43+ int res = 0 ;
44+
45+ while (!q .isEmpty ()) {
46+ Node now = q .poll ();
47+
48+ for (int i = 1 ; i < arr .length ; i ++) {
49+ if (!visited [i ] && arr [now .index ][i ] == 1 ) {
50+ q .add (new Node (i , now .count + 1 ));
51+ visited [i ] = true ;
52+ res += now .count + 1 ;
53+ }
54+ }
55+ }
56+
57+ if (res < min ) {
58+ min = res ;
59+ ans = index ;
60+ }
61+ }
62+
63+ static class Node {
64+ int index ;
65+ int count ;
66+
67+ public Node (int index , int count ) {
68+ this .index = index ;
69+ this .count = count ;
70+ }
71+ }
72+ }
0 commit comments