How Many Answers Are Wrong
题目传送门
题目大意
给你一个区间长度n,以及m个子区间
子区间有x,y,z三个参数,代表
[
a
,
b
]
[a,b]
[a,b]区间的和为z
如果一个子区间和与前面的子区间和冲突,则错误
求错误的子区间的个数
思路
参考大佬的详细做法吧,HDU 3038 带权并查集判错
我的区间表示的是
[
a
,
b
)
[a,b)
[a,b),一样的可以
AC Code
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=2e5 +9;
int n, m;
int fa[N], runkx[N];
void init(){
for(int i=1; i<=n+1; i++) fa[i]=i, runkx[i]=0;
return ;
}
int find(int u){
if(fa[u]==u) return u;
int tmp=find(fa[u]);
runkx[u]+=runkx[fa[u]];
fa[u]=tmp;
return fa[u];
}
void solve(){
while(cin>>n>>m){
int x, y, z;
init();
int ans=0;
while(m--){
cin>>x>>y>>z;
y++;
int fx=find(x);
int fy=find(y);
if(fx!=fy){
fa[fy]=fx;
runkx[fy]=runkx[x]+z-runkx[y];
}
else if(runkx[y]-runkx[x]!=z) ans++;
}
cout<<ans<<endl;
}
return ;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
solve();
return 0;
}