From d019461d417d084ffc2cad45e72505f2d0ddae02 Mon Sep 17 00:00:00 2001 From: 9kyo-hwang Date: Sat, 2 Nov 2024 23:42:30 +0900 Subject: [PATCH] 70-9kyo-hwang --- ... \354\265\234\354\206\214\355\231\224.cpp" | 53 +++++++++++++++++++ 9-kyo-hwang/README.md | 3 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 "9-kyo-hwang/Graph Traversal/\353\247\244\354\266\234 \355\225\230\353\235\275 \354\265\234\354\206\214\355\231\224.cpp" diff --git "a/9-kyo-hwang/Graph Traversal/\353\247\244\354\266\234 \355\225\230\353\235\275 \354\265\234\354\206\214\355\231\224.cpp" "b/9-kyo-hwang/Graph Traversal/\353\247\244\354\266\234 \355\225\230\353\235\275 \354\265\234\354\206\214\355\231\224.cpp" new file mode 100644 index 0000000..0bdf946 --- /dev/null +++ "b/9-kyo-hwang/Graph Traversal/\353\247\244\354\266\234 \355\225\230\353\235\275 \354\265\234\354\206\214\355\231\224.cpp" @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +vector> Tree; +pair DFS(const vector& InLosses, const int Head) +{ + const int LossIfHeadParticipation = InLosses[Head - 1]; + + if(Tree[Head].empty()) + { + return {0, LossIfHeadParticipation}; + } + + int TotalLosses = 0, MinLossDifference = 1e9; + bool bMemberParticipation = false; + + for(const int Member : Tree[Head]) + { + const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InLosses, Member); + TotalLosses += min(LossIfNotParticipation, LossIfParticipation); + + if(LossIfNotParticipation >= LossIfParticipation) + { + bMemberParticipation = true; + } + + MinLossDifference = min(MinLossDifference, LossIfParticipation - LossIfNotParticipation); + } + + if(bMemberParticipation) + { + return {TotalLosses, LossIfHeadParticipation + TotalLosses}; + } + else + { + return {TotalLosses + MinLossDifference, LossIfHeadParticipation + TotalLosses}; + } +} + +int solution(vector InSales, vector> InLinks) +{ + Tree.resize(InSales.size() + 1); + for(const vector& Link : InLinks) + { + int a = Link[0], b = Link[1]; + Tree[a].emplace_back(b); + } + + const auto& [LossIfNotParticipation, LossIfParticipation] = DFS(InSales, 1); + return min(LossIfNotParticipation, LossIfParticipation); +} \ No newline at end of file diff --git a/9-kyo-hwang/README.md b/9-kyo-hwang/README.md index 244478c..3a1df99 100644 --- a/9-kyo-hwang/README.md +++ b/9-kyo-hwang/README.md @@ -69,4 +69,5 @@ | 66차시 | 2024.9.14 | Sliding Window | [할인 행사](https://school.programmers.co.kr/learn/courses/30/lessons/131127) | [#225](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/225) | | 67차시 | 2024.9.23 | Graph Traversal | [퍼즐 조각 채우기](https://school.programmers.co.kr/learn/courses/30/lessons/84021) | [#228](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/228) | | 68차시 | 2024.10.1 | Greedy | [2024 KAKAO WINTER INTERNSHIP n + 1 카드 게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#230](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/230) | -| 69차시 | 2024.10.8 | Graph Traversal | [2022 KAKAO BLIND RECRUITMENT 사라지는 발판](https://school.programmers.co.kr/learn/courses/30/lessons/92345) | [#232](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/232) | \ No newline at end of file +| 69차시 | 2024.10.8 | Graph Traversal | [2022 KAKAO BLIND RECRUITMENT 사라지는 발판](https://school.programmers.co.kr/learn/courses/30/lessons/92345) | [#232](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/232) | +| 70차시 | 2024.11.2 | Graph Traversal | [2021 KAKAO BLIND RECRUITMENT 매출 하락 최소화](https://school.programmers.co.kr/learn/courses/30/lessons/72416) | [#233](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/233) |