How Many Answers Are Wrong

Posted Thunder-ray

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于How Many Answers Are Wrong的主要内容,如果未能解决你的问题,请参考以下文章

How Many Answers Are Wrong

hdu-3038 How Many Answers Are Wrong[并查集]

HDU 3038 How Many Answers Are Wrong (带权并查集)

HDU 3038 How Many Answers Are Wrong(带权并查集)

HDU3038 How Many Answers Are Wrong[带权并查集]

hdu 3038 How Many Answers Are Wrong (带权并查集)