HDU 3234 Exclusive-OR

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3234


Exclusive-OR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3299    Accepted Submission(s): 908



Problem Description

You are not given n non-negative integers X0, X1, ..., Xn-1 less than 220 , 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 220. 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

 

Recommend

chenrui

思路:加权的并查集。难度不小,自己没能够写出来。看了下面的两篇博客才明白过来,自己手敲了一遍。详见代码。

http://blog.csdn.net/acm_cxlove/article/details/8101710

http://blog.csdn.net/libin56842/article/details/46509325


附上AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20005;
int p[maxn], xorv[maxn], num[20];
bool vis[20];
int n, q;
char op[105];

int _find(int x){
	if (p[x] != x){
		int fx = p[x];
		p[x] = _find(p[x]);
		xorv[x] ^= xorv[fx];
	}
	return p[x];
}

bool _union(int x, int y, int z){
	int fx=_find(x), fy=_find(y);
	if (fx == fy){
		if ((xorv[x]^xorv[y]) != z)
			return false;
		return true;
	}
	if (fx == n)
		swap(fx, fy);
	p[fx] = fy;
	xorv[fx] = xorv[x]^xorv[y]^z;
	return true;
}

int query(int cnt){
	int ans = 0;
	memset(vis, false, sizeof(bool)*cnt);
	for (int i=0; i<cnt; ++i){
		if (vis[i])
			continue;
		int t = 0;
		int root = _find(num[i]);
		for (int j=i; j<cnt; ++j)
			if (!vis[j] && root==_find(num[j])){
				vis[j] = true;
				++t;
				ans ^= xorv[num[j]];
			}
		if (root!=n && (t&1))
			return -1;
	}
	return ans;
}

int main(){
	int cas = 0;
	while (~scanf("%d%d", &n, &q) && n+q){
		printf("Case %d:\n", ++cas);
		int a, b, c, id=0;
		bool ok = false;
		for (int i=0; i<=n; ++i){
			p[i] = i;
			xorv[i] = 0;
		}
		while (q--){
			if (ok){
				fgets(op, 105, stdin);
				continue;
			}
			int cmd;
			while (true){
				cmd = getchar();
				if (cmd=='I' || cmd=='Q')
					break;
			}
			if (cmd == 'I'){
				++id;
				fgets(op, 105, stdin);
				if (sscanf(op, "%d%d%d", &a, &b, &c) == 2){
					c = b;
					b = n;
				}
				if (!_union(a, b, c)){
					printf("The first %d facts are conflicting.\n", id);
					ok = true;
				}
			}
			else{
				scanf("%d", &a);
				for (int i=0; i<a; ++i)
					scanf("%d", num+i);
				int ans = query(a);
				if (ans == -1)
					puts("I don't know.");
				else
					printf("%d\n", ans);
			}
		}
		puts("");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值