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中string,time,datetime三者之间的转化

    这里time特指import time中的对象,datetime 特指from datetime import datetime中的对象,string指python自带的字符数据类型. 从使用的情况来 ...

  2. linux下磁盘分区、格式化、挂载

    (1)fdisk /dev/sdb进行分区 (2)选择n表示添加一个分区,选择d表示删除一个分区.可通过m获取帮助信息 (3)选择p表示主分区,然后输入分区大小 (4)分区完成后,可通过fdisk - ...

  3. CSS3实现3D球体旋转动画

    html <div class="ball-box"> <div class="ball"> <div class="l ...

  4. Hackerrank - [Algo] Matrix Rotation

    https://www.hackerrank.com/challenges/matrix-rotation-algo 又是一道耗了两小时以上的题,做完了才想起来,这不就是几年前在POJ上做过的一个同类 ...

  5. facebook hash key

    private void printHashKey() { try { PackageInfo info = getPackageManager().getPackageInfo( "xxx ...

  6. Asp.NET Core2.0与 EF的ABP框架入门视频教程

    https://ke.qq.com/course/287301?from=qqchat&ADUIN=1187219916&ADSESSION=1522716499&ADTAG= ...

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

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

  8. Node.js的require()的工作原理

    大多数人都知道Node.js中require()函数做什么的,但是有多少人知道它的工作原理呢?我们每天使用它加载库包和模块,但是它的内部行为原理很神秘. 我们追寻Node模块系统的核心: module ...

  9. 关键词提取TF-IDF算法/关键字提取之TF-IDF算法

    TF-IDF(term frequency–inverse document frequency)是一种用于信息检索与信息探勘的常用加权技术.TF的意思是词频(Term - frequency),  ...

  10. tensorflow的几种优化器

    最近自己用CNN跑了下MINIST,准确率很低(迭代过程中),跑了几个epoch,我就直接stop了,感觉哪有问题,随即排查了下,同时查阅了网上其他人的blog,并没有发现什么问题 之后copy了一篇 ...