Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22-alstjr7437 #78

Merged
merged 2 commits into from
May 10, 2024
Merged

22-alstjr7437 #78

merged 2 commits into from
May 10, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Apr 13, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

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

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„





๐Ÿ’ก ๋ถ€๊ฐ€ ์„ค๋ช…

๋„ค์ด๋ฒ„ ๋ถ€์ŠคํŠธ ์บ ํ”„๊ฐ€ ์ง€์› ์–ธ์–ด๋กœ ์ณ์•ผํ•  ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค์‹œ Swift๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค..
๊ทธ๋Ÿฐ๋ฐ ์ •๋ง... ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ™”๋‚œ ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ์ผ๋‹จ ์ ์–ด๋ดค์Šต๋‹ˆ๋‹ค...

  1. n,m ์งค๋ผ์„œ ๋ฐ›๊ธฐ
  2. graph ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
  3. queue ์—†์ด bfs ๋Œ๋ฆฌ๊ธฐ]
  4. ๋งˆ์ง€๋ง‰ ์˜ˆ์™ธ ๋ถ€๋ถ„..
    ๊ทธ๋ž˜๋„ ๊ตฌํ˜„์„ ์™„๋ฃŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.





โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์‚ฌ์‹ค ์ˆ˜๋„์ฝ”๋“œ๊ฐ€ ํ•„์š”๊ฐ€ ์—†์„ ์ •๋„๋กœ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ์•Œ๋ฉด ๊ทธ๋ƒฅ ๋ˆ„๊ตฌ๋‚˜ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.



  1. ๋…ธ๋“œ์™€ ๊ฐ„์„  ์ˆ˜ ์ž…๋ ฅ ๋ฐ›๊ธฐ
  2. ์ด์–ด์ง„ ๊ฐ„์„  ์ž…๋ ฅ ๋ฐ›๊ธฐ
  3. 2๋ฒˆ์„ ๊ฐ€์ง€๊ณ  ๊ทธ๋ž˜ํ”„ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
  4. ๋ชจ๋“  ๋…ธ๋“œ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๊ธฐ
    • ๋ฐฉ๋ฌธ ์•ˆํ–ˆ์œผ๋ฉด BFS๋กœ ๋“ค์–ด๊ฐ€๊ธฐ ( ๊ฒฐ๊ณผ์— +1 ํ•˜๊ธฐ)
    • ๋ฐฉ๋ฌธ ํ–ˆ์œผ๋ฉด ๋„˜์–ด๊ฐ€๊ธฐ
  5. ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ





๐Ÿ“œ ์ •๋‹ต ์ฝ”๋“œ

import Foundation

let input = readLine()!.split(separator:
                            " ").map{ Int($0)!}

let n = input[0] , m = input[1]

var graph : [[Int]] = Array(repeating: [], count: n + 1)
var visited : [Bool] = Array(repeating: false, count: n + 1)
var result : Int = 0

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

func bfs(start: Int){
    visited[start] = true
    var queue: [Int] = [start]
    
    var idx : Int = 0
    
    while idx < queue.count{
        let current = queue[idx]
        
        idx += 1
        for i in graph[current]{
            if visited[i] == false {
                queue.append(i)
                visited[i] = true
            }
        }
    }
}

for i in 1...n{
    if visited[i] == false {
        bfs(start : i)
        result += 1
    }
}

print(result)





๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

1๏ธโƒฃ Swift n,m ์ž˜๋ผ์„œ ์ž…๋ ฅ ๋ฐ›๊ธฐ

Swift์—์„œ ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ split๋กœ ์ž˜๋ผ์„œ map์œผ๋กœ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์„œ list๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ณ 
๊ทธ ๋งŒ๋“  list๋ฅผ ์ด์šฉํ•˜์—ฌ n,m์œผ๋กœ ๋‚˜๋ˆ ์คฌ์Šต๋‹ˆ๋‹ค..

let input = readLine()!.split(separator:
                            " ").map{ Int($0)!}

let n = input[0] , m = input[1]




2๏ธโƒฃ ๋น„์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธฐ๋ณธ๊ฐ’ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

graph ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋•Œ ๋…ธ๋“œ ๊ฐฏ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋œ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
์˜ˆ) [0, 0, 0, 0, 0]

var graph : [[Int]] = Array(repeating: [], count: n + 1)
var visited : [Bool] = Array(repeating: false, count: n + 1)




3๏ธโƒฃ queue ์—†์ด BFS ๋Œ๋ฆฌ๊ธฐ

Swift์—์„œ๋Š” queue๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์•„์„œ ๋”ฐ๋กœ
Array๋Š” removeFirst()ํ•จ์ˆ˜๊ฐ€ ์ œ๊ณต ๋˜์ง€๋งŒ O(n)์œผ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ ธ์„œ
์–ด๋–ป๊ฒŒ ๊ตฌํ˜„์„ ํ•˜์ง€ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ๋ธ”๋กœ๊ทธ์—์„œ ์ฐธ๊ณ ํ•œ ๋ถ€๋ถ„์ด

์•„๋ž˜์™€ ๊ฐ™์€ ๋กœ์ง์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ gif๋Š” ๋นจ๊ฐ„์ƒ‰ ๋ถ€๋ถ„์ด idx์ด๊ณ  queue๊ฐ€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
BFS

func bfs(start: Int){
    visited[start] = true
    var queue: [Int] = [start]
    
    var idx : Int = 0
    
    while idx < queue.count{
        let current = queue[idx]
        
        idx += 1
        for i in graph[current]{
            if visited[i] == false {
                queue.append(i)
                visited[i] = true
            }
        }
    }
}




4๏ธโƒฃ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ์ฐพ๊ธฐ

image ์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๊ณ„์†ํ•ด์„œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์—ˆ๋‹ค..



์ฒ˜์Œ์—๋Š” ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ธธ๋ž˜ ํฌ๊ธฐ๋ฅผ ์ž˜๋ชป์คฌ๋‚˜ ํ•˜๋ฉด์„œ
์Šค์œ„ํ”„ํŠธ ์š•์„ ์—„์ฒญ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ

์ฝ”๋“œ๊ฐ€ ์ „ํ˜€ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์—†๋Š” ๊ฒƒ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋“ค๊ณ ์™€์„œ ๋น„๊ตํ•ด๋ด๋„ ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋งž๊ณ  ๋‚ด ์ฝ”๋“œ๋Š” ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธธ๋ž˜
์ญ‰ ์‚ดํŽด๋ณด๋‹ˆ๊นŒ....

// ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋ฐœ์ƒ ์ฝ”๋“œ
for _ in 1...m {
    let tmp = readLine()!.split(separator: " ").map { Int($0)!}
    graph[tmp[0]].append(tmp[1])
    graph[tmp[1]].append(tmp[0])
}

// ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ
for _ in 0..<m {
    let tmp = readLine()!.split(separator: " ").map { Int($0)!}
    graph[tmp[0]].append(tmp[1])
    graph[tmp[1]].append(tmp[0])
}

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™”์—ˆ๋Š”๋ฐ





์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด _๋กœ ์‚ฌ์šฉํ•ด์„œ ๋”ฐ๋กœ ๋ฐ˜๋ณต๋ฌธ์˜ ํšŸ์ˆ˜๋งŒ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๊ณ„์†ํ•ด์„œ ๋„˜์–ด๊ฐ”์ง€๋งŒ

๋ฌธ์ œ๋Š”

์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 1,000, 0 โ‰ค M โ‰ค Nร—(N-1)/2)

์ด ์˜€๋‹ค.

M์ด 0๋ถ€ํ„ฐ ์‹œ์ž‘์ด ๋˜์„œ 1...m์„ ํ•˜๋ฉด 1 -> 0์œผ๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์ด์˜€๋‹ค..





๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์ž

@9kyo-hwang
Copy link

์•„๋‹ˆ queue ์ง€์› ์•ˆํ•˜๋Š” ๊ฑฐ ์ข€ ๋”์ฐํ•˜๋„ค;

์ฐพ์•„๋ณด๋‹ˆ ์ €๋Ÿฐ ์‹์œผ๋กœ head๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ๊ฑ”๋ฅผ ์˜ฌ๋ฆฌ๋Š” ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ณ , stack 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋„ค์š”.

ํŠน์ดํ•œ ๊ฒŒ ๋ณดํ†ต reverse๋ž€ ๋ฉ”์„œ๋“œ๋Š” $O(n)$์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฌ๋Š”๋ฐ swift์—์„œ reversed()๋ž€ ๋ฉ”์„œ๋“œ๋Š” $O(1)$๋งŒ์— ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋„๋ก ๊ตฌํ˜„๋ผ์žˆ์–ด์„œ stack 2๊ฐœ๋ฅผ ์จ๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋„ค์š”...

@alstjr7437
Copy link
Member Author

์•„๋‹ˆ queue ์ง€์› ์•ˆํ•˜๋Š” ๊ฑฐ ์ข€ ๋”์ฐํ•˜๋„ค;

์ฐพ์•„๋ณด๋‹ˆ ์ €๋Ÿฐ ์‹์œผ๋กœ head๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ๊ฑ”๋ฅผ ์˜ฌ๋ฆฌ๋Š” ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ณ , stack 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋„ค์š”.

ํŠน์ดํ•œ ๊ฒŒ ๋ณดํ†ต reverse๋ž€ ๋ฉ”์„œ๋“œ๋Š” O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฌ๋Š”๋ฐ swift์—์„œ reversed()๋ž€ ๋ฉ”์„œ๋“œ๋Š” O(1)๋งŒ์— ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋„๋ก ๊ตฌํ˜„๋ผ์žˆ์–ด์„œ stack 2๊ฐœ๋ฅผ ์จ๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋„ค์š”...

์˜ค.. ๊ตํ™ฉ๋‹˜ ๋ง์”€์„ ๋“ฃ๊ณ  reversed์™€ reverse ๋ฉ”์„œ๋“œ๋ฅผ ์กฐ๊ธˆ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ

reverse๋Š” ๊ทธ๋ƒฅ ๋ฐฐ์—ด์„ ๋ฐ˜๋Œ€๋กœ ๋’ค์ง‘๋Š” ๋ฉ”์„œ๋“œ๋ผ O(n)์ด ๋‚˜์˜ค๊ณ 
reversed๋Š” ์›๋ณธ์„ ๋ณต์‚ฌํ•ด์„œ ๋“ค๊ณ ๊ฐ€๋Š”๋ฐ ๋ณต์‚ฌ ์‹œ์ ์—์„œ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚˜๋„ ๋ฐ”๋€Œ๋Š” ๋ถ€๋ถ„์€ ์—†๊ตฐ์š”..
๊ทธ๋ž˜๊ณ  ์ƒˆ๋กœ์šด ReversedCollection ๋ž˜ํ•‘ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋”๊ตฐ์š”!!

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ์ƒˆ๋กœ์šด ์ง€์‹์„ ๋„ฃ์–ด์ฃผ์…”์„œ!

var originalArray = [1, 2, 3]
var reversedArray = originalArray.reversed()

print("0th index of OriginalArray \(originalArray[0])") // 1
print("0th index of ReversedBaseArray \(reversedArray._base[0])")       // 1


print("0th index of OriginalArray \(originalArray.first)")  // 1
print("0th index of ReversedBaseArray \(reversedArray.first)")  // 3


originalArray[0] = 4 // Modification makes a separate copy of each instance
print("0th index of OriginalArray \(originalArray[0])") // 4
print("0th index of ReversedBaseArray \(reversedArray._base[0])")   // 1


originalArray.reverse()
print(originalArray)    // [3,2,4]
print(reversedArray)    // ReversedCollection<Array<Int>>(_base: [1, 2, 3])

+ index ์ ‘๊ทผ ๋ถˆ๊ฐ€..

์•„๋ž˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด ReversedCollection์€ Int ์ธ๋ฑ์‹ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋”๊ตฐ์š”..

let arr = [1,2,3,4]
let rev = arr.reversed() // ReversedCollection<Array<Int>> ์ด๊ฒƒ์€ Int์ธ๋ฑ์‹ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
let i = rev[1] // compile error!

๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด ๋‘๊ฐœ ์“ฐ๋Š”๊ฒŒ ์ด๊ฒŒ ๋งž์„๊นŒ์š”..?
append๋ฅผ ํ•ด์„œ ํ•˜๋ฉด ์–ด์งœํ”ผ O(n)์ด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ
ํƒ€์ž…๋•Œ๋ฌธ์— ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ ์•ˆ๋‚˜๋„ค์š”.. @9kyo-hwang

func bfs3(start: Int){
    visited[start] = true
    var stack1: [Int] = [start]
    
    while !stack1.isEmpty{
        var temp: [Int] = []
        for i in stack1.reversed() { temp.append(i) }
        stack1 = temp
        let current = stack1.removeLast()
        for i in graph[current]{
            if visited[i] == false {
                stack1.append(i)
                visited[i] = true
            }
        }
    }
}

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift๋กœ Queue ๊ตฌํ˜„ํ•˜๊ธฐ ์—ฌ๊ธฐ ๋ธ”๋กœ๊ทธ ๋ณด๊ณ  ํ•ด๋ณธ ์ฝ”๋“œ์ธ๋ฐ...

struct Queue<T> {
    private var enqueue: [T] = []
    private var dequeue: [T] = []
    
    var isEmpty: Bool {
        return enqueue.isEmpty && dequeue.isEmpty
    }
    
    mutating func push(_ input: T) {
        enqueue.append(input)
    }
    
    @discardableResult
    mutating func pop() -> T? {
        if dequeue.isEmpty {
            dequeue = enqueue.reversed()
            enqueue = []
        }
        return dequeue.popLast()
    }
}

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ, ๋ณด๋ฉด ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฑด ์•„๋ฌด๊ฒƒ๋„ ์—†๊ณ  ์ „๋ถ€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌํ•˜๋”๋ผ๊ตฌ์š”.

ํ•ด์„œ

func bfs(start: Int) {
    visited[start] = true
    
    var q = Queue<Int>()
    q.push(start)
    
    while !q.isEmpty {
        let current = q.pop()!
        for i in graph[current] {
            if visited[i] == false {
                q.push(i)
                visited[i] = true
            }
        }
    }
}

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

push๋Š” enqueue์—๋งŒ, pop์€ dequeue์— ๋Œ€ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
reversed()๋Š” $O(1)$ ์‹œ๊ฐ„์— ํ•ด๊ฒฐ๋˜๊ณ , enqueue๋ฅผ ์ง€์šฐ๋Š” ๊ฑธ ๋ฐฐ์—ด ์žฌํ• ๋‹น์œผ๋กœ ํ•˜๋ฉด $O(1)$๋งŒ์— ํ•ด๊ฒฐ๋ผ์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


stack1์˜ reversed๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ์žฌ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, popํ•  ๋•Œ ๋น„์—ˆ์„ ๋•Œ๋งŒ ํ•˜๋„๋ก ์„ค์ •ํ•ด์•ผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋™์ž‘ํ•  ๊ฒ๋‹ˆ๋‹ค.

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ ‡๋‹ค๋ฉด DFS๋ฅผ ๋Œ๋ ค๋ด…์‹œ๋‹ค!

import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def dfs(start,visited):
    visited[start ] =mark
    for node in graph[start]:
        if visited[node] == 0:
            dfs(node,visited)

N, M = map(int, input().split())

graph=[[] for _ in range(n+1)]

for i in range(m):
    start, end = map(int, input().split())
    graph[start].append(end)
    graph[end].append(start)

visited=[0] * (N+1)
mark = 1

for i in range(1, N+1):
    if visited[i] == 0:
        dfs(i, visited)
        mark += 1

print(max(visited))

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌด๋‚œํ•œ bfs ๋ฌธ์ œ๊ตฐ์š” swift ์— queue ์—†๋Š”๊ฑด ์ง„์งœ..
ํ™”์ดํŒ…์ž…๋‹ˆ๋‹ค ํ•˜ํ•˜

gif ๋„ˆ๋ฌด ์ด์˜๊ฒŒ ์ž˜๋งŒ๋“ค์–ด์„œ ์ง๊ด€์ ์ด๊ณ  ์ข‹์•˜์–ด์š”!!

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m) = br.readLine().split(" ").map { it.toInt() }
    val graph = List(n + 1) { mutableListOf<Int>() }
    val visited = Array<Boolean>(n + 1) { false }

    repeat(m) {
        val (x, y) = br.readLine().split(" ").map { it.toInt() }
        graph[x].add(y)
        graph[y].add(x)
    }

    println((1..n).count { isConnected(it, graph, visited) })
}

private fun isConnected(start: Int, graph: List<List<Int>>, visited: Array<Boolean>): Boolean {
    if (visited[start]) {
        return false
    }

    val deque = ArrayDeque<Int>()
    deque.add(start)

    while (deque.isNotEmpty()) {
        val now = deque.removeFirst()
        for (next in graph[now]) {
            if (visited[next]) {
                continue
            }
            deque.add(next)
            visited[next] = true
        }
    }
    return true
}

@tgyuuAn tgyuuAn removed the request for review from wkdghdwns199 May 10, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants