SSLOJ1227 Parity game&P5937

这篇博客介绍了如何使用扩展域并查集算法来解决一个关于01序列的问题,玩家需要根据朋友给出的关于子序列奇偶性的信息判断整个序列,通过编程找出第一个不正确的答案。核心是将每个点拆分成奇偶性相同的域和不同的域,并通过并查集操作对比信息的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

有一个01序列,长度<=1000000000,现在有n条信息,每条信息的形式是-a b even/odd。表示第a位到第b位元素之间的元素总和是偶数/奇数。

你的任务是对于这些给定的信息,输出第一个不正确的信息所在位置-1。信息的数目不超过5000。

如果信息全部正确,即可以找到一个满足要求的01序列,那么输出n。

Input

The first line of input file PARITY.IN 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 5 000. 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).

第一行一个整数m表示01序列的长度,第二行一个整数n表示信息的数目。

Output

There is only one line in output file PARITY.OUT 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

思路

说实话没洛谷和样例都看不懂这破题,看中文版详见P5937


这题目和食物链一样,是扩展域并查集,我们把一个点拆成2份:
x: 与该点奇偶性相同的域
y: 与该点奇偶性不同的域


那么,我们比较的是什么的奇偶性呢??是从1~i的和的奇偶性,也就是说: x i 是 与 1 至 i 的 和 奇 偶 性 相 同 的 域 , 而 y i 是 与 1 至 i 的 和 奇 偶 性 不 同 的 域 x_i是与1至i的和奇偶性相同的域,而y_i是与1至i的和奇偶性不同的域 xi1iyi1i


对于1条信息,我们如何建立这种联系呢?

如果一条信息形如:x y even,即x ~y为偶数,那么1 ~x-1的奇偶性与1 ~y的奇偶性相同。
如果一条信息形如:x y odd,即x ~y为奇数,那么1 ~x-1的奇偶性与1 ~y的奇偶性不同。


code:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,m,tot=1;
struct fP{
 int x,y;
 bool u;
} a[5001];
int f[10001],b[10001];
int find(int x)
{
 if (f[x]==x) return x;
 return f[x]=find(f[x]);
}
string o;
int main()
{
 cin>>n>>m;
 for (int i=1;i<=m;i++)
 {
  cin>>a[i].x>>a[i].y>>o; 
  a[i].x--;
  b[tot++]=a[i].x;
  b[tot++]=a[i].y;
  if (o[0]=='o')
  {
   a[i].u=1;
  }
  else a[i].u=0;
 }
 sort(b+1,b+tot);
 tot=unique(b+1,b+tot)-b-1;
 for (int i=1;i<=tot*2;i++) f[i]=i;
 for (int i=1;i<=m;i++)
 {
  int x=lower_bound(b+1,b+tot,a[i].x)-b,y=lower_bound(b,b+tot,a[i].y)-b;
  if (a[i].u==0)
  {
   if (find(x)==find(y+tot))
   {
    cout<<i-1;
    exit(0);
   }
   f[find(x+tot)]=find(y+tot);
   f[find(x)]=find(y);
  }
  else
  {
   if (find(x)==find(y))
   {
    cout<<i-1;
    exit(0);
   }
   f[find(x)]=find(y+tot);
   f[find(tot+x)]=find(y);
  }
 }
 cout<<m;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值