1. 程式人生 > >【tyvj1252/poj1733】小胖的奇偶/Parity game 並查集

【tyvj1252/poj1733】小胖的奇偶/Parity game 並查集

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 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

擴充套件域並查集。

若[a,b]中出現了偶數個1,則表示[0,a-1]和[0,b]的1的奇偶性相同。
若[a,b]中出現了奇數個1,則表示[0,a-1]和[0,b]的1的奇偶性不同。

這樣就可以用並查集來維護:

fa[i]代表[0,i]有偶數個1,fa[i+len]代表[0,i]有奇數個1。

若[a,b]有偶數個1,則合併fa[a-1]和fa[b],fa[a-1+len]和fa[b+len]。
若[a,b]有奇數個1,則合併fa[a-1]和fa[b+len],fa[a-1]和fa[b+len]。

對於每次操作,提前查詢一下另一種回答所對應的集合是否是同一個,來判斷是否矛盾。

這個題需要離散化,因為查詢[a,b]時要使用的是a-1,不能離散化後再減1,因為這時候減的1實際可能代表的並不是一個單位,有可能是一個區間,因為你離散掉了。要做的就是在讀入的時候就a--,這樣就可以了。因為這個我WA了好幾遍……

程式碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int size=1000010;
int fa[size];

int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

int lsh[size];

struct input{
    int l,r,d;
}l[size];
int len,n;

int solve()
{
    for(int i=1;i<=n;i++)
    {
        int x=find(l[i].l);
        int x1=find(l[i].l+len);
        int y=find(l[i].r);
        int y1=find(l[i].r+len);
        if(l[i].d==0)
        {
            if(x==y1) return i-1;
            fa[x]=y;
            fa[x1]=y1;
        }
        else
        {
            if(x==y) return i-1;
            fa[x]=y1;
            fa[x1]=y;
        }
    }
    return n;
}

int main()
{
    scanf("%d%d",&len,&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&l[i].l,&l[i].r);
        l[i].l--;
        string s;
        cin>>s;
        if(s=="even") l[i].d=0;
        if(s=="odd") l[i].d=1;
        lsh[++lsh[0]]=l[i].l;
        lsh[++lsh[0]]=l[i].r;
    }
    sort(lsh+1,lsh+1+lsh[0]);
    len=unique(lsh+1,lsh+1+lsh[0])-lsh-1;

    for(int i=0;i<=len*3;i++) fa[i]=i;

    for(int i=1;i<=n;i++)
    {
        l[i].l=lower_bound(lsh+1,lsh+1+len,l[i].l)-lsh+1;
        l[i].r=lower_bound(lsh+1,lsh+1+len,l[i].r)-lsh+1;
    }   

    printf("%d",solve());

    return 0;
}