#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x))
const int maxn = 200000 + 100;
int fa[maxn],sum[maxn];
void ini(int n){
CLR(sum,0);
for(int i = 0; i <= n;++i){
fa[i] = i;
}
}
int fnd(int x){
if(x == fa[x]) return x;
int fax = fa[x];
fa[x] = fnd(fa[x]);
sum[x] += sum[fax];
return fa[x];
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(~scanf("%d%d",&n,&m)){
int u,v,tsum,ans = 0;
ini(n);
FOR(i,0,m){
scanf("%d%d%d",&u,&v,&tsum);
int t1,t2;
--u;
t1 = fnd(u);
t2 = fnd(v);
if(t1 == t2){
ans += (sum[v] - sum[u] != tsum);
}else {
fa[t2] = t1;
sum[t2] = tsum + sum[u] - sum[v];
}
}
printf("%d\n",ans);
}
return 0;
}
[2016-03-16][HDU][3038][How Many Answers Are Wrong]
原文:http://www.cnblogs.com/qhy285571052/p/c9c558c3de1a578d373cecd26c441d27.html