diff --git "a/src/main/kotlin/hyunsoo/47week/\355\232\214\354\236\245\353\275\221\352\270\260.kt" "b/src/main/kotlin/hyunsoo/47week/\355\232\214\354\236\245\353\275\221\352\270\260.kt" new file mode 100644 index 00000000..146efdf3 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/47week/\355\232\214\354\236\245\353\275\221\352\270\260.kt" @@ -0,0 +1,82 @@ +package hyunsoo.`47week` + +import java.util.* + +/** + * + * <문제> + * [회장뽑기](https://www.acmicpc.net/problem/2660) + * + * - 아이디어 + * + * - 트러블 슈팅 + * + */ +class `전현수_회장뽑기` { + + fun solution() { + + val peopleCnt = readln().toInt() + + val graph = Array(peopleCnt + 1) { + mutableListOf() + } + + val scoreList = IntArray(peopleCnt + 1) + + while (true) { + + val (a, b) = readln().split(" ").map { it.toInt() } + if (a == -1 && b == -1) break + + graph[a].add(b) + graph[b].add(a) + + } + + for (myIndex in 1..peopleCnt) { + val visited = BooleanArray(peopleCnt + 1).apply { + this[0] = true + this[myIndex] = true + } + val queue: Queue> = LinkedList() + + queue.addAll(graph[myIndex] to 1) + + + while (queue.isNotEmpty()) { + + val (friendIndex, cost) = queue.poll() + + if (visited[friendIndex]) continue + + scoreList[myIndex] = cost + visited[friendIndex] = true + + queue.addAll( + graph[friendIndex].filter { visited[it].not() } as MutableList to cost + 1) + + } + } + + val candidateScore = scoreList.drop(1).minOf { it } + println("$candidateScore ${scoreList.count { it == candidateScore }}") + scoreList.forEachIndexed { index, score -> + if (score == candidateScore) print("$index ") + } + + } + + private fun Queue>.addAll( + pairElement: Pair, Int>, + ) { + pairElement.first.forEach { + this.add(it to pairElement.second) + } + } + +} + +fun main() { + 전현수_회장뽑기().solution() +} \ No newline at end of file