Description

You are not given n non-negative integersX0,X1,...,
Xn-1 less than220, 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:

Format Meaning
I
p v
I tell you
Xp = v
I
p q v
I tell you
Xp XOR Xq = v
Q
k p1 p2...pk
Please tell me the value of
Xp1 XOR Xp2 XOR...XOR Xpk

Input

There will be at most 10 test cases. Each case begins with two integers
n and Q (1n20,
000, 2Q40,
000). Each of the following lines contains either a fact or a question, formatted as stated above. Thek parameter in the questions will be a positive integer not greater than 15, and thev
parameter in the facts will be a non-negative integer less than220. The last case is followed byn =
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 youbefore that question, print ``I don't know.",
without quotes. If thei-th fact (don't count questions)cannot be consistent with
all the facts before that, print ``The firsti 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.

题意:

有n(n<=20000)个未知的整数X0,X1,X2...Xn-1,有下面Q个(Q<=40000)操作:

    I p v :告诉你Xp=v

    I p q v :告诉你Xp Xor Xq=v

    Q k p1 p2 … pk : 询问 Xp1 Xor Xp2 .. Xor Xpk。 k不大于15。

    假设当前的I跟之前的有冲突的话,跳出   

思路:并查集题目,深深的感到没好好做并查集的无力感,知道是并查集却不知道怎么下手,说一下思路:

1.对于每次的询问,我们并不须要知道每一个数的大小也能够推出来结果,对于这样的: I p v

的我们能够虚拟一个数xn=0,这样就能够有通式p^q=v,由于p^0=p。

虚根xn是不能变的,它的子孙都有确定的值

2.我们如果偏移量val[i]=x[i]^x[fa[i]],还有熟悉a^b = 1 , b^c = 2 , 那么 a^c = 1^2 = 3,还有异或能够互相转化:a^b=c -> a^b^b=c^b -> a = b^c

3.为什么会用到并查集呢,由于对于同一个集合里的话我们能够通过他们与根的偏移量和根的值来知道两个数的异或结果,这样更方便计算,同一时候计算:Q k x1 .. xk 的时候。就能够转化为:(val[x1]^val[x2]..val[xk])^(x[fa[x1]]^x[fa[x2]]..x[fa[xk]]),然后利用异或偶数次不变的原理推断必须是奇数次才有能够得到结果,推断是不是xn根就是了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 20010; int n, m;
int val[MAXN], fa[MAXN]; int find(int x) {
if (x != fa[x]) {
int tmp = fa[x];
fa[x] = find(fa[x]);
val[x] ^= val[tmp];
}
return fa[x];
} int Union(int x, int y, int v) {
int fx = find(x);
int fy = find(y);
if (fx == fy)
return (val[x]^val[y]) == v;
if (fx == n)
swap(fx, fy);
fa[fx] = fy;
val[fx] = val[x]^v^val[y];
return 1;
} int main() {
char str[MAXN];
int p, q, v, k, x;
int cas = 1;
while (scanf("%d%d", &n, &m) != EOF && n+m) {
for (int i = 0; i <= n; i++) {
val[i] = 0;
fa[i] = i;
}
printf("Case %d:\n", cas++);
int facts = 0;
int err = 0;
while (m--) {
scanf("%s", str);
if (str[0] == 'I') {
gets(str);
facts++;
if (err)
continue;
int cnt = sscanf(str, "%d%d%d", &p, &q, &v);
if (cnt == 2) {
v = q;
q = n;
}
if (!Union(p, q, v)) {
err = true;
printf("The first %d facts are conflicting.\n", facts++);
}
} else {
scanf("%d", &k);
int ans = 0;
int is = 1;
map<int, int> mp;
for (int i = 0; i < k; i++) {
scanf("%d", &x);
if (err)
continue;
int f = find(x);
ans ^= val[x];
mp[f]++;
}
if (err)
continue;
map<int, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
if (it->second % 2) {
if (it->first != n) {
is = 0;
break;
}
else ans ^= val[it->first];
}
}
if (is)
printf("%d\n", ans);
else printf("I don't know.\n");
}
}
printf("\n");
}
return 0;
}

UVA - 12232 Exclusive-OR (并查集扩展偏离向量)的更多相关文章

  1. UVA 12232 Exclusive-OR(并查集+思想)

    题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...

  2. POJ 1182 (经典食物链 /并查集扩展)

    (參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...

  3. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  4. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  5. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  6. UVA 10158 War(并查集)

    //思路详见课本 P 214 页 思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素. #include< ...

  7. UVA - 11987 Almost Union-Find 并查集的删除

    Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...

  8. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

随机推荐

  1. 3TB硬盘的容量已经超出了传统分区标准的支持

    为什么3TB会有接近750G空间不能用? MBR分区格式是瓶颈 其实3TB硬盘之所以会出现各种问题,关键就在于它的容量已经超出了传统分区标准的支持.传统的硬盘采用MBR分区格式,使用LBA寻址,这种寻 ...

  2. 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)

    访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...

  3. crm2011js操作IFRAME和选项集

  4. Linux Socket编程注意事项

    Socket API 是网络应用程序开发中实际应用的标准 API.虽然该 API 简单.可是开发新手可能会经历一些常见的问题.本文识别一些最常见的隐患并向您显示怎样避免它们. 隐患 1.忽略返回状态 ...

  5. Virtualbox mouse move in and out and file share with windows

    How to use Virstalbox to share files with Linux and Windows, and to move the mouse in and out Virtua ...

  6. HDU 3277Marriage Match III(二分+并查集+拆点+网络流之最大流)

    题目地址:HDU 3277 这题跟这题的上一版建图方法差点儿相同,仅仅只是须要拆点.这个点拆的也非常巧妙,既限制了流量,还仅仅限制了一部分,曾经一直以为拆点会所有限制,原来也能够用来分开限制,学习了. ...

  7. poj - 1170 - Shopping Offers(减少国家dp)

    意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...

  8. cpe移植framework后,。解决问题的现有数据库

    最近,该公司的业务需求,原始订单apk的形式CPE.渗透framework层.这被剥离cpe,从事相当长的一段,终于有时间来写博客,记下遇到的问题,未来. 第一个问题是,原来的apk有些事情,移植fr ...

  9. qt安装遇到的错误

    /usr/bin/ld: cannot find -lXrender collect2: ld returned 1 exit status make[1]: *** [../../../../lib ...

  10. Android圆弧形ListView的实现

    本文带大家来实现ListView的圆弧形的分布排列,原理非常easy,就是依据ListView的每个Item的高度来对每个item进行偏移. 首先自己定义一个LinearLayout,这是ListVi ...