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

83-tgyuuAn #259

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@
| 80์ฐจ์‹œ | 2024.11.11 | ๋‹ค์ต์ŠคํŠธ๋ผ | <a href="https://www.acmicpc.net/problem/11779">์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/254
| 81์ฐจ์‹œ | 2024.11.15 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/1701">Cubeeditor</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/255
| 82์ฐจ์‹œ | 2024.11.22 | ํฌ์†Œ ๋ฐฐ์—ด | <a href="https://www.acmicpc.net/problem/17435">ํ•ฉ์„ฑํ•จ์ˆ˜์™€ ์ฟผ๋ฆฌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/257
| 83์ฐจ์‹œ | 2024.12.01 | ์ˆ˜ํ•™ + ๊ตฌํ˜„ | <a href="https://www.acmicpc.net/problem/1033">์นตํ…Œ์ผ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/259
---
98 changes: 98 additions & 0 deletions tgyuuAn/์ˆ˜ํ•™/์นตํ…Œ์ผ.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
fun gcd(a: Int, b: Int): Int {
var tempA = maxOf(a, b)
var tempB = minOf(a, b)

if(tempB <= 0){
return 0
}

while (tempA % tempB >= 1){
val tempC = tempA%tempB
tempA = tempB
tempB = tempC
}

return tempB
}

fun main() {
val N = readln().toInt()
val ratio = IntArray(N){ 1 }
val linked = Array(N) { mutableListOf<Int>() }

repeat(N-1){
val info = readln().split(" ").map{ it.toInt() }
val a = info[0]
val b = info[1]
var p = info[2]
var q = info[3]
val divGcd = gcd(p, q)
p /= divGcd
q /= divGcd

// println("$a $b $p $q")

val aLinked = mutableSetOf<Int>(a)
val aDeq = ArrayDeque<Int>()
aDeq.addFirst(a)
while(!aDeq.isEmpty()){
val now = aDeq.removeFirst()

for(neighbor in linked[now]){
if(neighbor in aLinked){
continue
}

aLinked.add(neighbor)
aDeq.addLast(neighbor)
}
}

val bLinked = mutableSetOf<Int>(b)
val bDeq = ArrayDeque<Int>()
bDeq.addFirst(b)
while(!bDeq.isEmpty()){
val now = bDeq.removeFirst()

for(neighbor in linked[now]){
if(neighbor in bLinked){
continue
}

bLinked.add(neighbor)
bDeq.addLast(neighbor)
}
}

// println("$aLinked $bLinked")

val tempARatio = ratio[a]
val tempBRatio = ratio[b]

for(aNeighbor in aLinked){
ratio[aNeighbor] *= (tempBRatio * p)
}

for(bNeighbor in bLinked){
ratio[bNeighbor] *= (tempARatio * q)
}

linked[a].add(b)
linked[b].add(a)

// println(linked.toList().toString())
// println(ratio.toList().toString())
// println()
}

val allGcd = ratio.reduce(::gcd)
for(elemIdx in 0 until ratio.size){
ratio[elemIdx] /= allGcd
}

println(ratio.toList()
.toString()
.replace(",", "")
.replace("[", "")
.replace("]", ""))
}