#include #include #include #include #include using namespace std; #define MAXN 10010 #define MP(x,y) make_pair(x,y) #define FI first #define SE second map,int>ma; const int dis[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; struct _no { int x,y,c; int to[4]; _no(int _x,int _y) { x=_x,y=_y; c=-1; for(int i=0;i<4;++i) to[i]=-1; } _no(){} }no[MAXN]; int top,fa[MAXN],col[MAXN],xy[MAXN]; int que[MAXN]; bool vi[MAXN]; int findfa(int n) { if(fa[n]!=n) fa[n]=findfa(fa[n]); return fa[n]; } inline void smu(int a,int b) { a=findfa(a),b=findfa(b); if(a==b) return ; fa[b]=a; col[a]+=col[b]; } void del(int h) { int he=0,ta=0; que[ta++]=h; while(he,int> ::iterator ite=ma.find(MP(tx,ty)); if(ite!=ma.end()) no[i].to[j]=ite->SE; } for(int i=0;i0&&ty>0&&(no[p].to[j]==-1||no[no[p].to[j]].c==-1)) ++col[p]; } for(int j=0;j<4;++j) { int tmp=no[p].to[j]; if(tmp!=-1&&no[tmp].c!=-1) { --col[findfa(tmp)]; if(no[tmp].c==c) smu(tmp,p); } } for(int j=0;j<4;++j) { int tmp=no[p].to[j]; if(tmp!=-1&&no[tmp].c==!c&&col[findfa(tmp)]==0) del(tmp); } for(int j=0;j<4;++j) { int tmp=no[p].to[j]; if(tmp!=-1&&no[tmp].c==c&&col[findfa(tmp)]==0) del(tmp); } if(no[p].c!=-1&&col[findfa(p)]==0) del(p); } int nb=0,nw=0; for(int i=0;i