You are viewing a single comment's thread. Return to all comments →
c++
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef pair<long long, long long> PII; long long n,x,y,z,i,ans1,ans2; vector<PII> edge[500007]; void DFS1(long long bef, long long pos, long long sum) { long long i; for (i=0 ; i<edge[pos].size() ; i++) if (edge[pos][i].first != bef) { DFS1(pos,edge[pos][i].first,sum + edge[pos][i].second); } ans1 += sum; } void DFS2(long long bef, long long pos, long long sum) { long long i; for (i=0 ; i<edge[pos].size() ; i++) if (edge[pos][i].first != bef) { DFS2(pos,edge[pos][i].first,sum + edge[pos][i].second); } ans2 += sum; } int main() { scanf("%lld",&n); for (i=1 ; i<=n-1 ; i++) { scanf("%lld%lld%lld",&x,&y,&z); edge[x].push_back(PII(y,z)); edge[y].push_back(PII(x,z)); } DFS1(-1,1,0); DFS2(-1,n,0); //printf("%lld %lld\n",ans1,ans2); printf("%lld\n",min(ans1,ans2)); return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Tree Flow
You are viewing a single comment's thread. Return to all comments →
c++