How many answers wrong HDOJ-3038 一个很好的博客:https://www.cnblogs.com/liyinggang/p/5327055.html #includeiostream#includecstdio#includecstring#includestring#includealgorithm#includecmathusing namespace std;const int maxn=200004;in
How many answers wrong
HDOJ-3038
一个很好的博客:https://www.cnblogs.com/liyinggang/p/5327055.html
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; const int maxn=200004; int n,m; int set[maxn]; int sum[200004]; int ans; int find(int x){ if(x==set[x]) return set[x]; else{ int parent=set[x]; set[x]=find(set[x]); sum[x]+=sum[parent]; return set[x]; } } void merge(int a,int b,int w){ a--; int ta=find(a); int tb=find(b); if(ta==tb){ if(sum[a]-sum[b]!=w) ans++; }else{ set[ta]=tb; sum[ta]=sum[b]-sum[a]+w; } } int main(){ while(cin>>n>>m){ for(int i=0;i<=n;i++){ sum[i]=0; set[i]=i; } ans=0; int x,y,z; for(int i=1;i<=m;i++){ cin>>x>>y>>z; merge(x,y,z); } cout<<ans<<endl; } return 0; }