BZOJ2435: [Noi2011]道路修建
这种水题真是……没一次AC都不好意思见人啊
P.S. LINUX无限栈真是爽炸了… 我爱递归
1 /************************************************************** 2 Problem: 2435 3 User: zhuohan123 4 Language: C++ 5 Result: Accepted 6 Time:5320 ms 7 Memory:93192 kb 8 ****************************************************************/ 9 10 #include <iostream> 11 #include <cstdio> 12 using namespace std; 13 inline int iabs(int a){return a<0?-a:a;} 14 inline int getnum() 15 { 16 int ans=0;char ch=getchar(); 17 while(ch<'0'||ch>'9')ch=getchar(); 18 while(ch>='0'&&ch<='9')ans=ans*10+ch-'0',ch=getchar(); 19 return ans; 20 } 21 int n; 22 struct point{int head,f,size;}p[1100000]; 23 struct edge{int next,to,c;}g[2100000];int gnum; 24 void addedge(int from,int to,int c) 25 { 26 g[++gnum].to=to;g[gnum].c=c;g[gnum].next=p[from].head;p[from].head=gnum; 27 } 28 long long ans=0; 29 void dfs(int po) 30 { 31 p[po].size++; 32 for(int i=p[po].head;i;i=g[i].next) 33 if(g[i].to!=p[po].f) 34 { 35 p[g[i].to].f=po; 36 dfs(g[i].to); 37 p[po].size+=p[g[i].to].size; 38 ans+=(long long)g[i].c*iabs(n-2*p[g[i].to].size); 39 } 40 } 41 int main(int argc, char *argv[]) 42 { 43 n=getnum(); 44 for(int i=1;i<n;i++) 45 { 46 int u=getnum(),v=getnum(),c=getnum(); 47 addedge(u,v,c);addedge(v,u,c); 48 } 49 dfs(1); 50 cout<<ans<<endl; 51 return 0; 52 }