Skip to content

Commit

Permalink
70-9kyo-hwang
Browse files Browse the repository at this point in the history
  • Loading branch information
9kyo-hwang committed Nov 2, 2024
1 parent e3cb994 commit d019461
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <string>
#include <vector>

using namespace std;

vector<vector<int>> Tree;
pair<int, int> DFS(const vector<int>& 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<int> InSales, vector<vector<int>> InLinks)
{
Tree.resize(InSales.size() + 1);
for(const vector<int>& 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);
}
3 changes: 2 additions & 1 deletion 9-kyo-hwang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| 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) |

0 comments on commit d019461

Please sign in to comment.