HDU 3234 exclusive-or

Source: Internet
Author: User
Tags bool cas printf
exclusive-orAnd check the set of good questions
Because each time a given is the relationship of the XI, XJ, and can be given the value of Xi, we need 3 arrays to store the information for each value: V[i] Represents the value of I, P[i] represents the Father node of I, D[i] represents the value of V[i] ^ v[p[i]].
The first is the query operation to check the set. Here we not only need to compress the path, update the relationship between the X and the root node, which can be directly computed by or the transfer of the operation, and if we know the X or the value of the root of the known, we will be the corresponding sub-node values are also obtained. The value of a node or the root of a tree can be obtained directly or simply by manipulating it. Whenever we determine the value of a node, we need to determine the tree root of the node, so that the value of x can be obtained directly from the query operation on node x. This is very useful when merging or assigning values.
For each i x y C, which represents v[x]^v[y] = V, obviously if for a known situation, if two points on the same tree, that is d[x] ^ d[y] = = c If set up, then meet the conditions, otherwise not satisfied; If the two points are not on the same tree, if the value of the root node can be obtained, then only D[X] ^ d[y] ^ v[p[x] ^ V[p[y]] = = C satisfies then does not conflict, otherwise conflicts. If you do not know the value, then this time the added relationship must be established, we only need to merge two trees, and then calculate the relationship between the roots: D[x] ^ d[y] ^ C. For each L x C, which means v[x] = c, at this point, we just need to find the value of x, if x tree root has a value , then it is possible to calculate the value of x directly (through the query operation of a single check set) and then compare it.
For calculated values, because the data volume is only 15 smaller, violence can be. Using or manipulating the characteristics, if a number appears even several times, it is equivalent to not appear, only need to maintain a root and the number of nodes in the tree should use the array. For each point, a query operation is performed first, and then if the point has a value, then the answer is directly or the value. Otherwise, place this point in the array and update the number of occurrences of the node. The last array is scanned, if there is an odd number of occurrences of a tree root node, the answer cannot be determined, otherwise the answer can be determined.

#include <cstdio> const int MAXN = 20000+5;
int P[MAXN];
int V[MAXN];
int D[MAXN];
int count[20][2];
int cnt;
        void init (int n) {for (int i = 0; i < n; i + +) {P[i] = i;
        V[i] =-1;
    D[i] = 0;
    }} int find (int x) {if (x = = P[x]) return x;
    int r = Find (P[x]);
    D[X] = d[p[x]] ^ d[x];

    P[x] = r;
    if (v[x]! =-1) {V[r] = v[x] ^ d[x];
    } if (V[r]! =-1) {v[x] = V[r] ^ d[x];
} return R;
    } bool Joint (int x, int y, int c) {int px = find (x);

    int py = find (y);
        if (px = = py) {if ((D[x] ^ d[y])! = c) {return false;  }} if (v[px]! =-1 && v[py]! =-1) {if ((v[px] ^ v[py] ^ d[x] ^ D[y] ^ c)! = 0) {return
        False
        }} else{p[px] = py;
        D[PX] = d[x] ^ d[y] ^ C;
        if (v[px]! =-1) {v[py] = v[px] ^ d[px];
        } else if (v[py]! =-1) {v[px] = V[py] ^ d[px]; }
    }
    return true;
    }//If the value of x is set to c bool Set (int x, int c) {find (x);
    if (v[x]! =-1) {if (v[x]! = c) return false;
        } else{V[x] = c;
    V[P[X]] = d[x] ^ C;
} return true;
    } void Insert (int x) {int i;
            for (i = 0; i < cnt; i + +) {if (count[i][0] = = P[x]) {count[i][1] + +;
        Break
        }} if (i==cnt) {count[cnt][0] = p[x];
        COUNT[CNT][1] = 1;
    CNT + +;
        }} bool Check () {for (int i = 0; i < cnt; i + +) {if (count[i][1]&1) {return false;
}} return true;
    } int main () {int n, Q, x, Y, c, Ins, CAs, ans;
    BOOL silence, Flag;
    Char cmd[2];
    cas = 0;
    Freopen ("Data.in", "R", stdin); while (scanf ("%d%d", &n, &q), n| |
        Q) {printf ("Case%d:\n", + + CAs);
        Init (n);
        Silence = false;
        Flag = true;

        INS = 0;
            while (q--) {scanf ("%s", cmd); If(cmd[0]== ' I ')
                {ins + +;
                scanf ("%d%d", &x, &y);
                Cmd[0] = GetChar ();
                    if (cmd[0]== ') {scanf ("%d", &c);
                    if (silence) continue;
                        if (!joint (x, Y, c)) {silence = true;
                    printf ("The first%d facts is conflicting.\n", ins);
                    }} else{if (silence) continue;
                        if (!set (x, y)) {silence = true;
                    printf ("The first%d facts is conflicting.\n", ins);
                }}} else{scanf ("%d", &c);
                Ans = 0;
                CNT = 0;
                Flag = false;
                    while (c--) {scanf ("%d", &x);
         Find (x);           if (v[x]! =-1) {ans = ans ^ v[x];
                        } else{ans = ans ^ d[x];
                    Insert (x);
                }} if (silence) {continue;

                } flag = Check ();
                if (flag) {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.