//poj Parity game(带权并查集)
//换化数据
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<map>
using namespace std;
const int N=20000;
int father[N];
int rank[N];
int m,n;
int dk;
map<int ,int> mp;
void init()
{
dk=1;
for(int i=0;i<N;i++)
{
father[i]=i;
rank[i]=0;
}
}
int find(int x)
{
if(x!=father[x])
{
int temp=father[x];
father[x]=find(temp);
rank[x]=(rank[x]+rank[temp])%2;
}
return father[x];
}
bool union_find(int x,int y,int r)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
father[fy]=fx;
rank[fy]=(r+rank[x]-rank[y]+2)%2;
return true;
}
else
{
if((rank[x]+rank[y])%2==r)
{
return true;
}
else
{
return false;
}
}
}
int main()
{
scanf("%d",&m);
scanf("%d",&n);
init();
int i;
int a,b,x,y;
string str;
bool flag=true;
int nd=n;
for( i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
cin>>str;
b++;
if(flag==false) continue;
if(mp.find(a)==mp.end())
{
mp[a]=dk++;
}
if(mp.find(b)==mp.end())
{
mp[b]=dk++;
}
if(str=="even")
{
flag=union_find(mp[a],mp[b],0);
}
else
{
flag=union_find(mp[a],mp[b],1);
}
if(flag==false)
{
nd=i-1;
}
}
printf("%d\n",nd);
return 0;
}
poj Parity game(带权并查集)
最新推荐文章于 2019-10-02 20:27:14 发布