POJ1733 Parity Game 0x40「数据结构进阶」例题 带权并查集(离散化)或扩展域并查集

POJ1733 Parity Game 0x40「数据结构进阶」例题

http://bailian.openjudge.cn/practice/1733

总时间限制: 

1000ms

内存限制: 

65536kB

描述

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

带权并查集的例题。

先用前缀和sum[j]-sum[i-1]表示区间奇偶性,

然后就转化为了带上树上点异或的并查集,

可以把边权设成于父亲节点异或的结果。

路径压缩时,与祖先的异或等于沿途异或和。

合并时,利用judge[i]=d[x]^d[y]^d[get(x)],然后

利用异或的性质,可以知道,

d[p]=judge[i]^d[y]^d[get(x)];

#include <bits/stdc++.h>
using namespace std;
const int M = 100000+100;
/*template<class T>
inline T read(T&x)
{
    x=0;int 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;
}*/
int fa[M],d[M];
int l[M],r[M];
int ll[M],lr[M];
int a[M],judge[M];
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()
{
	int n,m;
	cin>>n>>m;
	char s[10];
	int cnt=0;
	for(int i=1;i<=100000+100;i++)
		fa[i]=i,d[i]=0;
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %s",&l[i],&r[i],s);
		a[++cnt]=l[i]-1,a[++cnt]=r[i];
		judge[i]=(s[0]=='o'?1:0);
	}
	
	sort(a+1,a+1+cnt);
	for(int i=1;i<=m;i++)
	{
		ll[i]=lower_bound(a+1,a+1+m+m,l[i]-1)-a;
		lr[i]=lower_bound(a+1,a+1+m+m,r[i])-a;
	//	printf("%d %d ======\n",ll[i],lr[i]);
	}
	for(int i=1;i<=m;i++)
	{
		int x=ll[i],y=lr[i];
		int gx=get(x),gy=get(y);
		if(gx==gy)
		{
		//	printf("%d+%d   -----   %d+%d   %d\n",x,gx,y,gy,i);
			int ans=d[x]^d[y];
			if(ans!=judge[i])
			{
				printf("%d\n",i-1);
				return 0;
			}
		}
		else
		{
			fa[gx]=gy;
			d[gx]=d[x]^d[y]^judge[i];
		}
	}
	printf("%d\n",m);
    return 0;
}

第二种就是用扩展域并查集。

思路和之前一样,

当e[i]==0时代表奇偶性相同,那我们就让x的odd节点和y的odd子节点合并,x的oven子节点和y的oven子节点合并

当e[i]==1时,代表奇偶性不同,那就交叉合并

先判断下到当前询问是否出现矛盾。如果要合并的节点不在一个集合里,那就矛盾。

//#include <bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int M = 100000+100;
int l[M],r[M];
int ldcz[M],rdcz[M];
int a[M],fa[M],e[M];
int get(int x)
{
	if(x==fa[x])	return x;
	return fa[x]=get(fa[x]);
}
int main()
{
	int n,m;
	cin>>n>>m;
	char s[10];
	int cnt=0;
	for(int i=1;i<=200000;i++)
	fa[i]=i;
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %s",&l[i],&r[i],s);
		a[++cnt]=l[i]-1,a[++cnt]=r[i];
		e[i]=(s[0]=='e'?0:1);
	}
	sort(a+1,a+1+cnt);
//	int mm=unique(a+1,a+1+cnt)-a-1;
	for(int i=1;i<=m;i++)
	{
		ldcz[i]=lower_bound(a+1,a+1+cnt,l[i]-1)-a;
		rdcz[i]=lower_bound(a+1,a+1+cnt,r[i])-a;
		//printf("%d   %d\n",ldcz[i],rdcz[i]);
	}
	for(int i=1;i<=m;i++)
	{
		int x_odd=ldcz[i],y_odd=rdcz[i];
		int x_oven=ldcz[i]+cnt,y_oven=rdcz[i]+cnt;
		int gx_odd=get(x_odd),gy_odd=get(y_odd);
		int gx_oven=get(x_oven),gy_oven=get(y_oven);
		if(gx_odd==gy_odd||gx_odd==gy_oven)
		{
		//	puts("ok");
			if(e[i]==0&&gx_odd!=gy_odd||e[i]==1&&gx_odd!=gy_oven)
			{
				printf("%d\n",i-1);
				return 0;
			}
		}
		else
		{
			if(e[i]==0)
				fa[gx_odd]=gy_odd,fa[gx_oven]=gy_oven;
			else
				fa[gx_odd]=gy_oven,fa[gx_oven]=gy_odd;
		}
	}
	printf("%d\n",m);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bjfu170203101/article/details/89006933