How Many Answers Are Wrong(带权并查集)

How Many Answers Are Wrong

题目传送门

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值