POJ1733 Parity game

题意

Language:
Parity game
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13833Accepted: 5303

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

分析

如果用sum表示序列S的前缀和,那么在每个回答中:

  1. S[l~r]有偶数个1,等价于sum[l-1]和sum[r]奇偶性相同。
  2. S[l~r]有奇数个1,等价于sum[l-1]和sum[r]奇偶性不同。

那么离散化后使用边带权并查集即可。带权并查集使用异或值来表示奇偶性的关系。

时间复杂度\(O(m \log m)\)

另外把每个节点拆点,使用扩展域并查集也可以做。具体是把每个点拆成奇数和偶数点,使用2-SAT那样的连通关系即可。

代码

#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=5e3+1;
struct {int l,r,ans;}query[N];
int a[2*N],fa[2*N],d[2*N],n,m;
void read_discrete(){
	read<int>(),read(m);
	for(int i=1;i<=m;++i){
		static char str[5];
		read(query[i].l),read(query[i].r);
		scanf("%s",str),query[i].ans=str[0]=='o'?1:0;
		a[++n]=query[i].l-1,a[++n]=query[i].r;
	}
	sort(a+1,a+n+1),n=unique(a+1,a+n+1)-a-1;
}
int get(int x){
	if(x==fa[x]) return x;
	int root=get(fa[x]);
	d[x]^=d[fa[x]];
	return fa[x]=root;
}
int main(){
//	freopen(".in","r",stdin),freopen(".out","w",stdout);
	read_discrete();
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1,x,y,p,q;i<=m;++i){
		x=lower_bound(a+1,a+n+1,query[i].l-1)-a;
		y=lower_bound(a+1,a+n+1,query[i].r)-a;
		p=get(x),q=get(y);
		if(p==q){
			if((d[x]^d[y])!=query[i].ans)
				return printf("%d\n",i-1),0;
		}
		else fa[p]=q,d[p]=d[x]^d[y]^query[i].ans;
	}
	return printf("%d\n",m),0;
}

posted on 2019-03-24 11:42  autoint  阅读(143)  评论(0编辑  收藏  举报

导航