题目传送

  

Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10870   Accepted: 4182

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

  分析:这道题可以用带权并查集和扩展域并查集两种方法做,这里两种方法都讲一下吧。

  首先讲带权并查集的思路。因为题目要求的是奇偶性,可知,一个01数列中从第l个元素到第r个元素之间有奇数个1,如果令sum[]为前缀和,则等价于sum[l-1]^sum[r]=1;偶数个1的情况就是sum[l-1]^sum[r]=0;这个应该不难理解。当然这个题目中我们不需要求出sum[]具体是多少,只需要知道它们之间的关系就行了。

  那么我们就可以设置并查集了,设置一个d[]数组,表示x与fa[x]之间的关系(也就是sum[x]^sum[fa[x]])。每次询问时,先令x=l-1,y=r,找到fa[x]与fa[y],再判断一下,如果x,y在同一并查集里,那么就直接看d[x]^d[y]是否等于所询问的关系,如果不满足,直接输出答案然后退出;否则,说明目前还不知道x,y之间的关系,那就将x,y合并到同一个并查集。在操作过程中,维护d[]数组即可。

  不过由于数据范围,所以需要离散化。

  Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=2e4+;
int n,m,a[N],d[N],fa[N],ans,cnt;
struct Node{int l,r,v;}p[N];
void ready()
{
cin>>n>>m;char str[];
Fi(i,,m){scanf("%d%d%s",&p[i].l,&p[i].r,str);
p[i].v=(str[]=='e'?:);
  //这里p[i]表示区间奇偶性是否相同
  //如果为1则相同,否则不相同
a[++cnt]=p[i].l-;a[++cnt]=p[i].r;}
sort(a+,a++cnt);n=unique(a+,a+cnt+)-a-;
}
inline int find(int x)
{
if(fa[x]==x)return x;
int father=find(fa[x]);
d[x]^=d[fa[x]];
return fa[x]=father;
}
int main()
{
ready();
Fi(i,,n)fa[i]=i;
Fi(i,,m){
int x=lower_bound(a+,a+n+,p[i].l-)-a;
int y=lower_bound(a+,a+n+,p[i].r)-a;
int fx=find(x),fy=find(y);
if(fx==fy){
if(d[x]^d[y]!=p[i].v){cout<<i-;return ;}}
else{
fa[fy]=fx;d[fy]=d[x]^d[y]^p[i].v;}}
cout<<m;return ;
}

  再来讲讲扩展域并查集的思路。这种方法我个人觉得会比较好理解。基本思路也是和上面一样用前缀和之间的关系来判断。把每个变量x变成两个x_odd和x_even,也就是奇偶两种情况。对于每一个询问,用0代表偶数,1代表奇数,那么对于每个询问只有以下情况:

  1,询问为0:判断x_odd与y_even是否在同一集合中(或者判断x_even和y_odd也一样,都是等价的,原因后面会说),如果在同一集合,则矛盾,直接输出答案;否则,合并x_odd与y_odd,x_even与y_even,表示sum[x]与sum[y]同奇偶。

  2,询问为1:判断x_odd与y_odd是否在统一集合中(当然判断x_even与y_even也一样),如果在统一集合,则矛盾,直接输出答案;否则,合并x_odd与y_even,x_even与y_odd,表示sum[x]与sum[y]异奇偶。

  可知,因为合并的时候是将两种情况一起合并的,所以两种询问是等价的。

  当然也需要离散化。

  Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=4e4+;
int n,m,a[N],d[N],fa[N],ans,cnt;
struct Node{int l,r,v;}p[N];
void ready()
{
cin>>n>>m;char str[];
Fi(i,,m){scanf("%d%d%s",&p[i].l,&p[i].r,str);
p[i].v=(str[]=='e'?:);
a[++cnt]=p[i].l-;a[++cnt]=p[i].r;}
sort(a+,a++cnt);n=unique(a+,a+cnt+)-a-;
}
inline int find(int x)
{return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
ready();
Fi(i,,*n)fa[i]=i;
Fi(i,,m){
int x=lower_bound(a+,a+n+,p[i].l-)-a;
int y=lower_bound(a+,a+n+,p[i].r)-a;
int xo=x,xe=x+n;int yo=y,ye=y+n;
if(p[i].v==){
if(find(xo)==find(ye)){
cout<<i-;return ;}
fa[find(ye)]=find(xe);
fa[find(yo)]=find(xo);}
else {
if(find(xo)==find(yo)){
cout<<i-;return ;}
fa[find(yo)]=find(xe);
fa[find(ye)]=find(xo);}
}cout<<m;return ;
}

  不过有一点令蒟蒻疑惑的是,理论上第二种方法复杂度会比第一种稍高,因为询问与操作更繁琐,但实际测出的结果第一种63ms,第二种32ms,希望大佬指教。

POJ1733 Party game [带权并查集or扩展域并查集]的更多相关文章

  1. poj1733 Parity game[带权并查集or扩展域]

    地址 连通性判定问题.(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来).转化后就是一个经典的并查集问题了. 带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过 ...

  2. POJ1733 Parity game 【带权并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  3. poj1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题意:给定由0.1组成的数串长度n,询问次数m,每次询问给出a,b,s,表示区间[a,b]内1的数量为s(odd-奇数或even ...

  4. Poj1733 Parity Game(带权并查集)

    题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...

  5. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

  6. POJ-1733 Parity game(带权并查集区间合并)

    http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...

  7. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  8. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  9. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

随机推荐

  1. ZooKeeper动态配置(十四)

    概述 在3.5.0发行之前,ZK的全体成员和所有其它的配置参数是静态加载的在启动的时候并且在运行的时候不可变.操作员诉诸于"滚动重启" - 一个手动密集和改变配置文件容易出错的方法 ...

  2. .NET类型转型的四种做法(转)

    .NET类型转型的四种做法: ◆ 强制转型:(int)变量名称 ◆ int.Parse(字符串变量名称) ◆ Convert.To类型(变量名称) ◆ TryParse 强制转型 (casting) ...

  3. ConvexScore

    题目描述 You are given N points (xi,yi) located on a two-dimensional plane. Consider a subset S of the N ...

  4. Linux局域网内文件传送

    先安装ssh服务 sudo apt-get install ssh 普通传输文件,可以使用scp命令 1.将本地文件复制到目标机器: scp  文件名 用户名@目标机器IP:目标机器路径 回车后输入密 ...

  5. go通过名称来调用对应的方法

    仅仅是为了学习go语言中的反射. package main import ( "errors" "fmt" "reflect" ) func ...

  6. 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  7. Java 中的成员内部类

    内部类中最常见的就是成员内部类,也称为普通内部类.我们来看如下代码: 运行结果为: 从上面的代码中我们可以看到,成员内部类的使用方法: 1. Inner 类定义在 Outer 类的内部,相当于 Out ...

  8. 打印 pmic register value

    打印 PMIC register value 方式有二種, 一種是使用 adb shell cat pmic register 一種是直接在 code 裡 call dump pmic registe ...

  9. 通过call_usermodehelper()在内核态执行用户程序【转】

    转自:http://edsionte.com/techblog/archives/category/linux%E5%86%85%E6%A0%B8%E7%BC%96%E7%A8%8B 背景 如何在Li ...

  10. 64_g2

    gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...