题目传送

  

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. nginx 负载均衡实现

    https://www.cnblogs.com/wang-meng/p/5861174.html

  2. B树和TreeSet与TreeMap

    1. 此前二叉搜索树相关的内容我们均假设可以把整个数据结构存储在计算机的内存中,但是如果数据量过大时,必须把数据结构放在磁盘上,导致大O模型不在适用.目前计算机处理器每秒至少可以执行5亿条指令,磁盘访 ...

  3. 2015/9/5 Python基础(9):条件和循环

    条件语句Python中的if语句如下: if expression: expr_true_suite 其中expression可以用布尔操作符and, or 和 not实现多重判断条件.如果一个复合语 ...

  4. Jmeter-8-FTP测试

    1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...

  5. TOJ 1049 Jesse's problem (最短路 floyd)

    描述 All one knows Jesse live in the city , but he must come to Xiasha twice in a week. The road is to ...

  6. 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)

    1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...

  7. hdu 2545 树上战争(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545 树上战争 Time Limit: 10000/4000 MS (Java/Others)     ...

  8. Linux中source命令的用法

    source命令: source命令也称为“点命令”,也就是一个点符号(.).source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录.因为linux所有的操作都会变成文 ...

  9. C++学习之路(八):关于C++提供的强制类型转换

    C语言中提供了旧式的强制类型转换方法.比如: int a  =1; char *p = (char *)&a; 上述将a的地址单元强制转换为char类型的指针.这里暂且不说上述转换结果是否合理 ...

  10. FAN54015 充電電流 軟硬體設定

    Ex1: Vrsense 選 37.4 mV --- 在第二張圖 Rsense 選 50 mΩ --- 在第三張圖 37.4 / 50 = 748 mA Ex2: Vrsense 選 44.2 mV ...