Skip to content

Commit

Permalink
2024-12-01
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Dec 1, 2024
1 parent 4f2c6c0 commit 7e2104c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
4 changes: 1 addition & 3 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
| 51์ฐจ์‹œ | 2024.04.07 | BFS | <a href="https://www.acmicpc.net/problem/5213">๊ณผ์™ธ๋งจ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179
| 52์ฐจ์‹œ | 2024.05.06 | ์œ„์ƒ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/1516">๊ฒŒ์ž„ ๊ฐœ๋ฐœ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182
| 53์ฐจ์‹œ | 2024.05.09 | ๋ฐฑํŠธ๋ž˜ํ‚น | <a href="https://www.acmicpc.net/problem/12100">2048 (Easy)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184
| 51์ฐจ์‹œ | 2024.04.07 | BFS | <a href="https://www.acmicpc.net/problem/5213">๊ณผ์™ธ๋งจ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179
| 52์ฐจ์‹œ | 2024.05.06 | ์œ„์ƒ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/1516">๊ฒŒ์ž„ ๊ฐœ๋ฐœ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182
| 53์ฐจ์‹œ | 2024.05.09 | ๋ฐฑํŠธ๋ž˜ํ‚น | <a href="https://www.acmicpc.net/problem/12100">2048 (Easy)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184
| 54์ฐจ์‹œ | 2024.05.14 | ๋‹ค์ต์ŠคํŠธ๋ผ | <a href="https://www.acmicpc.net/problem/9370">๋ฏธํ™•์ธ ๋„์ฐฉ์ง€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/185
| 55์ฐจ์‹œ | 2024.05.18 | ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ | <a href="https://www.acmicpc.net/problem/4195">์นœ๊ตฌ ๋„คํŠธ์›Œํฌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/189
| 56์ฐจ์‹œ | 2024.05.18 | BFS | <a href="https://www.acmicpc.net/problem/17836">๊ณต์ฃผ๋‹˜์„ ๊ตฌํ•ด๋ผ!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193
Expand Down Expand Up @@ -87,4 +84,5 @@
| 78์ฐจ์‹œ | 2024.10.06 | ๊ทธ๋ฆฌ๋”” | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/68646">ํ’์„  ํ„ฐ๋œจ๋ฆฌ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/250
| 79์ฐจ์‹œ | 2024.10.12 | ์ด๋ถ„ ๋งค์นญ | <a href="https://www.acmicpc.net/problem/9576">์ฑ… ๋‚˜๋ˆ ์ฃผ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/251
| 80์ฐจ์‹œ | 2024.11.11 | ๋‹ค์ต์ŠคํŠธ๋ผ | <a href="https://www.acmicpc.net/problem/11779">์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/254
| 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 7e2104c

Please sign in to comment.