From 7e2104cb87ed42f974914ff93e2f8459e099990a Mon Sep 17 00:00:00 2001 From: tgyuuAn Date: Sun, 1 Dec 2024 18:22:52 +0900 Subject: [PATCH] 2024-12-01 --- tgyuuAn/README.md | 4 +- .../\354\271\265\355\205\214\354\235\274.kt" | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 "tgyuuAn/\354\210\230\355\225\231/\354\271\265\355\205\214\354\235\274.kt" diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 1739d158..d65ae460 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -55,9 +55,6 @@ | 51차시 | 2024.04.07 | BFS | 과외맨 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179 | 52차시 | 2024.05.06 | 위상정렬 | 게임 개발 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182 | 53차시 | 2024.05.09 | 백트래킹 | 2048 (Easy) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184 -| 51차시 | 2024.04.07 | BFS | 과외맨 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179 -| 52차시 | 2024.05.06 | 위상정렬 | 게임 개발 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182 -| 53차시 | 2024.05.09 | 백트래킹 | 2048 (Easy) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184 | 54차시 | 2024.05.14 | 다익스트라 | 미확인 도착지 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/185 | 55차시 | 2024.05.18 | 유니온 파인드 | 친구 네트워크 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/189 | 56차시 | 2024.05.18 | BFS | 공주님을 구해라! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193 @@ -87,4 +84,5 @@ | 78차시 | 2024.10.06 | 그리디 | 풍선 터뜨리기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/250 | 79차시 | 2024.10.12 | 이분 매칭 | 책 나눠주기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/251 | 80차시 | 2024.11.11 | 다익스트라 | 최소비용 구하기 2 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/254 +| 83차시 | 2024.12.01 | 수학 + 구현 | 칵테일 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/259 --- diff --git "a/tgyuuAn/\354\210\230\355\225\231/\354\271\265\355\205\214\354\235\274.kt" "b/tgyuuAn/\354\210\230\355\225\231/\354\271\265\355\205\214\354\235\274.kt" new file mode 100644 index 00000000..52eb880e --- /dev/null +++ "b/tgyuuAn/\354\210\230\355\225\231/\354\271\265\355\205\214\354\235\274.kt" @@ -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() } + + 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(a) + val aDeq = ArrayDeque() + 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(b) + val bDeq = ArrayDeque() + 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("]", "")) +} \ No newline at end of file