POJ1733 Parity game (美丽的jo)

Description

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.

You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.


题目大意:(codevs上的奇偶游戏,vijos上的小胖的奇偶。)给定一个长度为L的01串,告诉你一定区间(闭区间)内1的个数是even还是odd。然后判断到那句话时出了错。

思路:用并查集,保存结点到根的奇偶情况(用数字相加,最后%2就好了),注意路径压缩的时候要处理。。。
 int rool(int x)
{
int j;
if (fa[x]!=x)
{
j=rool(fa[x]);
tot[x]=tot[x]+tot[fa[x]];
fa[x]=j;
}
return fa[x];
} (A little different)
每次对一条边的两点进行合并的时候要注意公式判断。
r1=rool(x);
    r2=rool(y);
    if (r1!=r2)
    {
    fa[r1]=r2;
    tot[r1]=tot[y]+kind[i]-tot[x];
    }
因为L很长,所以要离散化
#include<algorithm>
 sort(b+1,b+m*2+1);
    size=unique(b+1,b+m*2+1)-b-1;
    for (i=1;i<=2*m;++i)
    a[i]=upper_bound(b+1,b+size+1,a[i])-b-1;

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fa[10001]={0},a[10001]={0},b[10001]={0},tot[10001]={0},kind[10001]={0};
int rool(int x)
{
int j;
if (fa[x]!=x)
{
j=rool(fa[x]);
tot[x]=tot[x]+tot[fa[x]];
fa[x]=j;
}
return fa[x];
}
int main()
{
int size,i,j,n,m,r1,r2,x,y;
bool f=false;
char ch[10];
scanf("%d%d",&n,&m);
for (i=1;i<=m;++i)
{
 scanf("%d%d%s",&a[i*2-1],&a[i*2],&ch);
 if (ch[0]=='e') kind[i]=0;
 else kind[i]=1;
 ++a[i*2];
 b[i*2-1]=a[i*2-1];b[i*2]=a[i*2];
    }
    for (i=1;i<=m*2;++i)
      fa[i]=i;
sort(b+1,b+m*2+1);
    size=unique(b+1,b+m*2+1)-b-1;
    for (i=1;i<=2*m;++i)
    a[i]=upper_bound(b+1,b+size+1,a[i])-b-1;
    for (i=1;i<=m;++i)
    {
    x=a[i*2-1];
    y=a[i*2];
    r1=rool(x);
    r2=rool(y);
    if (r1!=r2)
    {
    fa[r1]=r2;
    tot[r1]=tot[y]+kind[i]-tot[x];
    }
    else
    {
    if (abs(tot[x]-tot[y])%2!=kind[i])
    {
    f=true;
    cout<<i-1<<endl;
    break;
    }
    }
    }
    if (!f) cout<<m<<endl;

 

你可能感兴趣的:(game)