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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1707๋ฒˆ - ์ด๋ถ„๊ทธ๋ž˜ํ”„ (DFS๋กœ ํ’€๊ธฐ!)

๊ฐ์ž ๐Ÿฅ” 2022. 5. 25. 18:42
๋ฐ˜์‘ํ˜•

๐Ÿ‘‰ ๋ฌธ์ œ ๋งํฌ

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

 

1707๋ฒˆ: ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์—

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

  • ์ด๋ถ„๊ทธ๋ž˜ํ”„ ์ด๋ฉด 1, ์ด๋ถ„๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋ฉด -1๋ฅผ flag์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ •์ ์€ ๊ฐ๊ฐ 1๊ณผ -1๋กœ ๊ตฌ๋ณ„ํ•ด์ฃผ์—ˆ๋‹ค. ๋งŒ์•ฝ ์ธ์ ‘ํ•œ ์ •์ ์ด ๋‘˜๋‹ค 1์ด๊ฑฐ๋‚˜, ๋‘˜๋‹ค -1์ด๋ผ๋ฉด ์ด๊ฑด ์ด๋ถ„๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹Œ๊ฑฐ๋‹ค! ์ด ๊ฐ’๋“ค์€ visited ์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” main์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์ฐจ๋ก€๋Œ€๋กœ ์„ค๋ช…์„ ์ž‘์„ฑํ•ด๋ณด์ž๋ฉด

  • ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›๋Š” K ๋งŒํผ result๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ชจ๋“  ๊ณผ์ •์„ for๋ฌธ์œผ๋กœ ๋ฌถ์–ด์•ผ ํ•œ๋‹ค.
  • VE์— ์ •์ ๊ฐฏ์ˆ˜ V, ๊ฐ„์„ ๊ฐฏ์ˆ˜ E๋ฅผ ๋ฐ›๋Š”๋‹ค.
  • grpah ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค.

  • ์ด ๋ฌธ์ œ์—์„œ ํ•ต์‹ฌ์€, ๋น„์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„๋„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹จ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋–„๋ฌธ์— ๋ชจ๋“  ์ •์ ์„ ํƒ์ƒ‰ํ•ด๋ณด๊ธฐ ์œ„ํ•ด์„œ for๋ฌธ์„ ๋Œ๋ฆด ๋•Œ, 1๋ถ€ํ„ฐ VE[0]+1 ๊นŒ์ง€ ๋Œ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ for๋ฌธ์—์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ชจ๋“  ์ •์ ์„ ํƒ์ƒ‰ํ•˜๊ณ , ์ธ์ ‘ํ•œ ๊ณณ์„ ํƒ์ƒ‰ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ์ฒซ ์ •์ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ visited๋ฅผ ํ™•์ธํ•œ๋‹ค. 0์ด๊ฒŒ ๋˜๋ฉด ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜ ๋…ธ๋“œ์ด๋ฏ€๋กœ bfs๋ฅผ ์ง„ํ–‰ํ•ด์„œ ํƒ์ƒ‰ํ•ด ์ค„ ๊ฒƒ์ด๋‹ค.
    • ๋งŒ์•ฝ bfs์—์„œ false ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค๋ฉด, ์ด๋ถ„๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ด๋‹ค. 
    • ๋ฐ”๋กœ flag์— -1์„ ๋„ฃ์–ด์ฃผ๊ณ  breakํ•ด์„œ for๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ ๋Œ๊ณ ์žˆ๋Š” ๊ฐ€์žฅ ์ตœ์ƒ๋‹จ for๋ฌธ์ด ๋๋‚˜๊ธฐ์ „์— YES or NO ๋ฅผ ํŒ๋‹จํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.
for _ in 0..<K {
    let VE = readLine()!.split(separator: " ").map{ Int(String($0))! }
    graph = Array(repeating: [Int](), count: VE[0]+1) // node ์ˆ˜ ๋ณด๋‹ค +1
    visited = Array(repeating: 0, count: VE[0]+1)
    flag = 1 // 1: ์ด๋ถ„๊ทธ๋ž˜ํ”„, -1: ์•„๋‹Œ๊ทธ๋ž˜ํ”„
    for _ in 0..<VE[1] {
        let uv = readLine()!.split(separator: " ").map{ Int(String($0))! }
        graph[uv[0]].append(uv[1])
        graph[uv[1]].append(uv[0])
    }
    
    for k in 1..<VE[0]+1 {
        if visited[k] == 0 {
            let tem = bfs(k)
            if tem == false {
                flag = -1
                break
            }
        }
    }
    
    if flag == 1 {
        print("YES")
    } else if flag == -1 {
        print("NO")
    }
}

 

  • DFS ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
  • ์šฐ์„  DFS์— ์ง„์ž…ํ•˜์ž๋งˆ์ž ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€๋‹ค. 
  • ๊ทธ๋ฆฌ๊ณ  deque ๋ฐฐ์—ด์— ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ์ •์ ์„ ๋„ฃ์–ด์ค€๋‹ค. 
    • deque์—๋Š” ๋‹ค์Œ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋“ค์„ ๋„ฃ์–ด์ฃผ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ํ™œ์šฉํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ํ•ด๋‹น deque๋Š” ์—ฐ๊ฒฐ๊ทธ๋ž˜ํ”„๊ฐ€ ๋๋‚ ๋•Œ๊นŒ์ง€ ๋น„์ง€ ์•Š๋Š”๋‹ค. deque๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋Š” ๊ฒƒ์€, ๊ทธ๋ž˜ํ”„๊ฐ€ ๋Š๊ฒผ๋‹ค๋Š” ๋œป์ด๋‹ค.
  • while๋ฌธ์— ์ง„์ž…ํ•ด์„œ deque์—์„œ ๊ฐ’์„ ์•ž์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋นผ์„œ ํƒ์ƒ‰ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.
func bfs(_ v: Int) -> Bool {
    visited[v] = 1
    var deque: [Int] = []
    deque.append(v)

    while !deque.isEmpty {
        let a = deque.removeFirst()

        for i in graph[a] {
            if visited[i] == 0 { //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ผ๋ฉด
                visited[i] = -visited[a] // visited ์—๋Š” 1, -1 ๋กœ ์ •์  ์ƒ‰์„ ๊ตฌ๋ณ„
                deque.append(i) //์—ฐ๊ฒฐ๋œ ๋‹ค์Œ ๋…ธ๋“œ๋Š i๊ฐ€ ๋  ๊ฒƒ
            } else {
                if visited[i] == visited[a] { // ๊ฐ™๋‹ค๋ฉด, ์ ‘ํ•œ ์ •์ ์˜ ์ƒ‰์ด ๊ฐ™๋‹ค๋Š” ๋œป -> false
                    return false
                }
            }
        }
    }
    return true
}

 

๐Ÿ’ป ์ „์ฒด์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1707_%EC%9D%B4%EB%B6%84%20%EA%B7%B8%EB%9E%98%ED%94%84/main.swift

 

GitHub - deslog/Algorithm

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

github.com

let K = Int(String(readLine()!))!
var flag = 0
var graph: [[Int]] = [[]]
var visited: [Int] = []

for _ in 0..<K {
    let VE = readLine()!.split(separator: " ").map{ Int(String($0))! }
    graph = Array(repeating: [Int](), count: VE[0]+1) // node ์ˆ˜ ๋ณด๋‹ค +1
    visited = Array(repeating: 0, count: VE[0]+1)
    flag = 1 // 1: ์ด๋ถ„๊ทธ๋ž˜ํ”„, -1: ์•„๋‹Œ๊ทธ๋ž˜ํ”„
    for _ in 0..<VE[1] {
        let uv = readLine()!.split(separator: " ").map{ Int(String($0))! }
        graph[uv[0]].append(uv[1])
        graph[uv[1]].append(uv[0])
    }
    
    for k in 1..<VE[0]+1 {
        if visited[k] == 0 {
            let tem = bfs(k)
            if tem == false {
                flag = -1
                break
            }
        }
    }
    
    if flag == 1 {
        print("YES")
    } else if flag == -1 {
        print("NO")
    }
}

func bfs(_ v: Int) -> Bool {
    visited[v] = 1
    var deque: [Int] = []
    deque.append(v)

    while !deque.isEmpty {
        let a = deque.removeFirst()

        for i in graph[a] {
            if visited[i] == 0 { //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ผ๋ฉด
                visited[i] = -visited[a] // visited ์—๋Š” 1, -1 ๋กœ ์ •์  ์ƒ‰์„ ๊ตฌ๋ณ„
                deque.append(i) //์—ฐ๊ฒฐ๋œ ๋‹ค์Œ ๋…ธ๋“œ๋Š i๊ฐ€ ๋  ๊ฒƒ
            } else {
                if visited[i] == visited[a] { // ๊ฐ™๋‹ค๋ฉด, ์ ‘ํ•œ ์ •์ ์˜ ์ƒ‰์ด ๊ฐ™๋‹ค๋Š” ๋œป -> false
                    return false
                }
            }
        }
    }
    return true
}
๋ฐ˜์‘ํ˜•