-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c34ef8c
commit adfce42
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: ::: ::: */ | ||
/* Problem Number: 2660 :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ahchjang <boj.kr/u/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<ArrayList<Integer>> 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<Integer> 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<Integer> 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<ArrayList<Integer>> graph, Queue<Integer> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 2660번: 회장뽑기 - <img src="https://static.solved.ac/tier_small/11.svg" style="height:20px" /> Gold V | ||
|
||
<!-- performance --> | ||
|
||
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.--> | ||
|
||
<!-- end --> | ||
|
||
## 문제 | ||
|
||
[문제 링크](https://boj.kr/2660) | ||
|
||
|
||
<p>월드컵 축구의 응원을 위한 모임에서 회장을 선출하려고 한다. 이 모임은 만들어진지 얼마 되지 않았기 때문에 회원 사이에 서로 모르는 사람도 있지만, 몇 사람을 통하면 모두가 서로 알 수 있다. 각 회원은 다른 회원들과 가까운 정도에 따라 점수를 받게 된다.</p> | ||
|
||
<p>예를 들어 어느 회원이 다른 모든 회원과 친구이면, 이 회원의 점수는 1점이다. 어느 회원의 점수가 2점이면, 다른 모든 회원이 친구이거나 친구의 친구임을 말한다. 또한 어느 회원의 점수가 3점이면, 다른 모든 회원이 친구이거나, 친구의 친구이거나, 친구의 친구의 친구임을 말한다.</p> | ||
|
||
<p>4점, 5점 등은 같은 방법으로 정해진다. 각 회원의 점수를 정할 때 주의할 점은 어떤 두 회원이 친구사이이면서 동시에 친구의 친구사이이면, 이 두사람은 친구사이라고 본다.</p> | ||
|
||
<p>회장은 회원들 중에서 점수가 가장 작은 사람이 된다. 회장의 점수와 회장이 될 수 있는 모든 사람을 찾는 프로그램을 작성하시오.</p> | ||
|
||
|
||
|
||
## 입력 | ||
|
||
|
||
<p>입력의 첫째 줄에는 회원의 수가 있다. 단, 회원의 수는 50명을 넘지 않는다. 둘째 줄 이후로는 한 줄에 두 개의 회원번호가 있는데, 이것은 두 회원이 서로 친구임을 나타낸다. 회원번호는 1부터 회원의 수만큼 붙어 있다. 마지막 줄에는 -1이 두 개 들어있다.</p> | ||
|
||
|
||
|
||
## 출력 | ||
|
||
|
||
<p>첫째 줄에는 회장 후보의 점수와 후보의 수를 출력하고, 두 번째 줄에는 회장 후보를 오름차순으로 모두 출력한다.</p> | ||
|
||
|
||
|
||
## 소스코드 | ||
|
||
[소스코드 보기](Main.java) |