Skip to content

Commit

Permalink
2024-02-15
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish03 committed Feb 15, 2024
1 parent 299e904 commit abae4be
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
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) |

---

0 comments on commit abae4be

Please sign in to comment.