POJ-1733 Parity game (并查集)

Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9424   Accepted: 3648

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' or `odd' (the answer, i.e. the parity of the number of ones in the chosen subsequence, where `even' means an even number of ones and `odd' 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

Source

 
题目大意:给定一串长为m的数,只有0和1。然后n次询问及回答,在第x至第y位中1的个数是奇数或是偶数。问从哪一次开始,下一次的回答就与之前的矛盾。
 
解题思路:跟上一篇并查集思路差不多(传送门:http://www.cnblogs.com/WWkkk/p/7304904.html),(发现这种方法挺容易理解的。。关键是不用这个数组那个数组的那么麻烦。。)。也是将其分为两块区域,1-n ,n+1-2n,分别来判断之间是奇数还是偶数。如果是偶数,那么(x,y)在一个集合,(x+maxn,y+maxn)在一个集合,在放进去前先需要判断(x,y+maxn)(x+maxn,y)在不在一个集合,如果在,就说明这条开始错了。同理如果是奇数,那么(x+maxn,y)在一个集合(x,y+maxn)在一个集合,也需要先判断(x,y)(x+maxn,y+maxn)在不在一个集合。
 
下面是代码
#include<cstdio>
#include<cmath>
using namespace std;
const int maxm = 1e5+5;
const int maxn = 50003;
int f[maxm];
long long m;
int n;
char s[5];


int Find(int x)
{
    int r=x;
    while(r!=f[r]) r = f[r];
    while(x!=f[x])
    {
        int j = f[x];
        f[x] = f[r];
        x = j;
    }
    return x;
}

void merge2(int x,int y)
{
    int fx = Find(x);
    int fy = Find(y);
    if(fx!=fy)
    {
        f[fy] = fx;
    }
}

bool same(int x, int y)
{
    return Find(x)==Find(y);
}

int main()
{
    scanf("%lld %d",&m,&n);
    int i,ans=-1,x,y;
    bool flag = false;
    for(i=1;i<=2*maxn;i++)
    {
        f[i] = i;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        getchar();
        scanf("%s",s);
        if(flag==false)
        {
            x=(x-1)%maxn; y=y%maxn;
            if(s[0]=='e')
            {
                if(same(x,y+maxn)||same(x+maxn,y))
                {
                    ans = i;
                    flag = true;
                }
                else
                {
                    merge2(x,y);merge2(x+maxn,y+maxn);
                }
            }
            if(s[0]=='o')
            {
                if(same(x,y)||same(x+maxn,y+maxn))
                {
                    ans = i;
                    flag = true;
                }
                else
                {
                    merge2(x,y+maxn);
                    merge2(x+maxn,y);
                }
            }
        }
    }
    if(ans==-1)
    {
        printf("%d
",n);
    }
    else
        printf("%d
",ans);
}
原文地址:https://www.cnblogs.com/WWkkk/p/7326832.html