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. Vmware 虚拟的Linux系统如何与宿主主机共享上网

    学校局域网内的机器是经过一个计费登陆客户端Gmon上网的,我前两天刚用Vmware虚拟了一个Linux      Guest OS 用作测试用,在Vmware的VM>>Settings 里 ...

  2. Linux下查看文件和文件夹大小的df和du命令

        转自:http://www.yayu.org/look.php?id=162 当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比 ...

  3. MySQL主从复制的原理及配置

    [http://www.jb51.net/article/50053.htm]   MySQL 数据库的高可用性架构:         集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单 ...

  4. Python单步调试

    运行 运行python -m pdb test.py (Pdb) 会自动停在第一行,等待调试,这时你可以看看帮助 (Pdb) h 几个关键命令 断点设置 (Pdb)b 10 #断点设置在本py的第10 ...

  5. javaScript hook

    今天在网上搜索了不少资料,基本概念如下: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到 ...

  6. Flex 舞台背景渐变

    <?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="htt ...

  7. linq 动态排序,不使用反射

    之前网上搜索的相关方法都是使用了反射的方法来动态获取字段,以实现动态linq排序,但是因为项目组觉得此方法效率低下,所以不予采纳. 所以有了以下代码 public interface IBase{ d ...

  8. C++学习笔记之迭代器

    模板是的算法独立于存储的数据类型,而迭代器使算法独立于使用的容器类型.理解迭代器是理解STL的关键. 迭代器应该具备的特征: (1)应该能够对迭代器进行解除引用的操作,以便能够访问它引用的值.即如果P ...

  9. 理解sizeof

    1.sizeof返回的是字节个数,内存编址的最小单元是字节.因此,空对象,bool值占用的内存也是一个字节. 2.可以对哪些东西求sizeof ? a.对象和类型.如int a; sizeof(a), ...

  10. BZOJ 1045: [HAOI2008] 糖果传递 数学

    1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...