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. python爬取豆瓣流浪地球影评,生成词云

    代码很简单,一看就懂. (没有模拟点击,所以都是未展开的) 地址: https://movie.douban.com/subject/26266893/reviews?rating=&star ...

  2. c语言中 *p++ 和 (*p)++ 和 *(p++) 和 *(++p) 和++(*p)和 *(p--)和 *(--p)有什么区别?

    *p++是指下一个地址; (*p)++是指将*p所指的数据的值加一; /******************解释**********************/ ->C编译器认为*和++是同优先级 ...

  3. go学习笔记-变量作用域

    变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. 变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函数定义中的变量称为形式 ...

  4. 生産管理(PP)

    伝票系 製造指図 マスタ系 生産資源/治工具 作業区 能力 作業手順 作業バージョン 作業記録 需要予測プロファイル 計画手配 MRP レシピ その他 カスタマイズ系 BOM関連 製造指図確認 伝票系 ...

  5. 如何在Moodle中显示PPT课件

    Moodle中目前是不直接支持PPT的,所以需要曲线救国: 1.安装 iSpring Free 8版本,免费版,其实是一个PPT的插件,在PPT的工具栏中有显示. 2.打开PPT后,直接在该工具中进行 ...

  6. 如何打war包

    1. 利用jdk里的工具 例如我们要打包的文件在D:\myHome\dist: 运行 cmd: cd D:\myHome\dist 进入D:\myHome\dist 然后输入 D:\myHome\di ...

  7. Putty的设置保存

    用了好几年都不知道这功能, 以前每次在连接时只能手工更改字符为utf-8,当时在想怎么这么弱呢 后来才知道... 1 字符 Translation下  字体Appearance下 颜色Colours下 ...

  8. fiddler显示出服务器IP方法

    fiddler的配置中是看不到服务器的IP的 1.打开进入fiddler界面,按快捷键ctrl+r 或者按照图中点击,进入customrules.js文件里. 2.在customrules.js文件里 ...

  9. window平台下使用python虚拟环境

    第一步:安装virtualenv模块 安装virtualenv模块,使用pip install C:\Users\wangjun>pip install virtualenv 第二步:创建虚拟环 ...

  10. cocos2d-x 场景切换

    场景切换的方法 场景切换是通过导演类director实现的,其中的相关方法如下: director.run(new_scene).该方法可以运行场景,只能在启动第一个场景时调用该方法.如果已运行场景, ...