带权并查集例题

link

int find(int x)
{
    if (p[x] != x)
    {
        int root = find(p[x]);
        d[x] += d[p[x]];
        p[x] = root;
    }
    return p[x];
}
 int fa = find(a), fb = find(b);
            d[fa] = si[fb];
            si[fb] += si[fa];
            p[fa] = fb;

link

int find(int x)
{
    if (p[x] != x)
    {
        int root = find(p[x]);
        d[x] =(d[x] +d[p[x]])%2;
        //d[x]=d[x]^d[y];
        p[x] = root;
    }
    return p[x];
}

int t;
int fa=find(a);
int fb=find(b);
if(fa==fb)
{
	if(d[a]^d[b]!==t)
	{
	cout<<i-1<<endl;
	break;
	}
}
if(fa!=fb)
{
	f[fa]=fb;
	d[fa]=d[a]^d[b]^t;
}

link

#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int>fa,bit;
int finds(int x){
    if(!fa.count(x))return x;
    int f=fa[x];
    fa[x]=finds(fa[x]);
    bit[x]^=bit[f];
    return fa[x];
}
void merge(int x,int y,int b){
    int fax=finds(x),fay=finds(y);
    if(fax==fay)return ;
    fa[fay]=fax;
    bit[fay]=bit[x]^bit[y]^b;
}
int query(int x,int y){
    int fax=finds(x),fay=finds(y);
    if(fax!=fay)return -1;
    return bit[y]^bit[x];
}
int main()
{
    int n;
    scanf("%d",&n);
    int la=0;
    while(n--){
        int op,x,y,b;
        scanf("%d%d%d",&op,&x,&y);
        x^=la,y^=la;
        if(x>y)swap(x,y);
        y++;
        if(op-2){
            scanf("%d",&b);
            b^=la;
            merge(x,y,b);
        }
        else{
            int ans=query(x,y);
            printf("%d\n",ans);
            la=~ans?ans:1;
        }
    }
    return 0;
}


link

          scanf("%d%d%d", &x, &y, &s);
			x = x-1;
			int a = Find(x);
			int b = Find(y);
			if(a!=b)
			{
				father[b] = a;
				dis[b] = dis[x] - dis[y] + s;
			}
			else
			{
				if(dis[y]-dis[x]!=s)	//判断是否满足和的关系
	                cnt++;
			}




蓝桥杯a组省赛 j

差分约束建图 bfs 或者dfs 搞一搞 差值关系就行了



#include <bits/stdc++.h>
using namespace  std;
#define  int long long
//typedef long long ll;
typedef pair<int,int> pii;
#define x first 
#define y second
#define pb  push_back
#define inf 1e18
#define IOS   std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define  fer(i,a,b)  for(int i=a;i<=b;i++)
#define  der(i,a,b)  for(int i=a;i>=b;i--) 
const int maxn=1e5+10;
const int mod=1e9+7;
int qmi(int a,int b)
{int res=1;  while(b){  if(b&1) res=res*a%mod;  a=a*a%mod;  b>>=1; } return res;}
const int N=2e5+10;
int dr[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int n,k;
int fa[N];
int vis[N];
int sum[N];
int m,q;
vector<pii>e[N];
int find(int x)
{
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void dfs(int u,int d)
{
   vis[u]=1;
   sum[u]=d;
   for(pii v:e[u])
   {
   	int t=v.x;
   	int w=v.y;
   	if(vis[t])continue;
   	dfs(t,d+w);
   }
}
void solve()
{
   cin>>n>>m>>q;
   fer(i,0,n)fa[i]=i;
   while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		e[a-1].pb({b,c});
		e[b].pb({a-1,-c});
		fa[find(a-1)] = find(b);
	}
fer(i,0,n)
if(!vis[i])dfs(i,0);
//fer(i,0,n)cout<<d[i]<<endl;
while(q--)
{
	int a,b;
	cin>>a>>b;
    a--;
	int x=find(a);
	int y=find(b);
	if(x!=y)puts("UNKNOWN");
	else cout<<sum[b]-sum[a]<<endl;
}
		
}

signed main()
{
	//IOS;
	int _=1;
	//cin>>_;
	while(_--) solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值