Test instructions: There are n nodes, a Q operation is given, the operation is a B string representing the Node A to Node B and is odd or even, output x (the first x operation is correct).
The problem: With the right and the collection of classical problems, because there may be 10,000,000,000 nodes, so the need for discretization, not the point is not considered. Because A is added to B, if A==b cannot find its own root node and determines if it wants to merge, it is actually mp[a-1] to Mp[b] to be merged into the collection. Also need to note that the node is in order, the Father node is greater than or equal to the child node, so that the judgment will not be wrong, so the merger to consider the size of the two root nodes have different relations.
#include <stdio.h>#include <map>using namespace STD;Const intN =10005;intPa[n], N, Q, CNT, rel[n], l[n], r[n];Charstr[n][Ten]; map<int, int>mpintGet_parent (intx) {if(pa[x]! = x) {intpx = get_parent (pa[x]); REL[X] = (Rel[x] + rel[pa[x])%2; PA[X] = px; }returnPA[X];}intMain () { while(scanf("%d", &n) = =1) {scanf("%d", &q); Mp.clear (); CNT =1; for(inti =1; I <= Q; i++) {scanf("%d%d%s", &l[i], &r[i], str[i]);if(!mp[l[i]-1]) Mp[l[i]-1] = cnt++;if(!mp[r[i]]) mp[r[i]] = cnt++; } for(inti =0; I <= CNT; i++) {Pa[i] = i; Rel[i] =0; }intres =-1; for(inti =1; I <= Q; i++) {intFlag = str[i][0] ==' E '?0:1;intpx = get_parent (Mp[l[i]-1]);intPY = get_parent (Mp[r[i]);if(px! = py) {if(px < PY) {PA[PX] = py; REL[PX] = (flag + rel[mp[r[i]]-rel[mp[l[i]-1]] +2) %2; }Else{Pa[py] = px; Rel[py] = (Rel[mp[l[i]-1]]-flag-rel[mp[r[i]] +4) %2; } }Else{if((Rel[mp[l[i)-1]]-rel[mp[r[i]] +2) %2! = Flag) {res = i-1; Break; } } }if(res = =-1) res = q;printf("%d\n", res); }return 0;}
POJ 1733 (with right and check set)