- Time: 2016-03-18-09:55:52
- Title Number: [2016-03-18][poj][1733][parity game]
- Question: Given a number of intervals within the range of the number of the and, ask from which sentence start is wrong
- Analysis:
- Take the right and check the set
- Interval lengths up to 1000000000 it is not possible to create an array directly, but only 5,000 times is found, that is, at most 5000*2 points, discretization can solve the problem.
- Relation[i] I is the left end of the interval, fa[i] is the right endpoint of the interval, relation[i] maintenance (i,fa[i]) The parity state of the interval, 0 for even, 1 for odd
- F (A, B) f (b,c) indicates the state of the interval, then f (a,c) = (f (A, B) + f (b,c))%2;
#include <map>
#include <cstdio>
using namespace std;
typedef long long LL;
#define for ( x y z ) for ( int ( x ) = ( y );( x z x
const int maxn = 5000*2 + 100;
int fa[maxn],relation[maxn];
map<LL ,int > m;
void ini(){
m.clear();
FOR(i,0,maxn) fa[i] = i;
}
int fnd(int x){
if(x == fa[x]) return x;
int tmp = fa[x];
FA [ = FND ( fa [ x
relation[x] = (relation[x] + relation[tmp])&1;
return fa[x];
}
int uni(int x,int y,int type){
int fax = fnd(x),fay = fnd(y);
if(fax == fay) return 0;
fa[fax] = fay;
relation[fax] = (relation[x] + relation[y] + type)&1;
return 1;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int len,n,cur = 0,ans = 0,flg = 1;
LL u,v;
char str[10];
scanf("%d%d",&len,&n);
ini();
FOR(i,0,n){
if(flg){
scanf("%I64d%I64d%s",&u,&v,str);
--u;
if(!m.count(u)) u = m[u] = cur++;
else u = m[u];
if(!m.count(v)) v = m[v] = cur++;
else v = m[v];
int type = (str[0] == ‘o‘?1:0);
if(!uni(u,v,type)){
int t = (relation[ u ] + relation[ v ])&1;
if(t != type){
flg = 0;
continue;
}
}
++ans;
}else scanf("%*I64d%*I64d%*s");
}
printf("%d\n",ans);
return 0;
}
From for notes (Wiz)
[2016-03-18] [POJ] [1733] [Parity Game]