How Many Answers Are Wrong

HDU - 3038 

 1 #include<iostream>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 const int N=200005;
 8 int n,m,a,b,w;
 9 int f[N],sum[N];
10 
11 int getf(int u)
12 {
13     if(u==f[u]) return u;
14     else
15     {
16         int fa=getf(f[u]);
17         sum[u]+=sum[f[u]]; //路径压缩的过程中把经过的都加起来 
18         f[u]=fa;
19         return f[u];
20     }
21 }
22 
23 int main()
24 {
25     while(cin>>n>>m)
26     {
27         int cnt=0;
28         memset(sum,0,sizeof(sum)); 
29         for(int i=0;i<=n;i++)
30             f[i]=i;
31         for(int i=1;i<=m;i++)
32         {
33             cin>>a>>b>>w;
34             a--;//对于A~B之间的和是S,其实可以理解成B比A-1大S;
35             int x=getf(a);
36             int y=getf(b);
37             if(x!=y)
38             {
39                 f[y]=x;
40                 sum[y]=sum[a]+w-sum[b];
41             }
42             else
43             {
44                 if(sum[b]-sum[a]!= w)
45                     cnt++;
46             }
47         }
48         cout<<cnt<<endl;
49     } 
50 }

猜你喜欢

转载自www.cnblogs.com/thunder-110/p/9026430.html