标签:带权并查集 size ace 数据 -o amp main hdu scanf
http://acm.hdu.edu.cn/showproblem.php?pid=3038
$1\leq n \leq 100$
#include<bits/stdc++.h> #define ll long long #define pii pair<int,int> using namespace std; const int maxn=2e5+7; const int mod=1e9+7; ll offe[maxn],boss[maxn]; int fin(int x){ if(x==boss[x])return x; int z=boss[x]; int y=fin(boss[x]); offe[x]+=offe[z]; boss[x]=y; return y; } int main() { int n,m,ans=0; while(scanf("%d %d",&n,&m)==2){ ans=0; for(int i=0;i<=n;i++)boss[i]=i,offe[i]=0; for(int i=1;i<=m;i++){ int l,r,x; scanf("%d %d %d",&l,&r,&x); l--; if(fin(l)==fin(r)){ if(offe[l]-offe[r]!=x)ans++; }else{ int fl=fin(l),fr=fin(r); boss[fl]=fr; offe[fl]=x-offe[l]+offe[r]; } } printf("%d\n",ans); } return 0; }
标签:带权并查集 size ace 数据 -o amp main hdu scanf
原文地址:https://www.cnblogs.com/carcar/p/12290373.html