Uvalive 4487 exclusive-or weighted and search for questions

Source: Internet
Author: User

X [0-(n-1)] is known, but the specific value is unknown. The information given by the question is only I p v, indicating XP = V, or I p q v, description: XP ^ XQ = V. Then, you need to answer each query, asking for any sequence value xp1 ^ xp2, and x ^ PK.

This problem is handled using weighted and query sets:

1. f [] still represents the parent node, and the path is compressed, and each V [I] = V [I] ^ V [f [I], that is, the value stored by the node is actually the same or different from its father. Why is this true? Because an exception or first satisfies the exchange law and the same number is even, that is, equivalent to itself, one of the requirements of this question is a test conflict, then, if the two vertices belong to the same set, their XP ^ XQ = XP ^ XF ^ XQ ^ XF = V [p] ^ V [Q], the principle that an exclusive or even number of times is equal to itself is used. In actual calculation, this is also the case. Only when the parent node is exclusive or even number of times can it be eliminated to obtain the true value, only one or more of the parent nodes have exceeded the exclusive or odd number of times, which means they cannot be obtained and output I don't know.

2. to cope with the direct assignment of I P V, manually add a super parent node T, V [T] = 0 to make it different from t each time, so that no special processing is needed.

3. T must be the parent node for each merge. During the find operation, the path is compressed, and the weighting is merged. The bottom node is smoothly converted to the child node of the parent node through the feature of an exclusive or even number equal to itself.

4. Pay attention to it! The priority of the operation is higher than ^. For this wa several times, if bit operations are involved in the future, multiple parentheses are written. It is not the first time that the operation has been put into a pitfall.

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int n = 21000 + 20; int f [N], V [N]; int vis [N], a [20]; int T = N-10; void Init (int n) {for (INT I = 0; I <= n + 1; I ++) {f [I] = I; V [I] = 0; vis [I] = 0;} f [T] = T; V [T] = 0;} int findset (int x) {If (X! = F [x]) {int TMP = f [X]; F [x] = findset (F [x]); V [x] ^ = V [TMP]; // because V [x] itself includes the value of the parent node of an exclusive or previous layer. At this time, the return f [X];} is offset when it is the same or once as the previous layer. bool uset (int p, int Q, int Val) {int R1 = findset (p); int r2 = findset (Q); If (r1 = R2) {If (V [p] ^ V [Q])! = Val) return 0; else return 1;} If (r1 = T) Swap (R1, R2); F [R1] = R2; V [R1] = V [p] ^ V [Q] ^ val; // note that the actual V [R1] = xr1 ^ xr2, VP and VQ include these two items, and use Val to offset themselves. The remaining two items are exclusive or indeed wonderful return 1;} int main () {int N, Q, P, Q, Val, Kase = 0; char ch [2]; char s [40]; while (scanf ("% d ", & N, & Q) {If (! (N + q) break; Init (n); printf ("case % d: \ n", ++ Kase); bool err = 0; int facts = 0; for (INT I = 1; I <= Q; I ++) {scanf ("% s", CH); If (CH [0] = 'I ') {facts ++; gets (s); If (sscanf (S, "% d", & P, & Q, & Val) = 2) {val = Q; q = T ;}// cout <p <"<q <" <Val <Endl; If (ERR) continue; if (! Uset (p, q, Val) {printf ("the first % d facts are conflicting. \ n ", facts); err = 1 ;}} else {int K, ANS = 0; scanf (" % d ", & K); For (INT I = 1; I <= K; I ++) {scanf ("% d", & A [I]); If (ERR) continue; int r = findset (A [I]); ans ^ = V [A [I]; A [I] = r; vis [R] ^ = 1 ;} if (ERR) continue; bool flag = 1; for (INT I = 1; I <= K; I ++) {If (vis [A [I]) {// check whether it is an odd number of times if (a [I]! = T) {// If the odd number of times is a super parent node, it does not matter because its value is known to be flag = 0;} vis [A [I] = 0 ;}} if (FLAG) printf ("% d \ n", ANS); else printf ("I don't know. \ n ") ;}} 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.