From cb59cbfc86500e46819d70f6cade35bf412ed03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=8A=B9=ED=98=B8?= <132066506+YIM2UL2ET@users.noreply.github.com> Date: Fri, 19 Apr 2024 17:26:15 +0900 Subject: [PATCH] =?UTF-8?q?19=EC=B0=A8=EC=8B=9C=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YIM2UL2ET/README.md | 1 + .../19\354\260\250\354\213\234 - BOJ1967.cpp" | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 "YIM2UL2ET/\355\212\270\353\246\254/19\354\260\250\354\213\234 - BOJ1967.cpp" diff --git a/YIM2UL2ET/README.md b/YIM2UL2ET/README.md index 57c0ce8..8c21ee2 100644 --- a/YIM2UL2ET/README.md +++ b/YIM2UL2ET/README.md @@ -20,4 +20,5 @@ | 16차시 | 2024.04.11 | 애드 혹 | [BOJ 15927](https://www.acmicpc.net/problem/15927) | [BOJ 15927 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/51) | | 17차시 | 2024.04.15 | DP | [BOJ 11726](https://www.acmicpc.net/problem/11726) | [BOJ 11726 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/53) | | 18차시 | 2024.04.15 | 트리 | [BOJ 5639](https://www.acmicpc.net/problem/5639) | [BOJ 5639 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/53) | +| 19차시 | 2024.04.15 | 트리 | [BOJ 1967](https://www.acmicpc.net/problem/1967) | [BOJ 1967 풀이](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/53) | --- \ No newline at end of file diff --git "a/YIM2UL2ET/\355\212\270\353\246\254/19\354\260\250\354\213\234 - BOJ1967.cpp" "b/YIM2UL2ET/\355\212\270\353\246\254/19\354\260\250\354\213\234 - BOJ1967.cpp" new file mode 100644 index 0000000..1aa0e73 --- /dev/null +++ "b/YIM2UL2ET/\355\212\270\353\246\254/19\354\260\250\354\213\234 - BOJ1967.cpp" @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +int maxValue, maxNode; + +struct node { + bool visit = false; + vector > link; +}; + +void dfs(vector &tree, int nd, int val) { + tree[nd-1].visit = true; + if (val > maxValue) maxValue = val, maxNode = nd; + for (auto i : tree[nd-1].link) { + if (!tree[i.first-1].visit) dfs(tree, i.first, val+i.second); + } +} + +int main(void) { + int n, parent, child, value; + + cin >> n; + vector tree(n); + + for (int i = 0; i < n-1; i++) { + cin >> parent >> child >> value; + tree[parent-1].link.push_back({child, value}); + tree[child-1].link.push_back({parent, value}); + } + + auto copyTree = tree; + dfs(copyTree, 1, 0); + dfs(tree, maxNode, 0); + cout << maxValue; + return 0; +} \ No newline at end of file