Skip to content

Commit

Permalink
19차시 업로드
Browse files Browse the repository at this point in the history
  • Loading branch information
YIM2UL2ET committed Apr 19, 2024
1 parent cd98166 commit cb59cbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions YIM2UL2ET/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
---
37 changes: 37 additions & 0 deletions YIM2UL2ET/트리/19차시 - BOJ1967.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
using namespace std;

int maxValue, maxNode;

struct node {
bool visit = false;
vector <pair <int, int>> link;
};

void dfs(vector <node> &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 <node> 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;
}

0 comments on commit cb59cbf

Please sign in to comment.