https://www.acmicpc.net/problem/2660

import java.util.*;
import java.io.*;


class Node implements Comparable<Node>{
    int idx;
    int max;

    Node(int idx, int max)
    {
        this.idx = idx;
        this.max = max;
    }

    @Override
    public int compareTo(Node now)
    {
        if (this.max == now.max) {
            return this.idx - now.idx;
        }

        return this.max - now.max;
    }

 
}

public class Main {

    static int N;
    static int[][] dist;

    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        N = Integer.parseInt(br.readLine());

        dist = new int[N + 1][N + 1];
        
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N; j++) {
                if (i == j) {
                    dist[i][j] = 0;
                }else{
                    dist[i][j] = 1000000 + 1;
                }
            }
        }

        while (true) {
            StringTokenizer st = new StringTokenizer(br.readLine());

            int s = Integer.parseInt(st.nextToken());

            int e = Integer.parseInt(st.nextToken());

            if (s == -1 && e == -1) {
                break;
            }

            dist[s][e] = 1;
            dist[e][s] = 1;

        }


        for (int k = 1; k <= N; k++) {
            for (int i = 1; i <= N; i++) {
                for (int j = 1; j <= N; j++) {
                    if (dist[i][j] > dist[i][k] + dist[k][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }

        PriorityQueue<Node>pq = new PriorityQueue<>();

        for (int i = 1; i <= N; i++) {

            int max = 0;

            for (int j = 1; j <= N; j++) {
                if(dist[i][j] != 1000000 + 1) max = Math.max(max, dist[i][j]);
            }

            pq.offer(new Node(i, max));
           
        }
       
        int max = 0;
        int index = 0;
        if (!pq.isEmpty()) {
            index = pq.peek().idx;
            max = pq.poll().max;
        }

        ArrayList<Integer>list = new ArrayList<>();

        list.add(index);

        while (!pq.isEmpty()) {
            
            if (pq.peek().max == max) {
                list.add(pq.poll().idx);
            }else{
                break;
            }

        }

        System.out.println(max+" "+list.size());

        for(int next : list)
        {
            System.out.print(next+" ");
        }
    }
}

0개의 댓글