Ultraviolet-12232 exclusive-or (and query the set extension deviation vector)

Source: Internet
Author: User

Description

You are not givenNNon-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 youXP =V
I P q v I tell youXPXOR xQ =V
Q K p1P2...PK Please tell me the valueXP1XOR XP2XOR...XOR xPK

Input

There will be at most 10 test cases. Each case begins with two integersNAndQ(1N20,000, 2Q40,000). Each of the following lines contains either a fact or a question, formatted as stated above.KParameter in the questions will be a positive integer not greater than 15, andVParameter in the facts will be a non-negative integer less than220. the last case is followedN=Q= 0, which shocould 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 particle question, from the facts I provide youbefore that question, print''I don't know.", Without quotes. IfI-Th fact (don't count questions) cannot be consistent withall the facts before that, print''The firstIFacts 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 3Q 1 0 Q 2 1 0I 0 2 Q 1 1 Q 1 0 3 3 I 0 1 6I 0 2 2Q 2 1 22 4 I 0 1 7Q 2 0 1I 0 1 8Q 2 0 10 0

Sample output

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

Question:

There are n (n <= 20000) unknown integers x0, X1, x2. .. Xn-1, there are the following Q (q <= 40000) operations:
I P V: Tell you XP = V
I p q V: Tell you XP XOR XQ = V
Q k P1 P2... PK: Ask xp1 XOR xp2 .. XOR xpk, K is not greater than 15.
If the current I conflicts with the previous one
Train of Thought: Check questions and deeply feel the powerlessness of doing well and querying the set. I don't know how to do it if I know it's a collection but I don't know how to do it. Let's talk about the idea:

1. For each query, we do not need to know the size of each number or roll out the results. For this: I P V

We can Virtualize a number xn = 0, so we can have a general formula P ^ q = V, because P ^ 0 = P. The imaginary root XN cannot be changed, and its descendant has a definite value.

2. assume that the offset Val [I] = x [I] ^ X [Fa [I], a ^ B = 1, B ^ c = 2, then a ^ c = 1 ^ 2 = 3, there are differences or mutual conversion: A ^ B = C-> A ^ B ^ B = C ^ B-> A = B ^ C

3. why do we use and query the set? For the same set, we can know the difference or result of the two numbers through their offset from the root and the root value, this is more convenient for computing and computing at the same time: Q k X1 .. XK can be converted to: (Val [X1] ^ Val [X2] .. val [XK]) ^ (X [Fa [X1] ^ X [Fa [X2] .. X [Fa [XK]). Then, the principle of the same or even number of times is used to determine whether the result can be obtained only when it is an odd number of times.

#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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.