Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions sep037/BFS/BOJ_11725.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// main.swift
// 11725
//
// Created by Seungeun Park on 4/10/25.
//

import Foundation

let N = Int(readLine()!)!

var graph = Array(repeating: [Int](), count: N+1)
var parent = Array(repeating:0, count: N+1)
var visited = Array(repeating: false, count: N+1)

for _ in 0..<N-1 {
let edge = readLine()!.split(separator: " ").map {Int($0)!}

let a = edge[0]
let b = edge[1]
Comment on lines +19 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ๋„ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ์ค„๋กœ๋„ ๊ฐ€๋Šฅํ•ด์š” !
์ทจํ–ฅ์ฐจ์ด๊ธดํ•œ๋ฐ, ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค !

let (a,b) = (edge[0],edge[1])

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ํ—‰ ~ ์•„์ฃผ ๊น”๊ผผ์Šค
์•ž์œผ๋กœ ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ๋„ ์Šต๊ด€ํ™” ํ•ด๋ณผ๊ฒŒ์š” !

๊ฐ์Ÿˆํ•ฉ๋‹ˆ๋‹ค ๐Ÿฅน


graph[a].append(b)
graph[b].append(a)
}
Comment on lines +16 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ „ ์ค‘๊ฐ„์— ๋ณ€์ˆ˜ ์„ ์–ธ ์—†์ด ์ธ๋ฑ์Šค๋กœ ๋ฐ”๋กœ ๋„ฃ์–ด์คฌ๋Š”๋ฐ ๋งˆ์นด๋กœ๋‹ˆ์”จ์ฒ˜๋Ÿผ ์„ ์–ธํ•ด๋†“๊ณ  ์ „๋‹ฌํ•˜๋Š”๊ฒŒ ๋ณด๊ธฐ๋Š” ํŽธํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”^^
์ „ ์•„๋ž˜์ฒ˜๋Ÿผ ์งฐ์Šด๋‹ค!

func getTree(_ n: Int) -> Tree {
    var tree = Array(repeating: [Int](), count: n + 1)
    
    for _ in 0..<n-1 {
        let edge = readLine()!.split(separator: " ").map { Int($0)! }
        
        // Tree = ์–‘๋ฐฉํ–ฅ์ด๋ผ ์–‘์ชฝ ๋‹ค์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ
        tree[edge[0]].append(edge[1])
        tree[edge[1]].append(edge[0])
    }
    
    return tree
}

Copy link
Member

Choose a reason for hiding this comment

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

@MuchanKim ์˜น ํŠธ๋ฆฌ์ƒ์„ฑ๋„ ๋ชจ๋“ˆํ™”ํ•˜๋Š”๊ฑฐ ์ข‹๋„ค์šฉ ๋ฌด!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ํ—‰ ์šฐ์™• ์ฃผ์ž…์‹ ๊ต์œก ์•„์ฃผ ์ข‹์Šต๋‹ˆ๋‹ค
์‰ฌ์šด ๊ฒƒ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ . . ~


func dfs(_ currentnode : Int){
visited[currentnode] = true

for next in graph[currentnode] {
if !visited[next] {
parent[next] = currentnode
dfs(next)
}
}
}
Comment on lines +26 to +35
Copy link
Collaborator

@bishoe01 bishoe01 Apr 13, 2025

Choose a reason for hiding this comment

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

ํ›จ์”ฌ ๊น”๋”ํ•˜๊ฒŒ ์ •์„์œผ๋กœ ํ‘ธ์…จ๋„ค์š”.
๊ดœํžˆ ๋ง‰ ์ฒ˜์Œ์— ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ๋‹ค๋ฅธ์ชฝ์œผ๋กœ ํ’€์—ˆ๋‹ค๊ฐ€ ๋Œ์•„์™”๋Š”๋ฐ, ๊ทธ๋ž˜์„œ ์›๋ž˜ ๋ณต์žกํ•˜๊ฒŒ ํ’€์–ด์•ผํ•˜๋Š”์ง€ ์•Œ์•˜์–ด์š”.
์ „์ฒด์ฝ”๋“œ๋Š” ์ฝ”๋ฉ˜ํŠธ๋กœ ๋‹ฌ๊ฒŒ์š” !


dfs(1)
Comment on lines +26 to +37
Copy link
Collaborator

@MuchanKim MuchanKim Apr 12, 2025

Choose a reason for hiding this comment

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

๊ต๊ณผ์„œ์ ์ธ ์ฝ”๋“œ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค. ์ €๋„ ์™„์ „ ๋˜‘๊ฐ™์ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๊ธฐ๋ณธ์— ์ถฉ์‹คํ•˜๊ธฐ !


for i in 2...N {
print(parent[i])
}
Comment on lines +39 to +41
Copy link
Member

Choose a reason for hiding this comment

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

(2..<N+1).forEach { print(parent[$0]) }

์š”๋Ÿฐ์‹์œผ๋กœ ์•ž์— ๋ฒ”์œ„๋ฅผ ์ •ํ•ด์ฃผ๊ณ  .forEach๋กœ ํ•œ์ค„๋กœ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šด๋‹ค~!!!

.forEach { ... } ๋Š” ๋ฐฐ์—ด์ด๋‚˜ ๋ฒ”์œ„ ์•ˆ์˜ ๊ฐ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ์ค‘๊ด„ํ˜ธ { } ์•ˆ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ๋ฉ‹์Ÿ์ด๊ฐ€ ๋˜๋Š” '๊ธธ'์ด๋„ค์š”
๊ฟ€ ํŒ ๊ฐ ์‚ฌ ํ•˜ ์˜ต ๋‹ˆ ๋‹ค ~ ~

Comment on lines +8 to +41
Copy link
Collaborator

@YooGyeongMo YooGyeongMo Apr 15, 2025

Choose a reason for hiding this comment

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

์˜ค ์ข‹๊ตฐ์š” !

์ด๋ฒˆ์— ๋ฌธ๋ฒ• ์—ฐ์Šตํ•œ๋‹ค๊ณ  Array ํ™•์žฅํ•ด์„œ ์ปดํŒฉํŠธ ๋งต์„ ๋งŒ๋“ค์–ด๋ณด๊ณ 
ํŠœํ”Œ๋กœ ๋ฐ›์•„๋ณด๊ณ ์ž ํ•œ๋ฒˆ ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ ํ†ต๊ณผ๋˜๋”๊ตฐ์š” ๋‹คํ–‰์ž…๋‹ˆ๋‹ค ํ˜ธํ˜ธ..

tree[a].append(b)
tree[b].append(a)

ํ•˜๊ณ ์‹ถ์–ด์„œ ํ–ˆ๋”๋‹ˆ ๋˜๋”๊ตฐ์š” !

ํ•จ์ˆ˜๋ฅผ ์ข€ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•ด๋ดค๋”๋‹ˆ ์ข‹์€๊ฑฐ๊ฐ™์•„์š” ํ˜ธํ˜ธ

//
//  แ„‡แ…ฎแ„†แ…ฉแ„‹แ…ด แ„แ…ณแ„…แ…ต.swift
//  แ„‹แ…กแ†ฏแ„€แ…ฉแ„…แ…ตแ„Œแ…ณแ†ท แ„‹แ…งแ†ซแ„‰แ…ณแ†ธ
//
//  Created by Demian Yoo on 4/16/25.
//

/* TODO
1. ํŠธ๋ฆฌ ๊ตฌ์กฐ ํŒŒ์•… -> ํŠธ๋ฆฌ ๋ฃจํŠธ 1, ์–‘๋ฐฉํ–ฅ์ž„
2. dfs ์‚ฌ์šฉํ•˜๋ฉด ๋ ๋“ฏํ•จ dfs(1)๋กœ ๋“ค์–ด๊ฐ€์„œ ๊นŠ์ด๋“ค์–ด๊ฐ€์„œ ๋น ์ ธ๋‚˜์˜ค๋ฉด์„œ ๊ทธ ์œ„์— ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•˜๊ณ  ๋ฑ‰์–ด์ฃผ๋ฉด๋จ.
3. ํ•จ์ˆ˜๋กœ ๋ถ„ํ• ํ•ด๋ณด๊ธฐ
 
 */
import Foundation

let N = Int(readLine()!)!

var tree: [[Int]] = Array( repeating:[Int](), count: N+1 )
var parentArr: [Int] = Array( repeating:0, count: N+1 )
var visited = Array( repeating: false, count: N+1 )


// MARK - ํ•จ์ˆ˜ ์ต์Šคํ…์…˜ ๋งŒ๋“ค์–ด๋ณด๊ธฐ
extension Array {
    func makeMap<T>(_ transform: (Element) -> T?) -> [T] {
        var result: [T] = []
        for element in self {
            if let value: T = transform(element) {
                result.append(value)
            }
        }
        return result
    }
    
    func asTuple() -> (Element, Element)? {
        guard self.count == 2 else { return nil }
        return (self[0], self[1])
    }
}

// MARK - ํŠธ๋ฆฌ ๋งŒ๋“ค๊ธฐ
func makeTree(_ loop: Int) {
    // loop-1๋ฒˆ
    for _ in 0..<loop {
        // NOTE - ํŠœํ”Œ๋กœ ๋“ค์–ด๊ฐ
        if let (a, b) = readLine()!.split(separator: " ").makeMap({ Int($0) }).asTuple() {
            tree[a].append(b)
            tree[b].append(a)
        }
    }
}

// MARK - dfs ๋ถ€๋ชจ์ฐพ๊ธฐ
func findParentNode(_ idx: Int) {
    visited[idx] = true
    
    for nextIdx in tree[idx] {
        if !visited[nextIdx] {
            parentArr[nextIdx] = idx
            findParentNode(nextIdx)
        }
    }
}

// MARK - ํŠธ๋ฆฌ ์ถœ๋ ฅํ•˜๊ธฐ
func printAnswer() {
    // 1๋ฒˆ ๋…ธ๋“œ ๋ฃจํŠธ๋ผ์„œ ํ‘œํ˜„ X
    for i in 2...N {
        print(parentArr[i])
    }
}

makeTree(N-1)
findParentNode(1)
printAnswer()