๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S1] ๋ฐฑ์ค€ 1389๋ฒˆ ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™

by immgga 2024. 8. 5.

์ถœ์ฒ˜: unsplash.com

 

์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™(1389๋ฒˆ)

Silver 1

#๊ทธ๋ž˜ํ”„ ์ด๋ก  #๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ #๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ #์ตœ๋‹จ ๊ฒฝ๋กœ #ํ”Œ๋กœ์ด๋“œ–์›Œ์…œ

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

๊ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ์‚ฌ์šฉ์ž์™€ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋Œ๋ฉด์„œ ๋ช‡ ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ ์›ํ•˜๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‹ฟ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ ์˜ˆ์ œ 1์„ ์ด์šฉํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ๋จผ์ € ๊ตฌ์„ฑํ•ด ์ฃผ๊ฒ ๋‹ค. ๊ทธ๋ž˜ํ”„์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์ง„ 1-1

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.math.min

private var graph = arrayOf<MutableList<Int>>()
private var visited = booleanArrayOf()
private var result = 0

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (user, relationships) = readLine().split(" ").map { it.toInt() }
    graph = Array(user + 1) { mutableListOf() }
    visited = BooleanArray(user + 1)

    for (i in 0 until relationships) {
        val relationship = readLine().split(" ").map { it.toInt() }
        graph[relationship[0]].add(relationship[1])
        graph[relationship[1]].add(relationship[0])
    }

    var currentMin = Int.MAX_VALUE
    var answer = 0
    for (i in user downTo 1) {
        var totalRes = 0
        visited.fill(false)
        result = 0

        for (f in 1 .. user) {
            visited.fill(false)
            result = 0

            if (i == f) continue
            find(i, f)

            totalRes += result
        }

        val newMin = min(currentMin, totalRes)

        // ๋” ์ ์€ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ.
        if (newMin < currentMin) {
            currentMin = newMin
            answer = i
        }
        // ํ˜„์žฌ i์˜ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜์˜ ํ•ฉ๊ณผ ํ˜„์ œ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ‘๊ณผ ๊ฐ™๋‹ค๋ฉด i๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ.
        else if (totalRes == currentMin) {
            answer = i
        }
    }

    println(answer)
}

private fun find(user: Int, toFind: Int) {
    val queue: Queue<List<Int>> = LinkedList()
    visited[user] = true

    for (u in graph[user]) {
        if (u == toFind) {
            result = 1
            return
        }
        queue.add(listOf(u, 1))
    }

    while (queue.isNotEmpty()) {
        val data = queue.poll()
        val checkUser = data[0]
        val level = data[1]

        for (g in graph[checkUser]) {
            if (!visited[g]) {
                if (g == toFind) {
                    result += level + 1
                    return
                }
                queue.add(listOf(g, level + 1))
                visited[g] = true
            }
        }
    }
}

์˜ˆ์ œ ์ž…๋ ฅ 1์—์„œ ๋‘ ๋ฒˆ์งธ ์ค„์˜ ์ž…๋ ฅ์ด 1 3์ธ๋ฐ, ๊ทธ๋ž˜์„œ 1์— ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜์—๋Š” 3์„ ๋„ฃ๊ณ , 3์— ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜์—๋Š” 1์„ ๋„ฃ์–ด์„œ ์„œ๋กœ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด ์ค€๋‹ค. ์ด๋ฅผ ๋ชจ๋“  ์ž…๋ ฅ๊ฐ’์˜ ๊ด€๊ณ„๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ๋  ๊ฒƒ์ด๋‹ค.

์ด์ œ ์œ„ ๊ทธ๋ž˜ํ”„์—์„œ ์œ ์ € 5๊ฐ€ ์œ ์ € 2๊นŒ์ง€ ์ด๋™ํ•˜๋ ค๋ฉด 5 -> 4 -> 3 -> 2๋กœ ์ด 3๋ฒˆ์˜ ์ด๋™์ด ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹ต์€ 3์ด ๋œ๋‹ค.

 

์ด์ œ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•ด ์กฐ๊ฑด์„ ํƒ์ƒ‰ํ•ด ๋ณด์ž.์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— min() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.๋˜ํ•œ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋„ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ 1์˜ ๊ฒฝ์šฐ์—๋„ ์ตœ์†Œ๊ฐ€ 3, 4๋กœ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๋Š” 5๋กœ ๋˜‘๊ฐ™๋‹ค.์ด ๊ฒฝ์šฐ์—๋Š” ๋” ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.์ด ๋ฌธ์ œ์˜ ๋‹ต์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์ค€์ด ๋˜๋Š” ์‚ฌ๋žŒ๊ณผ ์ฐพ์•„์•ผ ํ•˜๋Š” ์‚ฌ๋žŒ ์ด 2๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ฐพ์•„์•ผ ํ•˜๋Š” ์‚ฌ๋žŒ์€ ๊ธฐ์ค€์ด ๋˜๋Š” ์‚ฌ๋žŒ๊ณผ ๊ฐ™์œผ๋ฉด ์•ˆ ๋œ๋‹ค.2์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ์ค€์ด ๋˜๋Š” ์‚ฌ๋žŒ๊ณผ ์ฐพ์•„์•ผ ํ•˜๋Š” ์‚ฌ๋žŒ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.๋‘ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” continue๋กœ ๋„˜๊ธฐ๋ฉด ๋œ๋‹ค.๋˜ํ•œ ๋ฐ˜๋ณต ์ˆœ์„œ๋ฅผ ํฐ ์ˆ˜์—์„œ ์ž‘์€ ์ˆ˜๋กœ ๊ฐ€๋„๋ก ๋ฐ˜๋ณตํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ ์œ„์— ์–ธ๊ธ‰ํ–ˆ๋˜ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๊ฐ€ ๊ฐ™์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฌ ๋ช…์ผ ๋•Œ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์‚ฌ๋žŒ์„ ๊ตฌํ•˜๊ธฐ ์‰ฝ๋‹ค.bfs๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

 

์ž์„ธํ•œ bfs ๋กœ์ง์€ ๋ฌธ์ œ ํ’€์ด์—์„œ ์ ์–ด ๋ณด๊ฒ ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.math.min

private var graph = arrayOf<MutableList<Int>>()
private var visited = booleanArrayOf()
private var result = 0

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (user, relationships) = readLine().split(" ").map { it.toInt() }
    graph = Array(user + 1) { mutableListOf() }
    visited = BooleanArray(user + 1)

    for (i in 0 until relationships) {
        val relationship = readLine().split(" ").map { it.toInt() }
        graph[relationship[0]].add(relationship[1])
        graph[relationship[1]].add(relationship[0])
    }

    var currentMin = Int.MAX_VALUE
    var answer = 0
    for (i in user downTo 1) {
        var totalRes = 0
        visited.fill(false)
        result = 0

        for (f in 1 .. user) {
            visited.fill(false)
            result = 0

            if (i == f) continue
            find(i, f)

            totalRes += result
        }

        val newMin = min(currentMin, totalRes)

        // ๋” ์ ์€ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ.
        if (newMin < currentMin) {
            currentMin = newMin
            answer = i
        }
        // ํ˜„์žฌ i์˜ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜์˜ ํ•ฉ๊ณผ ํ˜„์ œ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ‘๊ณผ ๊ฐ™๋‹ค๋ฉด i๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ.
        else if (totalRes == currentMin) {
            answer = i
        }
    }

    println(answer)
}

private fun find(user: Int, toFind: Int) {
    val queue: Queue<List<Int>> = LinkedList()
    visited[user] = true

    for (u in graph[user]) {
        if (u == toFind) {
            result = 1
            return
        }
        queue.add(listOf(u, 1))
    }

    while (queue.isNotEmpty()) {
        val data = queue.poll()
        val checkUser = data[0]
        val level = data[1]

        for (g in graph[checkUser]) {
            if (!visited[g]) {
                if (g == toFind) {
                    result += level + 1
                    return
                }
                queue.add(listOf(g, level + 1))
                visited[g] = true
            }
        }
    }
}

 

๋ฌธ์ œ ํ’€์ด

graph(2์ฐจ์› List)์— ์ž…๋ ฅ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

2์ฐจ์› ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ index๊ฐ’์€ user์˜ ๋ฒˆํ˜ธ๋ฅผ ๋œปํ•˜๊ณ , ์ฒซ ๋ฒˆ์งธ index ์•ˆ์˜ ๋ฆฌ์ŠคํŠธ๋Š” user์™€ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ user๋“ค์ด๋‹ค.

์‚ฌ์ง„ 1-2

graph์˜ ๊ตฌ์กฐ๋Š” ์‚ฌ์ง„ 1-2์™€ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค.

visited๋Š” 1์ฐจ์› ๋ฆฌ์ŠคํŠธ๋กœ ์„ค์ •ํ•ด ์ค€๋‹ค. ๊ด€๊ณ„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋Œ๋ฉด์„œ ๋ช‡ ๋ฒˆ์งธ user๋ฅผ ๊ฑฐ์ณค๋Š”์ง€ ํ™•์ธํ•ด ์ฃผ๋Š” ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„ 1-2์˜ 4๋ฒˆ์งธ ๊ฐ’์€ 1, 5, 3์ด ๋“ค์–ด๊ฐ„ ๋ฆฌ์ŠคํŠธ์ธ๋ฐ, ์—ฌ๊ธฐ์—์„œ 3์ด ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž๋ผ๋ฉด 3์„ ์ œ์™ธํ•œ 1, 5๋งŒ ๋ณด๋ฉด ๋œ๋‹ค.

 

์ด์ œ 2์ค‘ ๋ฐ˜๋ณต์„ ๋Œ๋ฉด์„œ bfs๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

bfs์—์„œ๋Š” ํ˜„์žฌ ์‚ฌ์šฉ์ž(i)์™€ ๋งŒ๋‚˜์•ผ ํ•˜๋Š” ์‚ฌ์šฉ์ž(f)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ฒ˜์Œ์— queue์— graph์˜ i๋ฒˆ์งธ index์— ๋‚˜์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ target์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ queue์— ๋„ฃ์–ด ์ค€๋‹ค. ๋งŒ์•ฝ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐพ์•„์•ผ ํ•˜๋Š” ๊ฐ’์ธ f๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ 1๋กœ ์ €์žฅํ•˜๊ณ  ๊บผ๋„ ๋œ๋‹ค.

 

queue๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด ์ค€๋‹ค. 

๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ๊ฐœ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค. ๋งŒ์•ฝ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐพ์•„์•ผ ํ•˜๋Š” ๊ฐ’์ธ onFind๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด result๋ฅผ 1๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

์•„๋‹ˆ๋ฉด queue๊ฐ€  ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

bfs๋ฅผ ๋Œ๋ฉด์„œ ์‚ฌ์šฉ์ž ๋ฒˆํ˜ธ๋ฅผ ํ†ต๊ณผํ–ˆ๋Š”์ง€ ์•„๋ž˜์˜ ์กฐ๊ฑด์œผ๋กœ ํ™•์ธํ•œ๋‹ค.

if (!visited[g])

์ด ์กฐ๊ฑด์ด ๋งŒ์กฑํ•˜๊ณ  ์ฐพ์•„์•ผ ํ•˜๋Š” toFind์™€ ๊ฐ™์œผ๋ฉด result๋ฅผ queue์—์„œ ๋ฝ‘์•„์˜จ ๋ฐ์ดํ„ฐ์˜ level์˜ + 1๋งŒํผ ํ•ด์ค€ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

๋งˆ์ง€๋ง‰์— queue์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•œ๋‹ค.

 

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ๋ฅผ ์งœ์„œ bfs๋ฅผ ๋Œ๋ฆฌ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์‚ฌ์šฉ์ž๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์ž˜ ํ‘œํ˜„ํ•ด์•ผ bfs๋ฅผ ์“ธ ๋•Œ ํŽธํ•˜๋‹ค. ๊ทธ๊ฒƒ๋งŒ ์กฐ๊ธˆ ์ƒ๊ฐํ•˜๋ฉด ํ’€๊ธฐ ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค.

728x90