๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] Swift 9466 ํ…€ ํ”„๋กœ์ ํŠธ DFS

by Jouureee 2022. 4. 12.

๋ฌธ์ œ :

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

 

9466๋ฒˆ: ํ…€ ํ”„๋กœ์ ํŠธ

์ด๋ฒˆ ๊ฐ€์„ํ•™๊ธฐ์— '๋ฌธ์ œ ํ•ด๊ฒฐ' ๊ฐ•์˜๋ฅผ ์‹ ์ฒญํ•œ ํ•™์ƒ๋“ค์€ ํ…€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ํŒ€์› ์ˆ˜์—๋Š” ์ œํ•œ์ด ์—†๋‹ค. ์‹ฌ์ง€์–ด ๋ชจ๋“  ํ•™์ƒ๋“ค์ด ๋™์ผํ•œ ํŒ€์˜ ํŒ€์›์ธ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ํ•œ ํŒ€๋งŒ ์žˆ์„

www.acmicpc.net

๋ถ„์„ :

ํ•™์ƒ๋“ค์ด cycle์„ ์ด๋ฃจ๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด DFS๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค. ์ฒ˜์Œ์— ์ ‘๊ทผํ•œ ๋ฐฉ์‹์€ while๋ฌธ์„ ํ†ตํ•ด dfs ํƒ์ƒ‰์„ ํ•˜๊ณ ์ž ํ–ˆ์ง€๋งŒ visit๋ฅผ ๊ฒ€์‚ฌํ•ด์ฃผ์ง€ ์•Š์•„์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ๊ทธ๋ž˜์„œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด ๋‹ค์‹œ ํ’€์—ˆ๋‹ค ใ… 

 

์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•ด visit๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ 

์—ฌ๊ธฐ์„œ๋Š” done(ํŒ€์„ ์ด๋ฃจ์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€)๋„ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค. ๊ฐ™์ด ํ”„๋กœ์ ํŠธ ํ•˜๊ณ  ์‹ถ์€ ์นœ๊ตฌ๊ฐ€ ๊ฒน์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—(ex> arr[1] : 3, arr[3] : 3, arr[5] = 3) visit๊ฐ€ true์ผ๋•Œ done ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ด๋ฏธ ํŒ€ ๋งค์นญ์ด ๋๋‚ฌ๋Š”์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. 

๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์žฌ๊ท€์— done ๋ฐฐ์—ด๊นŒ์ง€ ์žˆ์–ด ์ถ”์ƒ์ ์ด๊ณ  ์–ด๋ ต๊ฒŒ ๋Š๊ปด์กŒ๋‹ค .. 

 

์‹œ๊ฐ„์ดˆ๊ณผ๋‚œ ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/03/17.
//

import Foundation
var Tcase = Int(readLine()!)!

func dfs(_ arr:[Int]) -> Int {
    var answer = 0
    for (idx, i) in arr.enumerated() { // me, friend
        var cnt = 0
        let start = idx + 1
        var friend = i
        while(start != friend) {
            if( friend == arr[friend-1]) {
                break
            }else {
                friend = arr[friend-1]
                //cnt += 1
            }
        }
        if(start == friend){
            cnt += 1
        }else {
            cnt = 0
        }
        answer += cnt
    }
    return arr.count - answer
}

for _ in 0..<Tcase {
    var n = Int(readLine()!)!
    var arr = readLine()!.split(separator: " ").map{ Int($0)!}
    print(dfs(arr))
}

 

Swift ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/03/17.
//

import Foundation
var Tcase = Int(readLine()!)!
var visit: [Bool] = []
var done: [Bool] = [] //cycle์„ ๊ฑฐ์นœ node์ธ์ง€(ํŒ€ ๋งค์นญ์„ ๊ฑฐ์นœ ๋…ธ๋“œ์ธ์ง€)
var cnt = 0

func dfs(_ arr:[Int], cur: Int) {
    var nextNode = arr[cur]
    visit[cur] = true
    if !visit[nextNode] {
        dfs(arr, cur: nextNode)
    }else {
        if !done[nextNode] { // cycle์ด ๋˜์—ˆ๋Š”๋ฐ ์•„์ง ํŒ€ ๋งค์นญ ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด
            while nextNode != cur { // cycle ์นœ๊ตฌ๋“ค ์ €์žฅ
                cnt += 1
                nextNode = arr[nextNode]
            }
            cnt += 1 // cur ์ž๊ธฐ ์นด์šด๋“œ
        }
    }
    done[cur] = true
}

for _ in 0..<Tcase {
    let n = Int(readLine()!)!
    var arr = readLine()!.split(separator: " ").map{ Int($0)!}
    arr.insert(0, at: 0) //์˜๋ฏธ ์—†๋Š” ์ˆ˜ index 1๋ถ€ํ„ฐ ~ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด 
    cnt = 0
    visit = Array(repeating: false, count: n+1)
    done = Array(repeating: false, count: n+1)
    for i in arr {
        if !visit[i] {
            dfs(arr, cur: i)
        }
    }
    print(arr.count - cnt)
    
}

๋Œ“๊ธ€