Poj1733 & Ural 1003 parity game (hash + query set)

Source: Internet
Author: User

A string consisting of 0 and 1 with the length of N. Then, ask whether there is an odd number of 1 or an even number of 1 between the position L and R.

Based on these answers, we can determine the number one is wrong (in conflict with the previous one.

Once a conflict is found, it is necessary to jump out of the loop immediately, and the subsequent data does not need to be taken care of. Otherwise, if we use continue, we will use wa.

# Include <iostream> # include <map> # include <cstring> using namespace STD; const int Maxx = 10005; int N, P, pre [Maxx], parity [Maxx]; map <int, int> numindex; // discretization int find (int x) {If (pre [x] =-1) return X; int F = find (pre [x]); Parity [x] = (parity [x] + parity [pre [x]) % 2; Pre [x] = F; return F;} bool query (int x, int y, int ODD) {int R1, R2; R1 = find (x); r2 = find (y ); if (r1 = R2) {If (parity [x] + parity [y]) % 2 = ODD) return 1; else return 0 ;} else {pre [R1] = R2; Parity [R1] = (parity [x] + parity [y] + ODD) % 2; return 1 ;}} void solve () {int I, X, Y, index, idx1, idx2, odd; char s [10]; CIN >>> P; Index = 0; for (I = 0; I <p; I ++) {CIN> x> Y> S; X --; If (numindex. find (x) = numindex. end () numindex [x] = index ++; idx1 = numindex [X]; If (numindex. find (y) = numindex. end () numindex [y] = index ++; idx2 = numindex [y]; If (strcmp (S, "odd") = 0) odd = 1; else odd = 0; If (query (idx1, idx2, odd) = false) break;} cout <I <Endl;} void Init () {memset (PRE, -1, sizeof (pre);} int main () {Init (); solve (); // system ("pause"); Return 0 ;}

The AC code on Ural:

# Include <iostream> # include <map> # include <cstring> using namespace STD; const int Maxx = 10005; int N, P, pre [Maxx], parity [Maxx]; map <int, int> numindex; // discretization int find (int x) {If (pre [x] =-1) return X; int F = find (pre [x]); Parity [x] = (parity [x] + parity [pre [x]) % 2; Pre [x] = F; return F;} bool query (int x, int y, int ODD) {int R1, R2; R1 = find (x); r2 = find (y ); if (r1 = R2) {If (parity [x] + parity [y]) % 2 = ODD) return 1; els E return 0;} else {pre [R1] = R2; Parity [R1] = (parity [x] + parity [y] + ODD) % 2; return 1 ;}} int main () {While (CIN> N & n! =-1) {memset (PRE,-1, sizeof (pre); memset (parity, 0, sizeof (parity); numindex. clear (); int I, X, Y, index, idx1, idx2, odd; char s [10]; CIN> P; Index = 0; for (I = 0; I <p; I ++) {CIN> x> Y> S; X --; If (numindex. find (x) = numindex. end () numindex [x] = index ++; idx1 = numindex [X]; If (numindex. find (y) = numindex. end () numindex [y] = index ++; idx2 = numindex [y]; If (strcmp (S, "odd") = 0) odd = 1; else odd = 0; If (query (idx1, idx2, odd) = false) break;} For (int ii = I + 1; II <p; II ++) {CIN> x> Y> S;} cout <I <Endl;} // system ("pause"); 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.