Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-Redish03 #59

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Redish03/DFS/1167.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>

using namespace std;

vector<vector<pair<int, int>>> tree(100001);
int max_dist = -99;
vector<bool> 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;
}
1 change: 1 addition & 0 deletions Redish03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |

---