diff --git "a/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/Main.java" "b/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/Main.java" new file mode 100644 index 0000000..a9f9e3f --- /dev/null +++ "b/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/Main.java" @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: ::: ::: */ +/* Problem Number: 2660 :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ahchjang +#+ +#+ +#+ */ +/* +#+ +#+ +#+ */ +/* https://boj.kr/2660 #+# #+# #+# */ +/* Solved: 2024/09/30 21:46:58 by ahchjang ### ### ##.kr */ +/* */ +/* ************************************************************************** */ + +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + ArrayList> graph = new ArrayList<>(); + StringTokenizer st; + int[] member_score = new int[N + 1]; + + for (int i = 0; i <= N; i++) { + graph.add(new ArrayList<>()); + } + + while (true) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + if (a == -1 && b == -1) + break; + graph.get(a).add(b); + graph.get(b).add(a); + } + + int candidate_score = 51; + + for (int start = 1; start <= N; start++) { + int[] score = new int[N + 1]; + Queue queue = new LinkedList<>(); + Arrays.fill(score, -1); + score[start] = 0; + queue.add(start); + member_score[start] = bfs(graph, queue, score); + candidate_score = Math.min(candidate_score, member_score[start]); + } + + ArrayList answer = new ArrayList<>(); + int cnt = 0; + + for (int i = 1; i <= N; i++) { + if(member_score[i] == candidate_score){ + cnt++; + answer.add(i); + } + } + + System.out.println(candidate_score + " " + cnt); + for (Integer candidate : answer) { + System.out.print(candidate + " "); + } + + } + + static int bfs(ArrayList> graph, Queue queue, int[] score) { + int max = 0; + while (!queue.isEmpty()) { + int cur = queue.poll(); + for (int next : graph.get(cur)) { + if (score[next] == -1) { + score[next] = score[cur] + 1; + queue.add(next); + max = score[next]; + } + } + } + return max; + } +} \ No newline at end of file diff --git "a/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/README.md" "b/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/README.md" new file mode 100644 index 0000000..ec1a8d3 --- /dev/null +++ "b/2660\353\262\210\357\274\232 \355\232\214\354\236\245\353\275\221\352\270\260/README.md" @@ -0,0 +1,40 @@ +# 2660번: 회장뽑기 - Gold V + + + + + + + +## 문제 + +[문제 링크](https://boj.kr/2660) + + +

월드컵 축구의 응원을 위한 모임에서 회장을 선출하려고 한다. 이 모임은 만들어진지 얼마 되지 않았기 때문에 회원 사이에 서로 모르는 사람도 있지만, 몇 사람을 통하면 모두가 서로 알 수 있다. 각 회원은 다른 회원들과 가까운 정도에 따라 점수를 받게 된다.

+ +

예를 들어 어느 회원이 다른 모든 회원과 친구이면, 이 회원의 점수는 1점이다. 어느 회원의 점수가 2점이면, 다른 모든 회원이 친구이거나 친구의 친구임을 말한다. 또한 어느 회원의 점수가 3점이면, 다른 모든 회원이 친구이거나, 친구의 친구이거나, 친구의 친구의 친구임을 말한다.

+ +

4점, 5점 등은 같은 방법으로 정해진다. 각 회원의 점수를 정할 때 주의할 점은 어떤 두 회원이 친구사이이면서 동시에 친구의 친구사이이면, 이 두사람은 친구사이라고 본다.

+ +

회장은 회원들 중에서 점수가 가장 작은 사람이 된다. 회장의 점수와 회장이 될 수 있는 모든 사람을 찾는 프로그램을 작성하시오.

+ + + +## 입력 + + +

입력의 첫째 줄에는 회원의 수가 있다. 단, 회원의 수는 50명을 넘지 않는다. 둘째 줄 이후로는 한 줄에 두 개의 회원번호가 있는데, 이것은 두 회원이 서로 친구임을 나타낸다. 회원번호는 1부터 회원의 수만큼 붙어 있다. 마지막 줄에는 -1이 두 개 들어있다.

+ + + +## 출력 + + +

첫째 줄에는 회장 후보의 점수와 후보의 수를 출력하고, 두 번째 줄에는 회장 후보를 오름차순으로 모두 출력한다.

+ + + +## 소스코드 + +[소스코드 보기](Main.java) \ No newline at end of file