HDU3234&&UVA12232&&LA4487:Exclusive-OR(经典带权并查集)

Problem Description
You are  not given  n non-negative integers  X0, X1, ..., Xn-1 less than 2 20 , but they do exist, and their values never change.

I'll gradually provide you some facts about them, and ask you some questions.

There are two kinds of facts, plus one kind of question:

 

Input
There will be at most 10 test cases. Each case begins with two integers  n and  Q (1 <=  n <= 20,000, 2 <=  Q <= 40,000). Each of the following lines contains either a fact or a question, formatted as stated above. The  k parameter in the questions will be a positive integer not greater than 15, and the  v parameter in the facts will be a non-negative integer less than 2 20. The last case is followed by  n=Q=0, which should not be processed.
 

Output
For each test case, print the case number on its own line, then the answers, one on each one. If you can't deduce the answer for a particular question, from the facts I provide you  before that question, print "I don't know.", without quotes. If the  i-th fact (don't count questions)  cannot be consistent with  all the facts before that, print "The first i facts are conflicting.", then keep silence for everything after that (including facts and questions). Print a blank line after the output of each test case.
 

Sample Input
  
  
2 6 I 0 1 3 Q 1 0 Q 2 1 0 I 0 2 Q 1 1 Q 1 0 3 3 I 0 1 6 I 0 2 2 Q 2 1 2 2 4 I 0 1 7 Q 2 0 1 I 0 1 8 Q 2 0 1 0 0
 

Sample Output
  
  
Case 1: I don't know. 3 1 2 Case 2: 4 Case 3: 7 The first 2 facts are conflicting.
 

Source
 


题意:
对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题

I P V :告诉你a[P] = V
I P Q V:告诉你a[P] XOR a[Q] = V
Q K P1..PK:询问a[P1]^a[P2]^...a[PK]的值

思路:
很经典的并查集题目
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的异或都是本身
这样一来合并问题就解决了

然后对于查询而言,对于n是根节点的我们不需要考虑,但是对于不是以n为跟节点的,根节点的值被重复计算了,这个时候我们要统计根节点被统计的次数,如果是奇数次,那么必然是无法确定的了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 20005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;

int n,m,tot;
int father[N],r[N],num[N],u,v,w,vis[N];
char str[100];

int find(int x)
{
    if(x!=father[x])
    {
        int fx = father[x];
        father[x] = find(father[x]);
        r[x]^=r[fx];
    }
    return father[x];
}

bool Union(int a,int b,int c)
{
    int fa = find(a),fb=find(b);
    if(fa==fb)
    {
        if((r[a]^r[b])!=c) return false;
        return true;
    }
    if(fa==n) swap(fa,fb);
    father[fa] = fb;
    r[fa] = r[a]^r[b]^c;
    return true;
}

int query()
{
    int i,j,cnt,ans = 0;
    MEM(vis,0);
    for(i = 0; i<tot; i++)
    {
        if(vis[i]) continue;
        cnt = 0;
        int root = find(num[i]);
        for(j = i; j<tot; j++)
        {
            if(!vis[j] && root == find(num[j]))
            {
                vis[j] = 1;
                cnt++;
                ans^=r[num[j]];
            }
        }
        if(root!=n&&(cnt&1))
        {
            return -1;
        }
    }
    return ans;
}

int main()
{
    int i,j,k,cas = 1;
    while(~scanf("%d%d",&n,&m),n+m)
    {
        printf("Case %d:\n",cas++);
        for(i = 0; i<=n; i++)
        {
            father[i] = i;
            r[i] = 0;
        }
        bool flag = false;
        int fac = 0;
        while(m--)
        {
            scanf("%s",str);
            if(str[0]=='I')
            {
                getchar();
                gets(str);
                fac++;
                int cnt = 0;
                for(i = 0; str[i]!='\0'; i++)
                {
                    if(str[i]==' ')
                        cnt++;
                }
                if(cnt == 1)
                {
                    sscanf(str,"%d%d",&u,&w);
                    v = n;
                }
                else
                {
                    sscanf(str,"%d%d%d",&u,&v,&w);
                }
                if(flag)
                    continue;
                if(!Union(u,v,w))
                {
                    printf("The first %d facts are conflicting.\n",fac);
                    flag = true;
                }
            }
            else
            {
                scanf("%d",&tot);
                for(i = 0; i<tot; i++)
                    scanf("%d",&num[i]);
                if(flag) continue;
                int ans = query();
                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、付费专栏及课程。

余额充值