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 ;}