๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Swift

[์•Œ๊ณ ๋ฆฌ์ฆ˜/Swift] BOJ 4803 ํŠธ๋ฆฌ :: ๊ทธ๋ž˜ํ”„, DFS + Python

by Danna 2021. 5. 30.
728x90
728x90

์ด๋ฏธ์ง€๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฌธ์ œ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค!

 

๐Ÿ’ก ํ’€์ด ๋ฐฉ๋ฒ•

์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  2์ฐจ์› ๋ฐฐ์—ด graph, ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ 1์ฐจ์› ๋ฐฐ์—ด visited ์ด ํ•„์š”ํ•˜๋‹ค.

DFS ๋ฅผ ํ†ตํ•ด ํŠธ๋ฆฌ๊ฐ€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š”๋ฐ, ๋…ธ๋“œ ์ „์ฒด๋ฅผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์— ๋Œ€ํ•ด DFS ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. 

 

๐Ÿ‘€ DFS ๊ณผ์ •์—์„œ ํŠธ๋ฆฌ๊ฐ€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ?!

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ "ํŠธ๋ฆฌ๋Š” ์‚ฌ์ดํด์ด ์—†๋Š” ์—ฐ๊ฒฐ ์š”์†Œ์ด๋‹ค." ๋ฅผ ํ†ตํ•ด,, ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ ์‚ฌ์ดํด์ด ์ƒ๊ธด๋‹ค๋ฉด, ํŠธ๋ฆฌ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ž„์„ ์ฒดํฌํ•œ๋‹ค. graph ๋ฐฐ์—ด์—์„œ๋Š” ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์ „์ฒด ์ €์žฅํ•˜๋ฏ€๋กœ, ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ์‚ฌ์ดํด๋กœ ์ธ์‹ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๋…ธ๋“œ์™€ ์ด์ „ ๋…ธ๋“œ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ์ž‘์—…์„ ์ถ”๊ฐ€ํ•œ๋‹ค. (์ด์ „ ๋…ธ๋“œ == ๋‹ค์Œ ๋…ธ๋“œ) ๋Š” ์ด๋ฏธ ์ง€๋‚˜์˜จ ๊ธธ์ด๋ฏ€๋กœ DFS ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋ฌด์‹œํ•œ๋‹ค.

 

๐Ÿ‘€ ํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ๋ฒ• ?!

DFS ๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๋Š”๋ฐ, ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ๋ฐฐ์—ด ์ค‘ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด DFS ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ „์ฒด ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ณ  DFS ๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ํŠน์ • ๋…ธ๋“œ๊ฐ€ ๋Š๊ฒจ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ๋œ DFS ์™ธ์—, ์ฒ˜์Œ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” DFS ๋ฅผ ์นด์šดํŒ…ํ•˜๋ฉด ํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿค” ์ฃผ์˜ํ•ด์•ผํ•  ์ 

  • ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฐ„์„ ์— ์—ฐ๊ฒฐ๋œ ๋‘ ๋…ธ๋“œ๋Š” ๊ฐ™์„ ์ˆ˜ ์žˆ๋‹ค. (๊ฐ™์€ ๋…ธ๋“œ ์—ฐ๊ฒฐ -> ํŠธ๋ฆฌ๊ฐ€ ๋งž์Œ)
  • ํŠธ๋ฆฌ์˜ ๊ฐ„์„  ๊ฐœ์ˆ˜๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. (ํŠธ๋ฆฌ์˜ ์ •์  6๊ฐœ, ๊ฐ„์„  0๊ฐœ -> ํŠธ๋ฆฌ 6๊ฐœ)
  • ์ถœ๋ ฅ ์˜ˆ์‹œ๋ฅผ ์ž˜ ๋งž์ถ”์ž ... 

 

๐Ÿฅ Swift ์ฝ”๋“œ

func DFS(prev: Int, curr: Int, graph: [[Int]], visited: inout [Bool]) -> Bool {
    
    visited[curr] = true // ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    
    for next in graph[curr] {
        // ๋‹ค์Œ๊ณผ ์ด์ „์ด ๊ฐ™์€ ๊ฒฝ์šฐ, ๊ฐ™์€ ๊ฒฝ๋กœ๋ฅผ ๋œปํ•˜๋ฏ€๋กœ ๋ฌด์‹œ
        if next == prev {
            continue
        }
        // next != prev, ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๋ฏ€๋กœ ํŠธ๋ฆฌ๊ฐ€ ์•„๋‹˜
        if visited[next] {
            return false
        }
        // next != prev, ์‚ฌ์ดํด์ด ์—†๋Š” ๊ฒฝ์šฐ DFS ์ˆ˜ํ–‰
        // DFS ์ˆ˜ํ–‰ ๋„์ค‘ ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๋ฉด false๊ฐ€ ๋ฐ˜ํ™˜๋จ -> ์žฌ๊ท€์ ์œผ๋กœ false ๋ฐ˜ํ™˜
        if !DFS(prev: curr, curr: next, graph: graph, visited: &visited) {
            return false
        }
    }
    // DFS ์ˆ˜ํ–‰ ๋„์ค‘ ์‚ฌ์ดํด์ด ์ƒ๊ธฐ์ง€ ์•Š์œผ๋ฉด true ๋ฐ˜ํ™˜.
    return true
}

func BOJ_4803() {
    var i = 0
    while true {
        let line = readLine()!.split(separator: " ").map({Int($0)!})
        let n = line[0], m = line[1]
        if n == 0 && m == 0 { break }
        i += 1

        // ๊ทธ๋ž˜ํ”„์™€ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ๋ฐ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์ฒ˜๋ฆฌ
        var graph: [[Int]] = Array(repeating: Array<Int>(), count: n+1)
        var visited: [Bool] = Array(repeating: false, count: n+1)
        for _ in 0..<m {
            let line = readLine()!.split(separator: " ").map({Int($0)!})
            graph[line[0]].append(line[1])
            graph[line[1]].append(line[0])
        }
        
        var tree = 0 // ํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜
        for node in 1...n {
            if !visited[node] {
                // ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ DFS ์ˆ˜ํ–‰
                if DFS(prev: 0, curr: node, graph: graph, visited: &visited) {
                    tree += 1 // ์‚ฌ์ดํด์ด ์ƒ๊ธฐ์ง€ ์•Š์€ ๊ฒฝ์šฐ ํŠธ๋ฆฌ์˜ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€
                }
            }
        }
        if tree == 0 {
            print("Case \(i): No trees.")
        } else if tree == 1 {
            print("Case \(i): There is one tree.")
        } else{
            print("Case \(i): A forest of \(tree) trees.")
        }
    }
}

BOJ_4803()

 

๐Ÿ Python3 ์ฝ”๋“œ

 

algorithmFor2021/dain_song

Contribute to algorithmFor2021/dain_song development by creating an account on GitHub.

github.com

728x90
728x90