您的位置:首页 > 其它

poj 1733 Parity game(带权并查集)

2016-10-04 12:08 483 查看
题目链接

Parity game

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8154 Accepted: 3179
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

CEOI 1999

题意:

有一个由0,1组成的数字串,现在你问一个人,。他会告诉你这一个数字串中第i位到第j位的1的个数为奇数还是偶数

要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输出这个k,要是这个人回答的都是正确的,则输出组数

odd为奇数,even为偶数。

kuangbin题解链接

这是比较经典的一类并查集的题目。

 

解题思路:hash离散化+并查集

首先我们不考虑离散化:s[x]表示(root[x],x]区间1的个数的奇偶性,0-偶数,1-奇数

每个输入区间[a,b],首先判断a-1与b的根节点是否相同

a)如果相同表示(a-1,b]之间1的个数奇偶性已知s((a-1,b])=s[a-1]^s[b],此时只需简单判断即可

b)如果不同,我们需要合并两个子树,我们将root较大的子树(例root[a])合并到root较小的子树(例root[b]),且此时s[root[a]]=s[a]^s[b]^s((a-1,b])

在路径压缩的过程中s[i]=s[i]^s[root[i]],s[root[i]]为(root[root[i]], root[i]]区间内1个数的奇偶性,例(a, b]区间1的个数为偶数,(b, c]区间1的个数为奇数,(a, c]之间1的个数显然为0^1=1奇数

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
using namespace std;
const int MAXN=10010;
int pa[MAXN],s[MAXN];
map<int,int> cnt;
int tol;
int getid(int x)
{
if(cnt.find(x)==cnt.end()) cnt[x]=tol++;
return cnt[x];
}
void init()
{
memset(pa,-1,sizeof(pa));
memset(s,0,sizeof(s));
tol=0;
cnt.clear();
}

int find(int x)
{
if(pa[x]==-1) return x;
int t=find(pa[x]);
s[x]^=s[pa[x]];
pa[x]=t;
return pa[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
init();
int ans=m;
for(int i=1;i<=m;i++)
{
char ss[10];
int u,v;
scanf("%d%d%s",&u,&v,ss);
if(u>v) swap(u,v);
if(ans<m) continue;
u=getid(u-1);
v=getid(v);
int fu=find(u),fv=find(v);
int tmp;
if(ss[0]=='e') tmp=0;
else tmp=1;
if(fu==fv)
{
int p=s[u]^s[v];
if(p!=tmp) ans=i-1;
}
else
{
pa[fv]=fu;
s[fv]=s[u]^s[v]^tmp;
}
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: