Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 16947๋ฒˆ - ์„œ์šธ ์ง€ํ•˜์ฒ  2ํ˜ธ์„  (BFS, DFS ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ)

๊ฐ์ž ๐Ÿฅ” 2022. 6. 30. 17:31
๋ฐ˜์‘ํ˜•

 

๐ŸŸฃ ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/16947

 

16947๋ฒˆ: ์„œ์šธ ์ง€ํ•˜์ฒ  2ํ˜ธ์„ 

์ฒซ์งธ ์ค„์— ์—ญ์˜ ๊ฐœ์ˆ˜ N(3 ≤ N ≤ 3,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์—ญ๊ณผ ์—ญ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ตฌ๊ฐ„์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ™์€ ๊ตฌ๊ฐ„์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๊ณ , ์—ญ์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ

www.acmicpc.net

 

๐ŸŸฃ ๋ฌธ์ œ ํ’€์ด

์šฐ์„  ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์–ด๋ ค์› ๋‹ค. ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ + ๊ทธ๋ฆฌ๊ณ  ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ–ˆ๋‹ค.

์šฐ์„ , '์ˆœํ™˜์„  = ์‚ฌ์ดํด'์„ ์ฐพ์•„์•ผํ•œ๋‹ค. 

์ด ์ง€ํ•˜์ฒ  ๋…ธ์„ ๋„๋กœ ๋ณด๋ฉด, '์‹ ๋„๋ฆผ'์—ญ์€ ์ด๋ฏธ ์ˆœํ™˜์„  (์‚ฌ์ดํด) ๋‚ด๋ถ€์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆœํ™˜์„ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ 0์ด๋‹ค. ํ•˜์ง€๋งŒ '๊นŒ์น˜์‚ฐ'์—ญ์ธ ๊ฒฝ์šฐ, ์ˆœํ™˜์„ ์ด ์•„๋‹ˆ๋ผ ์ง€์„ ์— ์†ํ•œ๋‹ค. ์ด ๊นŒ์น˜์‚ฐ์—ญ์€ '์ˆœํ™˜์„ '๊นŒ์ง€ 4๊ฐœ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค.

์ด์ฒ˜๋Ÿผ ์ˆœํ™˜์„ ๊ณผ ์ง€์„ ์„ ๊ตฌ๋ณ„ํ•˜์—ฌ, ์ง€์„ ์— ์œ„์น˜ํ•œ ์—ญ์ธ ๊ฒฝ์šฐ ์ˆœํ™˜์„ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.


๊ทธ๋Ÿผ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ํ•ด๋‹น์—ญ์ด ์ˆœํ™˜์„ ์— ์œ„์น˜ํ•˜๋Š”์ง€, ์ง€์„ ์— ์œ„์น˜ํ•˜๋Š”์ง€ ์•Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์„ ๋จผ์ € ํŒ๋‹จํ•ด์ค˜์•ผํ•œ๋‹ค.

๐ŸŸฃ ์ˆœํ™˜์„  ์ฐพ๊ธฐ → DFS ์ด์šฉ

์ˆœํ™˜์„ ์ด๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์ด ์žˆ๋‹ค. '์‹œ์ž‘์ '์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„์™€์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

1๋ฒˆ์—ญ → 2๋ฒˆ์—ญ → 3๋ฒˆ์—ญ → 1๋ฒˆ์—ญ 

์ด๋ ‡๊ฒŒ ๋œ๋‹ค๋ฉด, ์œ„ 1,2,3๋ฒˆ์—ญ์€ ๋ชจ๋‘ ์ˆœํ™˜์„ ์— ์œ„์น˜ํ•œ๋‹ค.

โญ๏ธ ํ•˜์ง€๋งŒ 1๋ฒˆ์—ญ → 2๋ฒˆ์—ญ  → 1๋ฒˆ์—ญ ์ด๋ ‡๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ, ์ˆœํ™˜์„ ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜์—†๋‹ค. ๊ทธ๋ƒฅ ์•ž์œผ๋กœ ๊ฐ”๋‹ค๊ฐ€ ๋’ค๋กœ ๋Œ์•„๊ฐ„๊ฒƒ์ด๊ธฐ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‘๋ฒˆ ์ด์ƒ ๋‹ค๋ฅธ์—ญ์— ๋ฐฉ๋ฌธํ•ด์•ผ๋งŒ! ์ˆœํ™˜์„ ์— ํ•ด๋‹น๋œ๋‹ค.โญ๏ธ ์ด ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋‚ด๋Š”๊ฒŒ ํฌ์ธํŠธ์ธ๊ฒƒ ๊ฐ™๋‹ค...

๊ฒฐ๋ก ์€ "ํ˜„์žฌ์—ญ == ์‹œ์ž‘์—ญ  and ๋‘๋ฒˆ ์ด์ƒ ๋‹ค๋ฅธ ์—ญ ๋ฐฉ๋ฌธ" ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ๋งŒ ์ˆœํ™˜์„ ์ด ๋œ๋‹ค.

 

๐ŸŸฃ ์ˆœํ™˜์„ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ  → BFS ์ด์šฉ

์ผ๋‹จ ์ˆœํ™˜์„ ์— ์†ํ•˜๋Š” ์—ญ์€ ๋ชจ๋‘ distance๋ฅผ 0์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  BFSํƒ์ƒ‰์„ ํ•ด์ค„ ๊ฒƒ์ด๋‹ค. ์ˆœํ™˜์—ญ์— ์†ํ•˜์ง€ ์•Š๋Š” ์—ญ์€ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋Š” ์—ญ๋“ค๋งŒ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ํ˜„์žฌ ์—ญ์˜ ๊ฑฐ๋ฆฌ + 1์„ ํ•ด์ฃผ๋ฉด์„œ ๋‹ค์Œ ์—ญ์— ์ €์žฅํ•ด์ค€๋‹ค.


๐ŸŸฃ ๋ฌธ์ œ ์†Œ์Šค์ฝ”๋“œ

์ธํ„ฐ๋„ท ์ฐธ๊ณ ํ•˜๋ฉด์„œ ํ’€์—ˆ๊ณ , ์ƒ๊ฐํ•˜๋Š”๋ฐ ์ข€ ํž˜๋“ค์—ˆ๋‹ค. ๋งŽ์ด ํ’€์–ด๋ดค๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ ์™œ bfs/dfs ๋Š” ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง€์ง€ ใ… ใ… 

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/16947_%EC%84%9C%EC%9A%B8%EC%A7%80%ED%95%98%EC%B2%A02%ED%98%B8%EC%84%A0/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

//
//  main.swift
//  Algorithm
//
//  Created by ์ด์ง€์ˆ˜ on 2022/06/30.
//

let n = Int(readLine()!)!
var graph = Array(repeating: [Int](), count: n+1)
var visited = Array(repeating: false, count: n+1)
var cycle = Array(repeating: false, count: n + 1)
var dist = Array(repeating: 0, count: n + 1)

// ์ˆœํ™˜์„ ?
func dfs(depth: Int, startIdx: Int, currentIdx: Int) {
    if currentIdx == startIdx && depth >= 2 {
        cycle[currentIdx] = true
        return
    }
    visited[currentIdx] = true
    
    for i in graph[currentIdx] {
        if !visited[i] {
            dfs(depth: depth+1, startIdx: startIdx, currentIdx: i)
        } else { //์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์—ญ์ด๋ผ๋ฉด
            if depth >= 2, startIdx == i {
                dfs(depth: depth, startIdx: startIdx, currentIdx: i)
            }
        }
    }
}

// ์ˆœํ™˜์„  ์•„๋‹Œ์• ๋“ค dfs๋Œ๋ ค์„œ dist ์ฐพ์•„์ค˜
func bfs(x: Int) -> Int {
    var bfsVisited = Array(repeating: false, count: 3001)
    var queue = [[Int]]()
    queue.append([x, 0])
    while !queue.isEmpty {
        let pop = queue.removeFirst()
        if cycle[pop[0]] {  // ์‚ฌ์ดํด์— ํฌํ•จ -> ์‚ฌ์ดํด์— ๋„๋‹ฌํ•œ๊ฑฐ์ž„
            return pop[1]
        }
        for i in graph[pop[0]] {
            if !bfsVisited[i] {
                queue.append([i, pop[1] + 1])
                bfsVisited[i] = true
            }
        }
    }
    return 0
}

for _ in 0..<n {
    let a = readLine()!.split(separator: " ").map{ Int(String($0))! }
    graph[a[0]].append(a[1])
    graph[a[1]].append(a[0])
    graph[a[0]].sort()
    graph[a[1]].sort()
}

// dfsํ•จ์ˆ˜ ์‹คํ–‰
for i in 1...n {
    visited = Array(repeating: false, count: 3001)
    dfs(depth: 0, startIdx: i, currentIdx: i)
}

// cycle ์•ˆ์—์žˆ์œผ๋ฉด ๊ฑฐ๋ฆฌ 0 ์•„๋‹ˆ๋ฉด bfs์ ์šฉ
for i in 1...n {
    if !cycle[i] {
        dist[i] = bfs(x: i)
    } else {
        dist[i] = 0
    }
}

dist.removeFirst()
print(dist.map{ String($0) }.joined(separator: " "))
๋ฐ˜์‘ํ˜•