HDU 3234 Exclusive-OR and check set deformation

Source: Internet
Author: User
/* From: http://www.cppblog.com/Yuan/archive/2010/09/02/125667.html? Opt = admin slightly rewrite there are n (n <= 20000) unknown integer X0, X1, X2Xn-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, jump out and check the set extension! For I p v, if a point Xn = 0 is set, it can be regarded as I p n v (different from 0 or). So for all those I, it is a ^ B = v, for the effect of the two, val [I] = Xi ^ Xfa [I] is the same as the above. fa [I] is the father of I: 1) I p q v First find if p q is in the same set, and judge whether there is Xp ^ Xq = v is not a conflict. Otherwise, the merged points should always be kept as the root. 2) Q k p1pk Xp1 ^ Xp2 ^ Xpk is converted: val [p1] ^ val [p2] ^ val [k] ^ (Xfa [p1] ^ Xfa [p2] ^ Xfa [pk]) val [pi] known, you only need to determine whether Xfa [pi] is known because it is an exclusive or an odd number of Xfa [pi. Determine whether the root is Xn */# include <cstdio> # include <cstring> # include <algorithm> # include <vector> using namespace std; const int MAXN = 20010; int fa [MAXN], val [MAXN]; int n, q; void init (int n) {for (int I = 0; I <= n; I ++) {val [I] = 0; fa [I] = I ;}} int find (int a) {if (! = Fa [a]) {int t = fa [a]; fa [a] = find (fa [a]); val [a] ^ = val [t];} return fa [a];} bool unin (int a, int B, int v) {int ra = find (a); int rb = find (B ); if (ra = rb) {return v = (val [a] ^ val [B]);} if (ra = n) swap (ra, rb ); fa [ra] = rb; val [ra] = val [a] ^ val [B] ^ v; return true;} char cmd [MAXN]; int main () {// freopen ("in", "r", stdin); int t = 1; while (scanf ("% d", & n, & q ), n) {printf ("Case % d: \ n", t ++); init (n); int fact = 0; bool err = False; for (int I = 0; I <q; I ++) {scanf ("% c", & cmd [0]); if (err) {gets (cmd); continue;} if (cmd [0] = 'I') {gets (cmd); // fact ++; int x, y, v; if (sscanf (cmd, "% d", & x, & y, & v) = 2) // sscanf () directly scanf () I am not sure why {swap (y, v); y = n;} if (! Unin (x, y, v) {err = true; printf ("The first % d facts are conflicting. \ n ", fact) ;}} else {int k, x, ans = 0, pos =-1, flag = true; vector <pair <int, int> V; scanf ("% d", & k); for (int I = 0, jj; I <k; I ++) {scanf ("% d", & x ); int rx = find (x); ans ^ = val [x]; for (jj = 0; jj <V. size (); jj ++) {if (V [jj]. first = rx) break;} if (jj = V. size () V. push_back (make_pair (rx, 1); else V [jj]. second ++;} for (int j = 0; j <V. size (); j ++) {if (V [j]. secon D & 1) {if (V [j]. first! = N) {flag = false; break ;}} if (flag) printf ("% d \ n", ans); else puts ("I don't know. ") ;}} puts (" ");} 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.