Q. 2660 자바 : 회장 뽑기
Q. 2660 자바 : 회장 뽑기(BFS)
문제 : boj.kr/2660
골드 5 난이도의 문제이다.
간선들이 주어지고, 모든 정점에 방문 가능한 깊이가 가장 적은 사람을 구하는 문제이다.
주의해야 할 점은 출력할 때 회장 후보를 오름차순으로 모두 출력한다는 내용 때문이 있으므로
결과값을 계산한 뒤 정렬이 한번 필요하다.
아래와 같은 방식으로 풀었다.
1. 모든 정점을 계산해야 하므로 모든 정점에 대해 bfs 탐색이 필요하다.
2. 탐색을 할 때 만약 깊이를 줄일 수 있다면 방문한 정점도 다시 탐색한다.
(이전 정점을 탐색하지 않도록 주의)
3. 탐색이 끝난 뒤 모든 정점 방문 시 깊이에 대한 값을 계산한다.
이 때 회장 후보일 수 있으므로 결과값과 현재 회장 후보의 값을 비교해 저장한다.
4. 깊이의 최소값과 일치하는 정점을 구하고, 정렬해서 출력한다.
시간 복잡도의 경우 아래와 같다.
1. 모든 정점에 대한 bfs 탐색 - O(N * N^2 = N^3)
2. 각 정점마다 bfs 탐색 뒤 깊이 계산 - O(N)
3. 모든 정점 탐색 뒤 최소값 계산 - O(N)
4. 최소값과 일치하는 정점 계산 뒤 정렬 - O(N log N)
5. 출력 - O(N)
복잡해 보이지만, 모든 시간 복잡도는 연관이 없으므로 더해서 계산하면 된다.
그래서 시간복잡도는 약 O(N^3)이 나와 시간복잡도는 긴 편 이지만,
N의 최대값이 50이므로 시간초과는 걱정하지 않아도 된다.
풀 때 골드5문제 치고 꽤나 구현량이 많아서 찾아 보니
플로이드-와샬 알고리즘을 이용해서 푸는 문제라고 한다.
그리고 이 알고리즘을 사용해도 시간 복잡도가 O(N^3)이다.
풀이는 아래와 같다.
import java.io.*;
import java.util.*;
public class Main {
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();
static int n, min;
static ArrayList<Integer>[] Graph;
static int[] Cnt;
static void input(){
n = scan.nextInt();
min = n;
Graph = new ArrayList[n + 1];
Cnt = new int[n + 1];
for (int i = 1; i <= n; i++) {
Graph[i] = new ArrayList<>();
}
int v1 = scan.nextInt(), v2 = scan.nextInt();
while (v1 != -1 && v2 != -1) {
Graph[v1].add(v2);
Graph[v2].add(v1);
v1 = scan.nextInt();
v2 = scan.nextInt();
}
}
static void pro() {
for (int i = 1; i <= n; i++) {
bfs(i);
}
ArrayList<Integer> res = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (Cnt[i] == min) res.add(i);
}
sb.append(min).append(' ').append(res.size()).append('\n');
Collections.sort(res);
for (int i = 0; i < res.size(); i++) {
sb.append(res.get(i)).append(' ');
}
System.out.println(sb);
}
static void bfs(int x) {
Queue<int[]> que = new LinkedList<>();
que.add(new int[] {x, -1, 0});
int[] depth = new int[n + 1];
while (!que.isEmpty()) {
int[] cur = que.poll(); // cur, prev, depth
if (depth[cur[0]] == 0 || depth[cur[0]] > cur[2]){
depth[cur[0]] = cur[2];
}
for (int y: Graph[cur[0]]) {
if (y == cur[1]) continue;
if (depth[y] == 0 || depth[y] > (cur[2] + 1)){
que.add(new int[] {y, cur[0], cur[2] + 1});
}
}
}
HashSet<Integer> hashSet = new HashSet<>();
for (int i = 1; i <= n; i++) {
if (x == i) continue;
hashSet.add(depth[i]);
}
Cnt[x] = hashSet.size();
min = Math.min(min, Cnt[x]);
}
public static void main(String[] args) throws IOException {
input();
pro();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}