标签:.com close image ble git app star group long
一个数n(1<=n<=100,000,n保证为偶数) 接下来n-1行每行三个数x,y,z表示有一条长度为z的边连接x和y(0<=z<=1,000,000,000)
一个数表示答案
6 1 2 1 1 3 1 1 4 1 3 5 1 4 6 1
7 //配对方案为(1,2)(3,4)(5,6)
1 #include <cstdio> 2 #include <cctype> 3 #include <vector> 4 5 typedef long long LL; 6 const int MAXN=100010; 7 8 int n; 9 10 LL ans; 11 12 int son[MAXN]; 13 14 struct node { 15 int to,val; 16 node() {} 17 node(int to,int val):to(to),val(val) {} 18 }; 19 20 std::vector<node> Graph[MAXN]; 21 22 inline void read(int&x) { 23 int f=1;register char c=getchar(); 24 for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar()); 25 for(;isdigit(c);x=x*10+c-48,c=getchar()); 26 x=x*f; 27 } 28 29 void DFS(int now,int fa) { 30 son[now]=1; 31 for(register int i=0;i<Graph[now].size();++i) { 32 node p=Graph[now][i]; 33 if(p.to==fa) continue; 34 DFS(p.to,now); 35 son[now]+=son[p.to]; 36 } 37 return; 38 } 39 40 void DFS_Ans(int now,int fa) { 41 for(register int i=0;i<Graph[now].size();++i) { 42 node p=Graph[now][i]; 43 if(p.to==fa) continue; 44 DFS_Ans(p.to,now); 45 ans+=(LL)(son[p.to]<n-son[p.to]?son[p.to]:(n-son[p.to]))*p.val; 46 } 47 return; 48 } 49 50 int hh() { 51 read(n); 52 for(register int x,y,z,i=1;i<n;++i) { 53 read(x);read(y);read(z); 54 Graph[y].push_back(node(x,z)); 55 Graph[x].push_back(node(y,z)); 56 } 57 DFS(1,0); 58 DFS_Ans(1,0); 59 printf("%lld\n",ans); 60 return 0; 61 } 62 63 int sb=hh(); 64 int main(int argc,char**argv) {;}
标签:.com close image ble git app star group long
原文地址:http://www.cnblogs.com/whistle13326/p/7593529.html