# 杭电hdu 3038 how many answers are wrong 并查集求解

#include <stdio.h>
#define MAX 200001

int father[MAX];
int sum[MAX];//存储从1到i相加的结果

int n,m;
int ai, bi, si;

void init()
{
for(int i = 0; i <= n; i ++){
father[i] = i;
sum[i] = 0;
}
}

int getfather(int x)
{
if(x == father[x])
return x;
int now = getfather(father[x]);//
sum[x] += sum[father[x]];//需要我研究的地方
father[x] = now;
return now;
}

bool join(int a, int b)
{
int fa = getfather(a);
int fb = getfather(b);
if(fa == fb)return false;
father[fb] = fa;
sum[fb] = sum[a] - sum[b] + si;//
return true;
}

int main()
{
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

while(~scanf("%d%d",&n,&m)){
init();
int ans = 0;
while(m--){
scanf("%d%d%d",&ai, &bi, &si);
ai --;
if(!join(ai, bi)&&sum[bi] - sum[ai] != si)ans ++;
}
printf("%d\n", ans);
}
return 0;
}

#### HDU3038 How Many Answers Are Wrong【巧妙并查集】

2017-07-10 16:25:53

#### hdu3038How Many Answers Are Wrong（并查集）

2016-01-31 09:50:45

#### HDU3038 How Many Answers Are Wrong (加权并查集)

2017-07-10 10:07:36

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

2012-09-22 09:49:30

#### HDU - 3038 How Many Answers Are Wrong（带权并查集）

2017-08-15 10:23:37

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

2016-03-29 20:59:03

#### HDU 3038 - How Many Answers Are Wrong

2016-03-04 19:01:29

#### HDU 1213 How Many Tables (并查集,连通分支数,两种方式)

2016-06-09 23:12:36

#### HDU - 3038(并查集)How Many Answers Are Wrong

2017-04-20 20:48:22

#### HDU - 3038 How Many Answers Are Wrong（并查集）

2017-08-16 19:03:43