1
+ import java .util .*;
2
+ import java .io .*;
3
+
4
+ public class YJ_2660 {
5
+ static class Node {
6
+ int index ;
7
+ int distance ;
8
+ Node (int index , int distance ) {
9
+ this .index = index ;
10
+ this .distance = distance ;
11
+ }
12
+ }
13
+
14
+ static List <List <Integer >> graph = new ArrayList <>();
15
+ static int MAX = 51 ; //★무작정 MAX_VALUE 넣지않기
16
+ static int n ;
17
+
18
+ public static void main (String [] args ) throws IOException {
19
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
20
+ n = Integer .parseInt (br .readLine ());
21
+
22
+ //다익스트라 초기화
23
+ for (int i =0 ; i <n +1 ; i ++){
24
+ graph .add (new ArrayList <>());
25
+ }
26
+
27
+ while (true ){
28
+ StringTokenizer st = new StringTokenizer (br .readLine ());
29
+ int to = Integer .parseInt (st .nextToken ());
30
+ int from = Integer .parseInt (st .nextToken ());
31
+ if (to == -1 && from == -1 ){
32
+ break ;
33
+ }
34
+ graph .get (to ).add (from );
35
+ graph .get (from ).add (to );
36
+ }
37
+
38
+ getCandidates ();
39
+ }
40
+
41
+ static void getCandidates (){
42
+ int min = MAX ;
43
+ List <Integer > candidates = new ArrayList <>();
44
+
45
+ //한 사람에 대한 모든 간선의 연결수 구하기
46
+ for (int i =1 ; i <n +1 ; i ++){
47
+ //회원 별 연결점수 중 최고점을 저장
48
+ int connection = findConnection (i );
49
+
50
+ //회장 후보: 점수가 가장 낮은 사람
51
+ if (connection < min ){
52
+ candidates = new ArrayList <>();
53
+ candidates .add (i );
54
+ min = connection ;
55
+ }else if (connection == min ) {
56
+ candidates .add (i );
57
+ }
58
+ }
59
+
60
+ System .out .printf ("%d %d%n" ,min ,candidates .size ());
61
+ candidates .forEach (candidate -> System .out .printf ("%d " ,candidate ));
62
+ }
63
+
64
+ static int findConnection (int index ){
65
+ PriorityQueue <Node > queue = new PriorityQueue <>((n1 ,n2 ) -> n2 .distance - n1 .distance );
66
+ queue .offer (new Node (index ,0 ));
67
+
68
+ int [] table = new int [n +1 ];
69
+ Arrays .fill (table ,MAX );
70
+ table [index ] = 0 ;
71
+
72
+ while (!queue .isEmpty ()){
73
+ Node person = queue .poll ();
74
+
75
+ for (int friend : graph .get (person .index )){
76
+ if (table [friend ] > table [person .index ]+1 ){ //★다음 간선까지의 거리 1
77
+ table [friend ] = table [person .index ]+1 ;
78
+ //★거리를 누적해서 다음 순회에서 친구의 친구 > 친구의 친구의 친구 > ... 연결을 만듬
79
+ queue .offer (new Node (friend ,table [friend ])); //★Node(연결된 친구,거리)
80
+ }
81
+ }
82
+ }
83
+
84
+ int max = 0 ;
85
+ for (int i =1 ; i <n +1 ; i ++){
86
+ max = Math .max (max ,table [i ]);
87
+ }
88
+ return max ;
89
+ }
90
+ }
0 commit comments