poj 1733 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' 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

思路

一类经典的并查集题目,经典模型就是将[l,r]这个区间化为[l-1,r],那么1的个数就可以表示为sum[r]-sum[l-1],也就确定了奇偶性,我们可以用r[]数组表示这个端点到它的根节点的1的奇偶(这个区间就是(i,root(i)](0代表偶,1代表奇) 对于每个输入的区间,我们查找它们的根节点是否相同
如果相同,证明这个区间的奇偶性在之前已经得知,那么直接判断即可
如果不同,那么就是u-1与v此时不在同一个集合中,那么我们可以知道(u-1,root([u-1])]区间和(v,root([v])]区间1的奇偶,并且我们知道了(u-1,v]区间1的奇偶,那么就可以推算出(root([u-1]),root([v])]区间的属性,进而合并两者。在合并时,根节点,r[root(u)]=r(u)^r(v)^r(u-1,v], 在路径压缩过程中r[i]=r[i]^r[root(i)],比如(a,b]中1的个数为偶数,(b,c]中1的个数为奇数,(a,c]中1的个数显然为奇数
注意:这题目的点最大值高达10^9,而语句只有5000条,所以需要离散化一下

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=50000;
map<int,int>mp;
char s[10];
int n,m,cnt=0,father[N],dis[N],d=0,u,v;
void init()
{
    for (int i=0;i<N;i++)
    father[i]=i,dis[i]=0;
    return ;
}
int getfather(int x)
{
    if (father[x]==x) return x;
    int f=getfather(father[x]);
    dis[x]=(dis[x]+dis[father[x]])%2;
    father[x]=f;
    return father[x];
} 
int merge(int x,int y,int v)
{
    int fx=getfather(x),fy=getfather(y);
    if (fx==fy)
    {
        if (dis[x]==(dis[y]+v)%2) return 1;
        return 0;
    }
    dis[fx]=(dis[y]-dis[x]+v+2)%2;
    father[fx]=fy;
    return 1;
}
int main()
{
    n=read();m=read();
    init();
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%s",&u,&v,s);
        if (s[0]=='o') d=1;
        else d=0;
        u--;
        if(!mp.count(u)) mp[u]=++cnt;//离散化
        if(!mp.count(v)) mp[v]=++cnt;
        u=mp[u];v=mp[v];
        if (merge(u,v,d)) continue;
        else
        {
            printf("%d\n",i-1);
            return 0;
        }
    }
    printf("%d\n",m);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值