POJ 1733 Parity game

来源:互联网 发布:淘宝号注册马上注册 编辑:程序博客网 时间:2024/05/17 22:25

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.
Input
The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either even' orodd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even' means an even number of ones andodd’ means an odd number).
Output
There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3


这是一道鲜血淋漓的题,大家记着,一定要在外面写f[x]=fnd(x)(半位长者的忠告)。
题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数。一共会告诉你几组这样的数
要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输出这个k,要是这个人回答的都是正确的,则输出组数
odd为奇数,even为偶数。
分析:
思路:赤裸裸的种类并查集吧…….其中,我们把一段区间为奇数标记为0,为偶数标记为1,然后如果区间连贯,也就是说区间1到区间2,区间3到区间4,那么就是可以连贯成区间1到区间4的,如此的话,可以是左极限-1,或者右极限+1……判断是否在同一个树上,在的话,判断是否正确,不在的话,连接起来,在连接的时候,按照种类并查集的操作即可…….


#include<algorithm>#include<iostream>#include<cstdio>using namespace std;const int maxq=50005;int n,q,tp,num,f[maxq],w[maxq],d[maxq];struct qa{    int xx,yy,z;}a[maxq];int fnd(int x){    if(f[x]!=x)    {        int ff=fnd(f[x]);        w[x]=(w[f[x]]+w[x])%2;        f[x]=ff;    }    return f[x];}int solve(){    for(int i=1;i<=q;i++)    {        int t1=lower_bound(d+1,d+num+1,a[i].xx)-d,t2=lower_bound(d+1,d+num+1,a[i].yy)-d;        int fx=fnd(t1),fy=fnd(t2);        if(fx==fy)        {            if((w[t1]-w[t2]+2)%2!=a[i].z)                return i-1;        }        else        {            w[fx]=(w[t2]-w[t1]+a[i].z+2)%2;            f[fx]=fy;        }    }    return q;}int main(){    scanf("%d%d",&n,&q);    char ch[11];    for(int i=1;i<=q;i++)    {        scanf("%d%d%s",&a[i].xx,&a[i].yy,ch);        a[i].xx--;        d[++tp]=a[i].xx;        d[++tp]=a[i].yy;        if(ch[0]=='o')            a[i].z=1;        else            a[i].z=0;    }    sort(d+1,d+tp+1);    num=unique(d+1,d+1+tp)-d+1;    for(int i=1;i<=num;++i)        f[i]=i;    printf("%d\n",solve());    return 0;}
0 0
原创粉丝点击