标签:
#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[x] = 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;
}
[2016-03-18][POJ][1733][Parity game]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/1beb05cb78ccdde9544c55cf70360550.html