From abae4be12cc30b1b7c9bb6c3a7ddd5818bfbacaf Mon Sep 17 00:00:00 2001 From: SeongWooHong Date: Fri, 16 Feb 2024 01:00:52 +0900 Subject: [PATCH] 2024-02-15 --- Redish03/DFS/1167.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ Redish03/README.md | 1 + 2 files changed, 61 insertions(+) create mode 100644 Redish03/DFS/1167.cpp diff --git a/Redish03/DFS/1167.cpp b/Redish03/DFS/1167.cpp new file mode 100644 index 0000000..3111ced --- /dev/null +++ b/Redish03/DFS/1167.cpp @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; + +vector>> tree(100001); +int max_dist = -99; +vector path; +int max_node = 0; + +void dfs(int node, int distance) +{ + if (path[node]) + { + return; + } + if (distance > max_dist) + { + max_dist = distance; + max_node = node; + } + path[node] = true; + for (int i = 0; i < tree[node].size(); i++) + { + int nnode = tree[node][i].first; + int ndist = tree[node][i].second; + dfs(nnode, distance + ndist); + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + int V; + cin >> V; + int a, node, dist; + path.resize(V + 1, false); + + for (int i = 0; i < V; i++) + { + cin >> a; + while (true) + { + cin >> node; + if (node == -1) + break; + cin >> dist; + + tree[a].push_back({node, dist}); + } + } + dfs(1, 0); + path.assign(V + 1, false); + dfs(max_node, 0); + + cout << max_dist; +} \ No newline at end of file diff --git a/Redish03/README.md b/Redish03/README.md index 6490b3e..2326e12 100644 --- a/Redish03/README.md +++ b/Redish03/README.md @@ -16,5 +16,6 @@ | 12차시 | 2024.02.03 | DP | [구간 합 구하기 5](https://www.acmicpc.net/problem/11660) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/47) | | 13차시 | 2024.02.06 | Back tracking | [N과 M(9)](https://www.acmicpc.net/problem/15663) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/52) | | 14차시 | 2024.02.12 | KnapSack(DP) | [평범한 배낭](https://www.acmicpc.net/problem/12865) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/55) | +| 15차시 | 2024.02.15 | DFS | [트리의 지름](https://www.acmicpc.net/problem/1167) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/55) | ---