-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1389
82 lines (57 loc) · 1.42 KB
/
1389
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
82
import java.util.*;
class pair{
int x,d;
public pair(int x, int depth) {
this.x = x;
this.d = depth;
}
}
public class Main {
public static int[][] rel;
public static int[] checked;
public static int N,M;
public static int min=Integer.MAX_VALUE;
public static int mp;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt(); // 유저의 수
M = sc.nextInt(); // 관계의 수
rel = new int[N+1][N+1];
checked = new int[N+1];
for(int i=0; i<M; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
rel[x][y] = rel[y][x] = 1;
}
//체크
for(int i=1; i<N+1; i++) {
if(min>findkb(i)) {
min = findkb(i);
mp = i;
}
}
System.out.println(mp);
sc.close();
}
public static int findkb(int a) {
Queue <pair> q = new LinkedList<pair>();
int sum = 0;
for(int i=1;i<N+1; i++)
checked[i]=0;
checked[a] = 1;
q.add(new pair(a,1));
while(!q.isEmpty()) {
pair n = q.poll();
for(int i=1; i<N+1; i++) {
if(rel[n.x][i]==1 && checked[i]!=1) {
checked[i] = 1;
q.add(new pair(i,n.d+1));
sum += n.d;
//체크
// System.out.println(n.x+"과"+i+"확인" + n.d);
}
}
}
return sum;
}
}