Poj1733 parity game (beautiful Jo)

Source: Internet
Author: User

Description

Now and then you play the following game with your friend. your friend writes down a sequence consisting of zeroes and ones. you choose a continuous subsequence (for example the subsequence from the third to the nth digit sively) and ask him, whether this subsequence contains even or odd number of ones. your friend answers your question and you can ask him about another subsequence and so on. your task is to guess the entire sequence of numbers.

You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. thus you have decided to write a program to help you in this matter. the program will receive a series of your questions together with the answers you have stored ed from your friend. the aim of this program is to find the first answer which is provably wrong, I. e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

(Parity games on codevs and chubby parity on vijos .) Given A 01 string with a length of L, it tells you whether the number of 1 in a certain range (closed range) is even or odd. Then an error occurred when judging the sentence.

Idea: Use and check the set and save the parity between the node and the root (add the number and % 2 at last). Pay attention to the path compression process...
Int Rool (int x) {Int J; If (Fa [x]! = X) {J = Rool (Fa [x]); Tot [x] = tot [x] + tot [Fa [x]; Fa [x] = J ;} return Fa [X];} (a little different)
When merging two points of an edge, pay attention to formula judgment.
R1 = Rool (x); r2 = Rool (y); If (R1! = R2) {Fa [R1] = R2; Tot [R1] = tot [y] + kind [I]-tot [X];}
L is very long, so discretization is required.
# Include <algorithm>
Sort (B + 1, B + M * 2 + 1); size = unique (B + 1, B + M * 2 + 1)-B-1; for (I = 1; I <= 2 * m; ++ I) A [I] = upper_bound (B + 1, B + size + 1, a [I])-B-1;

Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int Fa [10001] = {0 }, A [10001] = {0}, B [10001] = {0}, TOT [10001] = {0}, kind [10001] = {0 }; int Rool (int x) {Int J; If (Fa [x]! = X) {J = Rool (Fa [x]); Tot [x] = tot [x] + tot [Fa [x]; Fa [x] = J ;} return Fa [X];} int main () {int size, I, j, n, m, R1, R2, X, Y; bool F = false; char ch [10]; scanf ("% d", & N, & M); for (I = 1; I <= m; ++ I) {scanf ("% d % s", & A [I * 2-1], & A [I * 2], & Ch ); if (CH [0] = 'E') kind [I] = 0; else kind [I] = 1; ++ A [I * 2]; B [I * 2-1] = A [I * 2-1]; B [I * 2] = A [I * 2];} for (I = 1; I <= m * 2; ++ I) Fa [I] = I; sort (B + 1, B + M * 2 + 1 ); size = unique (B + 1, B + M * 2 + 1)-B-1; for (I = 1; I <= 2 * m; ++ I) A [I] = u Pper_bound (B + 1, B + size + 1, a [I])-B-1; for (I = 1; I <= m; ++ I) {x = A [I * 2-1]; y = a [I * 2]; R1 = Rool (x); r2 = Rool (y); If (R1! = R2) {Fa [R1] = R2; Tot [R1] = tot [y] + kind [I]-tot [X];} else {If (ABS (TOT [x]-tot [y]) % 2! = Kind [I]) {f = true; cout <I-1 <Endl; break ;}} if (! F) cout <m <Endl ;}

 

Poj1733 parity game (beautiful Jo)

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.