Skip to content

Commit

Permalink
Merge pull request #259 from AlgoLeadMe/83-tgyuuAn
Browse files Browse the repository at this point in the history
83-tgyuuAn
  • Loading branch information
tgyuuAn authored Dec 31, 2024
2 parents b941599 + 664c323 commit 00438a2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
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("]", ""))
}

0 comments on commit 00438a2

Please sign in to comment.