LA4487 加权并查集

点击打开链接

异或具有很多奇妙的性质: 

aa ^  bb =  bb ^  aa; 
- ( aa ^  bb) ^  cc =  aa ^ ( bb ^  cc); 
- 若 aa ^  bb =  cc,那么 aa ^  cc =  bb

XpXp XORXOR XqXq = vv,那么我们可以把XpXpXqXq连一条边,权值为vv。 
XpXp = vv,那么就有XpXp XORXOR 00 = vv,所以我们可以把XpXpn+1n+1连一条边,权值为vv

所以我们就成功处理完了信息。 
那么怎样回答呢?

对于有相同根节点的节点a1,a2,a3,ama1,a2,a3…,am,设它们与根结点的距离为wiwi,则 
a1a1 ^ a2a2 ^ a3a3 ^ … ^ amam 
=rootroot ^ w1w1 ^ rootroot ^ w2w2 ^ … ^ rootroot ^ wmwm

综上所述, 
- 若rootroot的个数为奇数,及m为奇数,则这些节点的异或值为rootroot ^ w1w1 ^ w2w2 ^ … ^ wmwm; 
- 若rootroot的个数为偶数,及m为偶数,则这些节点的异或值为w1w1 ^ w2w2 ^ … ^ wmwm

mm为奇数且根结点的序号不为n+1n+1,那么就标志着答案不能唯一确定; 

否则就继续遍历下一个集合。

点击打开链接

r[a]记录的是a与其父亲节点的异或值
通过并查集合并的过程同时更新路径上的r值
令fa,fb分别是a,b,的父亲节点
我们知道r[a] = a^fa,r[b]=b^fa,a^b=c
那么在合并fa,fb的时候,令fb为fa的父节点
那么就有r[fa]=fa^fb
而r[a]=a^fa,所以fa=a^r[a]
同理fb=b^r[b]
所以r[fa] = r[a]^r[b]^a^b = r[a]^r[b]^c
当然对于一个节点合并问题不好解决,此时可以添加一一个新节点n作为根节点,使得n的值为0,那么任何值与0的异或都是本身
这样一来合并问题就解决了

/**
LA 4487:加权并查集
https://blog.csdn.net/XY20130630/article/details/50638922
https://blog.csdn.net/libin56842/article/details/46509325
*/
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<stdio.h>
using namespace std;
typedef long long int ll;
const int maxn = 20000;
int n,m,tot,num[25],fa[maxn+5],r[maxn+5],vis[205];
void getnum(char str[],int &a,int &b,int &c)
{
    int i;
    a = 0,b=0,c=0;
    for(i=0; str[i]!=' '&&str[i]!='\0'; i++) a = a*10 + str[i]-'0';
    for(i++; str[i]!=' '&&str[i]!='\0'; i++) b = b*10+str[i]-'0';
    if(str[i]=='\0') c = -1;
    if(c==-1) return;
    for(i++; str[i]!='\0'; i++) c = c*10+str[i]-'0';
}
int findset(int x)
{
    if(fa[x] == x) return x;
    int fx = fa[x];
    fa[x] = findset(fa[x]);
    r[x] ^= r[fx];
    return fa[x];
}
bool Merge(int u,int v,int z)
{
    int fx = findset(u), fy = findset(v);
    if(fx==fy)
    {
        return ((r[u]^r[v]) == z);
    }
    if(fx==n) swap(fx,fy);
    fa[fx] = fy;
    r[fx] = r[u]^r[v]^z;
    return true;
}
int solve()
{
    int ans = 0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=tot;i++)
    {
        if(vis[i]) continue;
        int root = findset(num[i]), cnt = 0;
        for(int j=i;j<=tot;j++)
        {
            if(findset(num[j])!=root||vis[j]) continue;
            cnt++, vis[j] = 1;
            ans^=r[num[j]];
        }
        if(cnt%2&&root!=n) return -1;
    }
    return ans;
}
int main()
{
    int cas = 1;
    while(~scanf("%d %d",&n,&m)&&(n+m))
    {
        for(int i=0; i<=n; i++) fa[i] = i, r[i] = 0;
        printf("Case %d:\n",cas++);
        char str[15],S[5];
        int t=0,flag=0;
        while(m--)
        {
            int u,v,z;
            scanf("%s",S);
            if(S[0]=='I')
            {
                t++;
                getchar();
                gets(str);
                getnum(str,u,v,z);
                if(flag) continue;
                if(z==-1)
                {
                    if(!Merge(u,n,v))
                    {
                        printf("The first %d facts are conflicting.\n",t);
                        flag = 1;
                    }
                }
                else
                {
                    if(!Merge(u,v,z))
                    {
                        printf("The first %d facts are conflicting.\n",t);
                        flag = 1;
                    }
                }
            }
            else
            {
                scanf("%d",&tot);
                for(int i=1; i<=tot; i++) scanf("%d",&num[i]);
                if(flag) continue;
                int ans = solve();
                if(ans == -1) printf("I don't know.\n");
                else printf("%d\n",ans);
            }
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值