POJ1733 Parity game
题意
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13833 | Accepted: 5303 |
Description
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
Output
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3
Source
分析
如果用sum表示序列S的前缀和,那么在每个回答中:
- S[l~r]有偶数个1,等价于sum[l-1]和sum[r]奇偶性相同。
- S[l~r]有奇数个1,等价于sum[l-1]和sum[r]奇偶性不同。
那么离散化后使用边带权并查集即可。带权并查集使用异或值来表示奇偶性的关系。
时间复杂度\(O(m \log m)\)
另外把每个节点拆点,使用扩展域并查集也可以做。具体是把每个点拆成奇数和偶数点,使用2-SAT那样的连通关系即可。
代码
#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=5e3+1;
struct {int l,r,ans;}query[N];
int a[2*N],fa[2*N],d[2*N],n,m;
void read_discrete(){
read<int>(),read(m);
for(int i=1;i<=m;++i){
static char str[5];
read(query[i].l),read(query[i].r);
scanf("%s",str),query[i].ans=str[0]=='o'?1:0;
a[++n]=query[i].l-1,a[++n]=query[i].r;
}
sort(a+1,a+n+1),n=unique(a+1,a+n+1)-a-1;
}
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(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
read_discrete();
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1,x,y,p,q;i<=m;++i){
x=lower_bound(a+1,a+n+1,query[i].l-1)-a;
y=lower_bound(a+1,a+n+1,query[i].r)-a;
p=get(x),q=get(y);
if(p==q){
if((d[x]^d[y])!=query[i].ans)
return printf("%d\n",i-1),0;
}
else fa[p]=q,d[p]=d[x]^d[y]^query[i].ans;
}
return printf("%d\n",m),0;
}
POJ1733 Parity game的更多相关文章
- POJ1733 Parity game 【扩展域并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- poj1733 Parity Game(扩展域并查集)
描述 Now and then you play the following game with your friend. Your friend writes down a sequence con ...
- POJ1733 Parity game —— 种类并查集
题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- Poj1733 Parity Game(带权并查集)
题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...
- poj1733 Parity game[带权并查集or扩展域]
地址 连通性判定问题.(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来).转化后就是一个经典的并查集问题了. 带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过 ...
- [POJ1733]Parity game(并查集 + 离散化)
传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- Selenium之Action Chains类
Action Chains类常用于模拟鼠标的行为,比如单击,双击,拖拽等行为,使用下面的方法导入Action Chains类 from selenium.webdriver.common.action ...
- Web端主流框架,jquery、angular、react、vue
不得不说,前端技术发展非常迅速,时不多久就有一个新的东西冒出来,并且迅速膨胀发展,让旁观者眼花缭乱,让开发者目眩神迷,但总体上来说,这波互联网大浪潮带动了前端技术的大发展,给曾经那些苦苦挣扎于DOM操 ...
- (GoRails) 用app/decorators来取代app/helpers; delegate()方法
视频:https://gorails.com/episodes/decorators-from-scratch?autoplay=1 装饰设置风格:把Model层变的干净,但不使用app/helper ...
- C++中的this指针
1.关于this指针的一个精典回答: 当你进入一个房子后,你可以看见桌子.椅子.地板等,但是房子你是看不到全貌了.对于一个类的实例来说,你可以看到它的成员函数.成员变量,但是实例本身呢?this是一个 ...
- 5.4 使用 Razor 表达式
以下内容主要展示 Razor 所支持的各种表达式,以及如何用它们来创建视图的内容. 在一个好的 MVC 框架应用程序中,动作方法与视图的作用是清晰.分离的.其规则很简单,如表所示: 组件 要做的事 不 ...
- 用js实现个优先队列吧
队列是一种很常用的数据结构,它是一组遵循先进先出(FIFO)规则的项.在现实生活中,最常见的队列的例子就是排队.队列有一些方法,入队.出队.队列的长度,清空队列等.用js实现一个普通的队列代码如下: ...
- bzoj2209
题解: splay打标机 往下传递 记录x,y为化简后,区间有多少(,) 代码: #include<bits/stdc++.h> ; using namespace std; ],sum[ ...
- spoj8406
题解: 二分+树状数组 记录以下i在当前拍第几 代码: #include<bits/stdc++.h> using namespace std; ; int a[N],f1[N],f2[N ...
- Jboss7 部署EJB3 简明教程
什么是EJB? EJB 是 Java 企业Bean, 是JavaEE服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序.话不多说,直接看如何在本机部署EJB3. 部署环境: 操作系统 ...
- C++面向对象多线程入门
第1节 背景 为了更好的理解多线程的概念,先对进程,线程的概念背景做一下简单介绍. 早期的计算机系统都只允许一个程序独占系统资源,一次只能执行一个程序.在大型机年代,计算能力是一种宝贵资源.对于资 ...