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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 11724๋ฒˆ - ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ (DFS ์—ฐ์Šต)

๊ฐ์ž ๐Ÿฅ” 2022. 5. 20. 21:48
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

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

 

11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฐ„์„ ์˜ ์–‘ ๋์  u์™€ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ u, v ≤ N, u ≠ v) ๊ฐ™์€ ๊ฐ„์„ ์€ ํ•œ ๋ฒˆ๋งŒ ์ฃผ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

  • ๋‚˜๋Š” ์•„์ง graph๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๊ฒŒ ํž˜๋“ค๋‹ค.
    • ์ด๋ฒˆ์— ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์€, ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. graph๋ผ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค๋กœ ์ฑ„์›Œ์ฃผ์—ˆ๋‹ค.

  • ๊ทธ๋ฆฌ๊ณ  ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๊ณ , ๊ทธ ๊นŠ์ด๋ฅผ ์ „์ฒด ๋‹ค ํƒ์ƒ‰ํ•œ ํ•œ๋‹ค์Œ, ์—ฐ๊ฒฐ์ด ๋Š๊ธฐ๋ฉด!! ์ƒˆ๋กœ์šด ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰ main for๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์กฐ๊ธˆ ํž˜๋“ค์—ˆ๋‹ค.
    • ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์ธ๋ฐ → ๋” ์ด์ƒ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค๋ฉด → ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ , count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค. (๊ทธ๋ž˜ํ”„ํ•˜๋‚˜๊ฐ€ ๋”์žˆ๋‹ค๋Š” ๋œป์ด๋‹ˆ๊นŒ!)
    • ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์ธ๋ฐ → ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด → dfs ์žฌ๊ท€๋ฅผ ๋Œ๋ ค์„œ ํƒ์ƒ‰ํ•ด์ค€๋‹ค.

 

์ „์ฒด ์†Œ์Šค์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/11724_%EC%97%B0%EA%B2%B0%20%EC%9A%94%EC%86%8C%EC%9D%98%20%EA%B0%9C%EC%88%98/main.swift

 

GitHub - deslog/Algorithm

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

github.com

// n : ๋…ธ๋“œ(์ •์ )์˜ ๊ฐœ์ˆ˜
// m : ๊ฐ„์„  ๊ฐœ์ˆ˜ (์—ฐ๊ฒฐ์š”์†Œ)

var nm = readLine()!.split(separator: " ").map{ Int(String($0))! }
var graph = Array(repeating: [], count: nm[0]+1)
var visited = Array(repeating: false, count: nm[0]+1)
var answer = 0
var depth = 0

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

func dfs(_ start: Int ,_ depth: Int) {
    visited[start] = true // ๋“ค์–ด๊ฐ€์ž๋งˆ์ž ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ go
    for i in 0..<graph[start].count{ // ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฉ๋ฌธ
        let next = graph[start][i]
        if visited[next as! Int] == false{ // ๋งŒ์•ฝ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜ ๋…ธ๋“œ๋ฉด
            dfs(next as! Int, depth + 1) // ๋ฐฉ๋ฌธํ•ด์„œ ๋‹ค์‹œ ํƒ์ƒ‰
        }
    }
}

// main
for i in 1..<nm[0]+1 {
    if !visited[i] { //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์ด๊ณ 
        if graph[i].isEmpty { // ๋”์ด์ƒ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค๋ฉด (๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ์ด ๋Š๊ฒผ๋‹ค๋ฉด)
            answer += 1 // ์ƒˆ๋กœ์šด ๊ทธ๋ž˜ํ”„๊ฐ€ ์ƒ์„ฑ๋œ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— answer += 1
            visited[i] = true // ๊ทธ๋Ÿฐ ๋‹ค์Œ, ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ go
        } else { // ์—ฐ๊ฒฐ๋œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ๋‹ค๋ฉด
            dfs(i, 0) // dfs๋กœ ๋‹ค์Œ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด dfs ์žฌ๊ท€
            answer += 1 // ์žฌ๊ท€๊ฐ€ ๋๋‚˜๋ฉด answer +=1
        }
    }
}

print(answer)
๋ฐ˜์‘ํ˜•