POJ1733:Parity Game(离散化+带权并查集)
Parity Game
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12853 | Accepted: 4957 |
题目链接:http://poj.org/problem?id=1733
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串,然后给出一些信息,是关于[x,y]区间中有奇数个1还是偶数个1,求出X,使得1-X条信息都可以满足。
题解:
如果不考虑数据范围,很明显就是带权并查集,用一个数组v来维护当前结点与其父亲结点的关系:v[x]=1 <==>(x,f[x]]有奇数个1;否则v[x]=0就是偶数个个1。
再来看一下数据范围,区间很大, 如果直接开数组那么肯定存不下的;再来看下信息个数,只有5000个,也就是最多也只有10000个区间端点。
所以我们考虑离散化(区间端点的具体值我们并不关心,主要是相对大小),把原来范围很大的区间变小。
我用的是map。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std; typedef long long ll;
const int N = ;
int f[N],v[N];
map<ll,int> mp;
ll n,x,y;
int q,tot; int find(int x){
if(x==f[x]) return x;
int tmp = f[x];
f[x]=find(f[x]);
v[x]+=v[tmp];
if(v[x]>=) v[x]-=;
return f[x];
} int main(){
cin>>n>>q;
int flag = ,ans = q;
for(int i=;i<=;i++) f[i]=i,v[i]=;
for(int i=;i<=q;i++){
char s[];
scanf("%lld%lld %s",&x,&y,s);
if(flag) continue ;
if(!mp[x]) mp[x]=++tot;
if(!mp[y+])mp[y+]=++tot;
int fx=find(mp[x]),fy=find(mp[y+]),pd;
if(s[]=='e') pd=;else pd=;
if(fx==fy){
int now = (v[mp[x]]+v[mp[y+]])%;
if((pd && !now) || (!pd &&now)) continue;
else{
ans=i-;
flag=;
continue;
}
}
f[fx]=fy;
if(pd) v[fx]=(v[mp[x]]+v[mp[y+]])%;
else if(!pd) v[fx]=(v[mp[x]]+v[mp[y+]]+)%;
}
printf("%d",ans);
return ;
}
POJ1733:Parity Game(离散化+带权并查集)的更多相关文章
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- Poj1733 Parity Game(带权并查集)
题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
- poj 1733 Parity game(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...
- POJ 1733 Parity game 【带权并查集】+【离散化】
<题目链接> 题目大意: 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. 解题分析: 我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a- ...
- AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)
小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...
- Parity game(带权并查集+离散化)
题目链接 //kuangbin 题意: 现在你和你的朋友正在玩一种游戏. 你的朋友写下一串0和1的序列,然后你选择其中一串子序列(如[3,5])并且问他这个序列是包含奇数个1还是偶数个1(和是奇数还 ...
- POJ 1733 Parity game(带权并查集)
题目链接:http://poj.org/problem?id=1733 题目大意:给你m条信息,每条信息告诉你区间l~r的1的个数是奇数还是偶数,如果后面出现信息跟前面矛盾则这条信息是错误的,问在第一 ...
随机推荐
- Python3 列表,元组,字典,字符串知识小结
一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...
- python2.7入门---文件I/O&简单用户交互
这篇文章开始之前,我们先来看下python中的输出方法.最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写 ...
- shell重温---基础篇(shell变量&字符串以及git GUI运行shell脚本方式)
既然是基础篇那肯定是需要对shell的各种需要注意的基本点进行说明了.接下来就是show time... shell呢,是一个用C语言编写的应用程序,是用户使用linux的桥梁.所以呢,他既是一 ...
- 初步学习pg_control文件之十
接前文 初步学习pg_control文件之九 看下面这个 XLogRecPtr checkPoint; /* last check point record ptr */ 看看这个pointer究竟保 ...
- RevealTrans图片切换效果
RevealTrans 更新时间:2013-06-01 17:11:59 | RevealTrans兼容性:IE5.5+ 语法: filter : progid:DXImageTransform.Mi ...
- redis入门:介绍、特点、安装、各类型常用操作
一.redis介绍 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. Redis支持多种类型的数据结构,如 字符串(strings), 散列(ha ...
- 29、phonegap入门
0. PhoneGap介绍 0.1 什么是PhoneGap? PhoneGap是一个基于HTML.CSS.JS创建跨平台移动应程序的快速开发平台.与传统Web应用不同的是,它使开发者能够利用iPho ...
- JMeter上传图片
JMeter怎样上传图片? 请注意图片的路径要与.jmx脚本的目录保持一致, 或者放在JMeter的bin目录下. 协议:http 服务器名称或IP:www.abcdef.com 方法:POST 路径 ...
- 51单片机数码管字符H自右向左移动
#include <reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...
- 1034 Head of a Gang (30 分)(图的遍历or并查集)
dfs #include<bits/stdc++.h> using namespace std; ; int mp[N][N]; int weight[N]; int vis[N]; ma ...