poj1733 - Parity game - 并查集、离散化(详解)

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

分析:

菜鸡第一次接触离散化……刚开始写的时候就是内存超限了,因为题上给出的n的范围很大:0-10^9,开数组肯定不行啦,用vector也不行……所以我们要进行离散化处理:

离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
通俗的说,离散化是在不改变数据 相对大小的条件下,对数据进行相应的缩小。例如:
原数据:1,999,100000,15;处理后:1,3,4,2;
原数据:{100,200},{20,50000},{1,400};
处理后:{3,4},{2,6},{1,5};

      适用于一些数,数值很大,但是数的个数很少的情况

用map处理,map<int,int>has;比如给的范围很大:[100000000,100000005],则has[100000000]=0,has[100000005]=1

这时就把区间缩减成[0,1]啦,左端点要减1,比如sum[3]表示区间[1,3]的1的个数,sum[6]表示[1,6]区间1的个数,区间[4,6]中1的个数=sum[6]-sum[3]

比如样例:

1 2 ---------has[0]=0,has[2]=1---------(0,1]=>(0,1]

3 4 ---------has[2]=1,has[4]=2---------(2,4]=>(1,2]

5 6 ---------has[4]=2,has[6]=3---------[4,6]=>(2,3]

1 6 ---------has[0]=0,has[6]=3---------(0,6]=>(0,3]

7 10 ---------has[6]=3,has[10]=4---------(6,10]=>(3,4]

这样就完成了离散化,所以我们不用再开n(区间的最大值)那么大的数组了,最多开m(询问的次数)大的就够了

代码如下:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int maxn=10010;
int pre[maxn],sum[maxn];
char s[7];
map<int,int>has;

int f(int x){
    if(pre[x]==x)return x;
    int tmp=pre[x];
    pre[x]=f(pre[x]);
    sum[x]=(sum[x]+sum[tmp])%2;
    return pre[x];
}
int link(int x,int y,int d){
    int root1=f(x);
    int root2=f(y);

    if(root1==root2){
        if((sum[x]+d)%2==sum[y]%2)return 1;
        else return 0;
    }
    else{
        pre[root2]=root1;
        sum[root2]=(d+sum[x]-sum[y]+2)%2;
        return 1;
    }
}

int main(){
    int n,m,l,r,v;

    scanf("%d%d",&n,&m);
    int cnt=0;
    for(int i=0;i<maxn;i++){
        pre[i]=i;
        sum[i]=0;//每个结点到自己都有0个1
    }
    int ans=0,flag=1,d;
    for(int i=1;i<=m;i++){
        scanf("%d%d%s",&l,&r,s);
        if(flag){
            l--;
            if(has.find(l)==has.end())has[l]=cnt++;
            int x=has[l];
            if(has.find(r)==has.end())has[r]=cnt++;
            int y=has[r];
            if(s[0]=='o')d=1;
            else d=0;
            if(!link(x,y,d)){flag=0;ans=i-1;}

        }

    }
    if(!ans)ans=m;
    printf("%d\n",ans);

}

猜你喜欢

转载自blog.csdn.net/m0_37579232/article/details/80159251
今日推荐