NOI的。。做一遍dfs就好了?为什么在codevs会RE。。。。

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(i=l;i<=r;i++)
 3 #define dec(i,l,r) for(i=l;i>=r;i--)
 4 #define inf 1e9
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define ll long long
 7 #define succ(x) (1<<x)
 8 #define NM 2000000
 9 using namespace std;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
13     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
14     return x*f;
15 }
16 struct edge{
17     int t,v;
18     edge *next;
19 }e[2*NM],*h[NM];
20 int b[NM],n,_x,_y,t,i,s;
21 ll ans;
22 bool v[NM];
23 void add(int x,int y,int v){
24     e[++s].t=y;e[s].v=v;e[s].next=h[x];h[x]=&e[s];
25 }
26 void dfs(int x){
27     v[x]++;
28     for(edge *j=h[x];j;j=j->next)
29     if(!v[j->t]){
30         dfs(j->t);
31         b[x]+=b[j->t];
32         ans+=(ll)j->v*abs(n-2*b[j->t]);
33     }
34     b[x]++;
35 }
36 int main(){
37 //    freopen("test.in","r",stdin);
38     n=read();
39     inc(i,1,n-1){
40         _x=read();_y=read();t=read();
41         add(_x,_y,t);add(_y,_x,t);
42     }
43     dfs(1);
44     printf("%lld\n",ans);
45     return 0;
46 }
View Code

 

posted on 2015-10-12 19:17  onlyRP  阅读(153)  评论(0编辑  收藏  举报