您的位置:首页 > 其它

poj-1733 Parity game 并查集

2014-07-27 14:28 357 查看
题目链接

题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数。一共会告诉你几组这样的数

要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输出这个k,要是这个人回答的都是正确的,则输出组数

odd为奇数,even为偶数。

w表示该点到他祖宗点的区间1的奇偶个数

数据范围比较大 需要离散化

#include "stdio.h"
#include "map"
#include "queue"
#include "iostream"
#include "functional"
#include "math.h"
#include "algorithm"
using namespace std;
const int maxn = 100005;
const int mod = 1000000007 ;
const int inf = 1<<30;
typedef __int64 LL;
typedef pair<double,int> pii;
int n,m,cnt,flag;
int num[maxn];
map<int,int>p,w;
struct node
{
int x,y,k;
}que[maxn];
int find( int x )
{
if( p[x] == x ) return x;
int t = p[x];
p[x] = find( p[x] );
w[x] = (w[x] + w[t])%2;
return p[x];
}
bool merge( int a,int b,int k )
{
int x = find( a );
int y = find( b );
if( x == y && w[a] != (w[b] + k)%2 )
return true;
else if( x < y )
{
p[x] = y;
w[x] = ((( w[b] + k )%2)-w[a] + 2)%2;
}
else
{
p[y] = x;
w[y] = (w[a] - (w[b]+k)%2 + 2)%2;
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
char str[10];
while( scanf("%d",&n) != EOF )
{
cnt = flag = 0;
p.clear();w.clear();
scanf("%d",&m);
for( int i = 1; i <= m; i ++ )
{
scanf("%d%d%s",&que[i].x,&que[i].y,str);
que[i].x --;
que[i].k = str[0] == 'o'?1:0;
num[cnt++] = que[i].x;
num[cnt++] = que[i].y;
}
sort( num,num+cnt );
cnt = unique( num,num+cnt ) - num;
for( int i = 0; i < cnt; i ++ )
{
p[num[i]] = num[i];
w[num[i]] = 0;
}
for( int i = 1; i <= m; i ++ )
{
if( !flag && merge( que[i].x,que[i].y,que[i].k ) )
flag = i-1;
}
if( flag == 0 )	flag = m;
printf("%d\n",flag);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: