Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 100010;
- using pii = pair<int, int>;
- vector<pii> G[N];
- int mx = 0;
- void dfs(int u, int p, int d) { // u = current node, p = parent, d = current distance
- // don't need to check visited because graph is a tree, there's only one way to go back (through parent)
- mx = max(mx, d);
- for (auto v : G[u]) {
- if (v.first != p) // don't go back to parent
- dfs(v.first, u, d+v.second); // go from u to v, so distance += weight of edge (u,v)
- }
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- for (int i = 0; i < n-1; ++i) {
- int u, v, w;
- scanf("%d%d%d", &u, &v, &w);
- G[u].push_back({v, w});
- G[v].push_back({u, w});
- }
- dfs(1, 0, 0);
- printf("%d\n", mx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement