POJ1733 Parity Game 并查集 边带权 and 扩展域 二种做法 算法竞赛进阶指南

描述

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.

输入

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

输出

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.

样例输入

10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

样例输出

3

来源

CEOI 1999

题意:有一个长度为n的序列
给出Q条限制        l,r,str str==odd 表示[l,r]区间内的数的和是奇数,否则为偶数
请你输出最小的不满足条件的编号-1(即最后一个满足的),如果全部满足,输出总数Q!     n<=1e9,q<=1e4

   边带权思路:用前缀和记录,sum[i]=0,表示前i项和为偶数,为1表示为奇数   显然当sum[l-1]和sum[r]奇偶性相同时,[l,r]区间内和为偶数,不同时为奇数  , 那么就用一个d数组记录x与rx=find(x)的奇偶关系  d[x]=0表示  x与rx 奇偶性相同,d[x]=1x与rx奇偶性不同,    如果  l和r 奇偶性关系确定     (find(x)==find(y))     那么x和y的关系就是 dx^dy  (请画图理解)直接和答案判断

如果不确定 rx=find(x),ry=find(y)   把rx连接到ry上 ,f[rx]=f[ry] 那么  drx = ans^dx^dy (请画图理解)         

当出现     fv为u (f[v]==u)  f[u]==w  就是v还没有状态压缩,那么dv=dv^du   (du已经更新) (请画图理解)    这种情况时,在路径压缩时更新

当然n数据范围太大,离散化就好了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int len=1e4+5;
const ll mod=1e9+7;
struct Node{
	int l,r,f;
}Q[len];
int a[len];
int f[len],d[len];
int find(int x)
{
	if(x==f[x])return x;
	int rx=find(f[x]);
	d[x]^=d[f[x]];
	f[x]=rx;
	return rx;
}
void mer(int x,int y,int ans)
{
	int rx=find(x),ry=find(y);
	f[rx]=ry;
	d[rx]=ans^d[x]^d[y];
}
int main()
{	
	int n,q;
	cin>>n>>q;
	int l=0;
	for(int i=1;i<=q;++i)
	{
		char str[9];
		scanf("%d%d%s",&Q[i].l,&Q[i].r,str);
		if(str[0]=='o')Q[i].f=1;
		else Q[i].f=0;
		a[l++]=Q[i].l-1;
		a[l++]=Q[i].r;
	}
	sort(a,a+l);
	l=unique(a,a+l)-a;
	for(int i=1;i<=l;++i)f[i]=i;
	for(int i=1;i<=q;++i)
	{
		int x=lower_bound(a,a+l,Q[i].l-1)-a+1;
		int y=lower_bound(a,a+l,Q[i].r)-a+1;
		if(find(x)!=find(y))mer(x,y,Q[i].f);
		else
		{
			if(Q[i].f!=d[x]^d[y])
			{
				cout<<i-1<<endl;
				return 0;
			}
		}
	}
	cout<<q<<endl;
}

扩展域思路:扩展域的做法就比较简单了

xo表示 x的奇数域,xe表示x的偶数域    当 x和y奇偶性相同时 mer(ox,oy),mer(ex,ey),不同时  mer(ox,ey),mer(ex,oy)

判断有没有错   如果   x和y奇偶性相同那么  ox一定要和oy在一个集合,ex和ey在一个集合

                         如果不同  ox一定要和ey在一个集合,ex和oy在一个集合         

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int len=2e4+5;
const ll mod=1e9+7;
struct Node{
	int l,r,f;
}Q[len];
int a[len];
int f[len],d[len];
int find(int x)
{
	if(x==f[x])return x;
	return f[x]=find(f[x]);
}
void mer(int x,int y)
{
	f[find(x)]=find(y);
}
int main()
{	
	int n,q;
	cin>>n>>q;
	int l=0;
	for(int i=1;i<=q;++i)
	{
		char str[9];
		scanf("%d%d%s",&Q[i].l,&Q[i].r,str);
		if(str[0]=='o')Q[i].f=1;
		else Q[i].f=0;
		a[l++]=Q[i].l-1;
		a[l++]=Q[i].r;
	}
	sort(a,a+l);
	l=unique(a,a+l)-a;
	for(int i=1;i<=l*2;++i)f[i]=i;
	for(int i=1;i<=q;++i)
	{
		int x=lower_bound(a,a+l,Q[i].l-1)-a+1;
		int y=lower_bound(a,a+l,Q[i].r)-a+1;
		if(Q[i].f)
		{
			if(find(x)==find(y)||find(x+l)==find(y+l))
			{
				cout<<i-1<<endl;
				return 0;
			}
			mer(x,y+l);
			mer(y,x+l);
		}
		else
		{
			if(find(x)==find(y+l)||find(y)==find(x+l))
			{
				cout<<i-1<<endl;
				return 0;
			}
			mer(x,y);
			mer(x+l,y+l);
		}
	}
	cout<<q<<endl;
}

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/86749852