变形并查集 (扩展域并查集 | 权值并查集)

一重扩展域并查集

/*
*扩展域并查集 二分图 容许问题 
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
static constexpr int N = 10,M = 20;
static int rep[N<<1|1]{0};
inline constexpr int __abs(const int x){return x<0?-x:x;}
int __find(int x){return (rep[x]<0)?x:(rep[x] = __find(rep[x]));}
inline void __union(int rx,int ry)
{
	if(__abs(rep[rx])<=__abs(rep[ry]))
		rep[rx] = ry,rep[ry]-=(int)(rep[rx] == rep[ry]);
	else rep[ry] = rx;
} 
_Bool SOLVE(void)
{
	memset(rep,-1,sizeof rep);
	int i,x,y,rx,ry;
	for(i = 0;i++<M;)
	{
		scanf("%d%d",&x,&y);
		if((rx = __find(x)) == (ry = __find(y)))return 0;
		__union(rx,y+N);__union(x+N,ry);
	}
	return 1;
}

权值并查集

/*
*加权并查集 
*/
#include <vector>
static constexpr int N = 10;
static std::vector<int> par(N+1,0),val(N+1,0);
static int w[N][N]{{}};
static void init(void){int i = 0;for(auto&e:par)e = ++i;}
static int _Find(int f)
{
	if(f == par[f])return f;
	int r = _Find(par[f]);val[f]+=val[par[f]];
	return (par[f] = r);
}
static void _Mrg(int x,int y)
{
	int rx{0},ry{0};
	if((rx = _Find(x)) == (ry = _Find(y)))return;
	par[rx] = ry;
	val[rx] = -val[x]+val[y]+w[x][y];
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XNB's Not a Beginner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值