Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: give n numbers and some conditions, X [I] = V or X [I] ^ X [J] = K, and then give some inquiries, X [a1] ^ X [a2]……
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3234
Classic query set
Similar to many questions, record the relationship between a node and the root node, that is, the difference or value with the root node.
If pre [a] = Ra, pre [B] = RB; then what is the situation after merging, that is, R [a] = num [a] ^ num [Ra]; R [B] = num [B] ^ num [RB]; Because R [a] ^ r [B] = C
Now the requirement is num [Ra] = num [Ra] ^ num [RB] = R [a] ^ r [B] ^ C;
There is no problem with merging, but there is another problem: there are two conditions given. What should we do if it is just a single node?
Add a redundant node. The label is N, the value is 0, and any number and 0 are different or are themselves.
The rest is the query. If a certain number of queries exist in a collection.
If the root node is the redundant node that we add, it is clear that R [x] = num [x] can be used to directly or differently.
If it is not a redundant node and R [x] = R [x] ^ r [pre [x], the root node is exclusive or one time. If the number of a set is an even number, if the root number is an odd number, it cannot be judged.
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1 <30 # define M 60005 # define n 20005 # define maxn 300005 # define EPS 1e-10 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll long # define lson step <1 # define rson step <1 | 1 # define mod 1000000009 # define sqr (A) (a) * ()) using namespace STD; int pre [N], R [N]; int N, Q; int K, num [N]; char ope [1000]; vector <int> que; void Init () {for (INT I = 0; I <= N; I ++) Pre [I] = I, R [I] = 0 ;} int find (int x) {If (X! = Pre [x]) {int F = pre [X]; Pre [x] = find (pre [x]); R [x] ^ = R [f];} return pre [X];} bool Union (int A, int B, int c) {int Ra = find (A), rB = find (B ); if (RA = Rb) {If (R [a] ^ r [B])! = C) Return false; else return true;} If (RA = N) Swap (RA, Rb); // This sentence is very important and is related to the query judgment, wa a few times pre [Ra] = RB; R [Ra] = R [a] ^ r [B] ^ C; return true;} int query () {bool vis [N]; MEM (VIS, false); int ans = 0; For (INT I = 0; I <K; I ++) {If (vis [I]) continue; int CNT = 0; int root = find (Num [I]); For (Int J = I; j <K; j ++) {If (! Vis [J] & find (Num [J]) = root) {CNT ++; vis [J] = true; ans ^ = R [num [J];} If (root! = N & CNT & 1) Return-1;} return ans;} int main () {int CAS = 0; while (scanf ("% d ", & N, & Q )! = EOF) {If (n = 0 & Q = 0) break; printf ("case % d: \ n", ++ CAS); Init (); bool error = false; int CAS = 0; while (Q --) {scanf ("% s", OPE); If (OPE [0] = 'I ') {getchar (); gets (OPE); CAS ++; int Space = 0, U, V, W; For (INT I = 0; I <strlen (OPE ); I ++) Space = space + (OPE [I] = ''); If (Space = 1) {sscanf (OPE," % d ", & U, & W); V = N;} else sscanf (OPE, "% d", & U, & V, & W); If (error) continue; If (! Union (U, V, W) {printf ("the first % d facts are conflicting. \ n ", CAS); error = true ;}} else {scanf (" % d ", & K); For (INT I = 0; I <K; I ++) scanf ("% d", & num [I]); If (error) continue; int ans = query (); If (ANS =-1) puts ("I don't know. "); else printf (" % d \ n ", ANS) ;}} puts (" ");} return 0 ;}