Zjnu Stadium 题解
题目描述:给出A,B,X,表示编号为B的观众必须坐在编号为A观众顺时针方向X距离的位置,找有多少个与之前冲突的要求
思路:我们可以用带权并查集,如果两个人不在同一集合内,则把他两合并到同一集合,并计算距离,如果在,就看他们的距离是不是z,如果不是,就是不合理的,答案就要加一
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int w[100010],f[100010];
int gf(int x){
if(f[x]==x){
return x;
}
int t=gf(f[x]);
w[x]+=w[f[x]];
return f[x]=t;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
while(cin>>n>>m){
int ans=0;
for(int i=1; i<=n; i++){
f[i]=i,w[i]=0;
}
for(int i=1; i<=m; i++){
int x,y,z;
cin>>x>>y>>z;
int xa=gf(x),ya=gf(y);
if(xa!=ya){
f[ya]=xa;
w[ya]=w[x]+z-w[y];
}else{
if(w[y]-w[x]!=z){
ans++;
}
}
}
cout<<ans<<"\n";
}
return 0;
}