-
Notifications
You must be signed in to change notification settings - Fork 0
2-sep037 #12
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
2-sep037 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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] | ||
|
|
||
| graph[a].append(b) | ||
| graph[b].append(a) | ||
| } | ||
|
Comment on lines
+16
to
+24
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MuchanKim ์น ํธ๋ฆฌ์์ฑ๋ ๋ชจ๋ํํ๋๊ฑฐ ์ข๋ค์ฉ ๋ฌด!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํจ์ฌ ๊น๋ํ๊ฒ ์ ์์ผ๋ก ํธ์
จ๋ค์. |
||
|
|
||
| dfs(1) | ||
|
Comment on lines
+26
to
+37
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ต๊ณผ์์ ์ธ ์ฝ๋ ์ ๋ณด๊ณ ๊ฐ๋๋ค. ์ ๋ ์์ ๋๊ฐ์ด ํ์์ต๋๋ค ใ ใ
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (2..<N+1).forEach { print(parent[$0]) }์๋ฐ์์ผ๋ก ์์ ๋ฒ์๋ฅผ ์ ํด์ฃผ๊ณ .forEach๋ก ํ์ค๋ก ์ถ๋ ฅํ ์๋ ์์ด๋ค~!!!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด๋ ๊ฒ ์ฐ๋ฉด ๋ฉ์์ด๊ฐ ๋๋ '๊ธธ'์ด๋ค์
Comment on lines
+8
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ค ์ข๊ตฐ์ ! ์ด๋ฒ์ ๋ฌธ๋ฒ ์ฐ์ตํ๋ค๊ณ Array ํ์ฅํด์ ์ปดํฉํธ ๋งต์ ๋ง๋ค์ด๋ณด๊ณ tree[a].append(b) ํ๊ณ ์ถ์ด์ ํ๋๋ ๋๋๊ตฐ์ ! ํจ์๋ฅผ ์ข ๋ถ๋ฆฌํด์ ์ฌ์ฉํด๋ดค๋๋ ์ข์๊ฑฐ๊ฐ์์ ํธํธ //
// แแ
ฎแแ
ฉแแ
ด แแ
ณแ
แ
ต.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() |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ ๊ฒ๋ํ ์ ์๊ณ , ํ์ค๋ก๋ ๊ฐ๋ฅํด์ !
์ทจํฅ์ฐจ์ด๊ธดํ๋ฐ, ๊ณต์ ํฉ๋๋ค !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ ~ ์์ฃผ ๊น๊ผผ์ค
์์ผ๋ก ์ด๋ ๊ฒ ์ฐ๋ ๊ฒ๋ ์ต๊ดํ ํด๋ณผ๊ฒ์ !
๊ฐ์ํฉ๋๋ค ๐ฅน