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(离散化+带权并查集)的更多相关文章

  1. POJ 1733 Parity game(离散化+带权并查集)

    离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...

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

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

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

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

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

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

  5. poj 1733 Parity game(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...

  6. POJ 1733 Parity game 【带权并查集】+【离散化】

    <题目链接> 题目大意: 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. 解题分析: 我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a- ...

  7. AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)

    小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...

  8. Parity game(带权并查集+离散化)

    题目链接  //kuangbin 题意: 现在你和你的朋友正在玩一种游戏. 你的朋友写下一串0和1的序列,然后你选择其中一串子序列(如[3,5])并且问他这个序列是包含奇数个1还是偶数个1(和是奇数还 ...

  9. POJ 1733 Parity game(带权并查集)

    题目链接:http://poj.org/problem?id=1733 题目大意:给你m条信息,每条信息告诉你区间l~r的1的个数是奇数还是偶数,如果后面出现信息跟前面矛盾则这条信息是错误的,问在第一 ...

随机推荐

  1. MFC接收ShellExecute多个参数

    在应用程序开发过程中,我们经常需要带参数启动另一个执行程序,如何传递多个参数,如何解析多个参数呢?   传参数 传递参数可使用ShellExecute函数,示例如下: ShellExecute(NUL ...

  2. elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词

    elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...

  3. 【python模块】——logging

    python学习——logging模块

  4. 让PC版网站在移动端原样式显示

    一般PC网站在移动端显示效果往往和PC版原样式不同,为了在移动端下还原原PC站样式,可以采用以下方式解决: 1) 去掉页头的: <meta name="viewport" c ...

  5. 转型、java基础之Java变量命名规范 (转载)

    向上转型:基类引用指向导出类(派生类)的对象(实例)向下转型:导出类的引用指向基类的对象(实例)  重点:向下转型只有将该引用的导出类的向上转型后向下转型,运行时才不会报错 Java是一种区分字母的大 ...

  6. linux学习总结----对象

    内置对象: Date new Date() --->系统当前时间 var d=new Date() d.getFullYear() getMonth() getDay() getDate() g ...

  7. Markdown常用的几种语法

    在VScode上面写的,现将代码粘贴如下:(在VScode里运行下即可) # Markdown语法 # Ctrl + k v 打开侧边预览 ## 一.加粗斜体删除线 **这是要加粗的文字** *这是要 ...

  8. LCA(最近公共祖先)——离线 Tarjan 算法

    tarjan算法的步骤是(当dfs到节点u时):1 在并查集中建立仅有u的集合,设置该集合的祖先为u1 对u的每个孩子v:   1.1 tarjan之   1.2 合并v到父节点u的集合,确保集合的祖 ...

  9. [译]在SQL查询中如何映射(替换)查询的结果?

    问题来源: https://stackoverflow.com/questions/38567366/mapping-values-in-sql-select 有一个表格,就称它Plant,它有三列: ...

  10. beta版本冲刺五

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...