Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6816   Accepted: 2636

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

和上一题很类似,但是这题给的范围太大,数组开不下,所以直接用map映射一下,还有个性质就是  奇数+奇数=偶数 奇数+偶数=奇数 偶数+偶数=偶数。
括号打偏了WA了一发,囧
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; map<int,int> ROOT;
map<int,int> SUM;
int N,M,ANS;
bool FLAG; int find_father(int);
void unite(int,int,int);
int main(void)
{
int x,y,w;
char box[]; while(scanf("%d%d",&N,&M) != EOF)
{
ROOT.clear();
SUM.clear();
FLAG = false;
for(int i = ;i <= M;i ++)
{
scanf("%d%d%s",&x,&y,box);
y ++;
if(FLAG)
continue;
w = box[] == 'e' ? : ;
unite(x,y,w);
if(FLAG)
ANS = i - ;
}
if(!FLAG)
ANS = M;
printf("%d\n",ANS);
} return ;
} int find_father(int n)
{
if(!ROOT[n])
return n;
int temp = ROOT[n];
ROOT[n] = find_father(ROOT[n]);
SUM[n] = (SUM[n] + SUM[temp]) % ;
return ROOT[n];
} void unite(int x,int y,int w)
{
int fx = find_father(x);
int fy = find_father(y); if(fx == fy)
{
if((SUM[x] + - SUM[y]) % != w)
FLAG = true;
return ;
}
if(fx < fy)
{
ROOT[fx] = fy;
SUM[fx] = (-SUM[x] + w + SUM[y]) % ;
}
else
{
ROOT[fy] = fx;
SUM[fy] = (-SUM[y] - w + SUM[x]) % ;
}
}

POJ 1733 Parity game (并查集)的更多相关文章

  1. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  2. Parity game POJ - 1733 带权并查集

    #include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...

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

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

  4. POJ 1733 Parity game(种类并查集)

    http://poj.org/problem?id=1733 题意: 给出一个01串,有多次询问,每次回答[l,r]这个区间内1的个数的奇偶性,但是其中有一些回答是错误的,问到第几个回答时与前面的回答 ...

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

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

  6. POJ - 1733 Parity game 种类并查集+离散化

    思路:d(i, j)表示区间(i, j]的1的个数的奇偶性.输入最多共有5000*2个点,需要离散化处理一下.剩下的就是并查集判冲突. AC代码 #include <cstdio> #in ...

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

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

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

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

  9. poj 1733 Parity game【hash+带权并查集】

    hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...

随机推荐

  1. SOA服务开发小计

    http://item.jd.com/11181846.html#comment SOA面向服务架构——SOA的概念 http://www.cnblogs.com/leslies2/archive/2 ...

  2. SPSS二次开发

    在以前关于SPSS二次开发文章中留下过自己联系方式,差不多一年的时间,零零散散的和我取得联系的人也有几十位,看来对于SPSS二次开发的需求不少. Web SPSS系统是利用SPSS二次开发技术,使用户 ...

  3. as [Frame]元标签

    [Frame(factoryClass="XXX_Class")] 利用Frame元标签.在主SWF类名上面添加 [Frame(factoryClass="加载类类名&q ...

  4. C#利用SharpZipLib解压或压缩文件夹实例操作

    最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode. ...

  5. CMSIS RTOS -- embOS segger

    #ifndef __CMSIS_OS_H__ #define __CMSIS_OS_H__ #include <stdint.h> #include <stddef.h> #i ...

  6. Windows Server Backup备份Exchange2010

    在Windows Server 2008 R2 SP1上Exchange2010 DAG备份测试成功: 1.分别在DAG成员服务器上安装WSB,不可以安装其命令行工具,因为其需要早期的PowerShe ...

  7. 微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!

    [导语]我们希望能够对一些个人开发人员进行专訪,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价值的文章还是能够的,得到一些业内大 ...

  8. 复选框输入Android Studio 如果修改LogCat的颜色,默认全是黑色看着挺不舒服的

    今天一直在查找复选框输入之类的问题,上午正好有机会和大家分享一下. 怎么找到并表现LogCat这里就不需要再讲了吧,主要说一下本篇的主题,如何修改他的颜色 .我们在使用Eclipse的时候应该都用过L ...

  9. C#中的ICollection接口

    一.集合类: 1.1 ICollection接口 前面我们学习了数组,这是.net Framework定义的最基本的集合类型,除过数组外,.net Framework还另外定义了很多集合类型以满足编程 ...

  10. Shell脚本[运算表达式,条件控制语句]

    #!/bin/bash #你值得收藏的四则表达式运算. val1=1 val2=1 val3=1 val4=1 val5=1 val6=1 val7=1 let val1++ ((val2++)) v ...