fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  4. #define ll long long
  5. const int N = 2e5 + 9;
  6. int dx[] = {1,-1,0,0,1,1,-1,-1};
  7. int dy[] = {0,0,1,-1,1,-1,1,-1};
  8. ll n, m, u, v, cost;
  9. ll ans = 1e18, sum;
  10. vector<pair<int,int> >adj[N];
  11. ll in[N], out[N], parent[N] ;
  12. map<int,bool>vis;
  13. ll DFS (int node, int costt)
  14. {
  15. int leaf_node = 1 ;
  16. vis[node] = 1 ;
  17. for(auto child : adj[node])
  18. {
  19. int next_node = child.first;
  20. int need = child.second;
  21. if(!vis[next_node])
  22. {
  23. leaf_node = 0 ;
  24. DFS(next_node , costt + need);
  25. }
  26. }
  27. if(leaf_node==1)
  28. ans = min (ans, sum-costt);
  29. return ans ;
  30. }
  31. int main()
  32. {
  33. IO
  34. cin>>n;
  35. for(int i=1 ; i<n ; i++)
  36. {
  37. cin>>u>>v>>cost;
  38. sum += cost;
  39. adj[u].push_back({v,cost});
  40. adj[v].push_back({u,cost});
  41. }
  42. sum*=2;
  43. cout<<DFS(1,0);
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 9788KB
stdin
Standard input is empty
stdout
Standard output is empty