UVA Alive 4487 Exclusive-or (Weighted and checked + understanding of XOR operation)

Source: Internet
Author: User

You are notGivenNnon-negative integers x0, x1,..., xn-1 Less than2 , but they does exist, and their values never change.

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

There is kinds of facts, plus one kind of question:


Format Meaning
i  p v i Tell You p  =  v
i  p q v x p  xor x q  = v
q  k p 1  p 2 ... p k Please tell me the value of xp1 xor xp2 xor... XOR X PK
Input

There'll is at the most test cases. Each case begins with a integersNandQ (1n, 2Q.). Each of the following lines contains either a fact or a question, formatted as stated above. Thekparameter in the questions would be a positive an integer not greater than, and thevparameter in the facts would be a non-negative an integer less than2 . The last case was followed by N = Q = 0, which should not being processed.

Output

For each test case, print the case number on its own line, then the answers, and one on each one. If you can ' t deduce the answer for a particular question, from the facts I provide youbeforeThat question, print 'I don ' t know.", without quotes. If theI-th fact (don ' t count questions)cannotBe consistent with AllThe facts before that, print 'The first i facts is conflicting.", then keep silence for everything after (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 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 is conflicting.

test instructions: Two operations, I p q v means p^q = V, if there is a conflict, then output" The first I facts is conflicting. " Where I is the number of previous I operations (counting the current conflict this time). Q k p1p2. PK indicates the value of P1^P2...^PK, the output value, or "I don t know."

idea: The subject can be described with the pit father, first we need to determine how a connected block determines the difference or value between 2 points. Then each node holds the XOR value of its parent node, and when you set the value of each node to different or odd times, then you will be different or a different value if you have a parent node that is different. If this parent is a virtual node n, that's okay because the D value of n is 0, xor or it equals itself. But if it's not n then it can't be judged. Because this multi-XOR or parent node value does not know what its D value is.

so we use D[i] to denote the XOR value of I and its parent node, and to update D with the use of a check set when entering P Q. What if I just enter p? Create a virtual node n, if the input is a value of a node, let its parent node point to N, so D[p] is P and N xor (n is initially 0). Enter the time to determine if there is a conflict, if not, then merge, n is always the parent node (if any).

Then the question section. If the inquiry occurs in a set, contains K points, if K is an even number, then the K d[] is the answer; if k is odd, the value of a root will be more or less. Reason: After find (i), d[i] represents the I and the set of the root node of the XOR value, then an even number of times or the same value is equal to nothing happened; odd times it will be different or a root node value, and we must not know the root node value, unless the root nodes are N, Because all known values have nodes that are children of N.

AC Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=20000+10;        Char str[2],s[20];int d[maxn],f[maxn],vis[maxn];int n,m;int Find (int x) {if (x!=f[x]) {int t=f[x];        F[x]=find (F[x]);        D[X]^=D[T];    return f[x]; } else return x;}    BOOL Add (int p,int Q,int v) {int x=find (p);    int Y=find (q);    if (x==y) return v== (D[p]^d[q]);    if (x==n) swap (x, y);    F[x]=y;    D[x]=d[p]^d[q]^v; return true;}        int A[20];int Main () {#ifndef Online_judge freopen ("In.cpp", "R", stdin);    Freopen ("Out.cpp", "w", stdout);    #endif//Online_judge int cas=1;        while (scanf ("%d%d", &n,&m)!=eof&&n+m) {int flag=0;        int wa=0;        int p,q,v;        for (int i=0;i<=n;i++) f[i]=i;        memset (vis,false,sizeof (VIS));        Memset (d,0,sizeof (d));        printf ("Case%d:\n", cas++);            while (m--) {scanf ("%s", str);       if (flag) {         Gets (s);            Continue                } if (str[0]== ' I ') {wa++;                Gets (s);                    if (sscanf (S, "%d%d%d", &p,&q,&v) ==2) {v=q;                Q=n;                    } if (!add (p,q,v)) {printf ("The first%d facts is conflicting.\n", WA);                flag=1;                }} else {int k;                scanf ("%d", &k);                v=0;                Q=1;                    for (int i=0;i<k;i++) {scanf ("%d", &a[i]);                    P=find (A[i]);                    V^=d[a[i]];                    A[i]=p;                VIS[P]=!VIS[P];                        } for (int i=0;i<k;i++) {if (Vis[a[i]]) {                            if (a[i]!=n)q=0;                    vis[a[i]]=0;                }} if (q) printf ("%d\n", V);            else printf ("I don ' t know.\n");    }} printf ("\ n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA Alive 4487 Exclusive-or (Weighted and checked + understanding of XOR operation)

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.