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
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 10004
#define INF 0x3f3f3f3f
typedef long long LL;
/*
数据量太大,数组开不下,在这里用map+并查集
*/
int pre[MAXN],rank[MAXN],n,m,tol;
map<int,int> Mp;
int insert(int x)
{
if(Mp.find(x)==Mp.end())
Mp[x] = tol++;
return Mp[x];
}
int find(int x)
{
if(pre[x]==-)
return x;
int fx = pre[x];
pre[x] = find(pre[x]);
rank[x] = (rank[x]+rank[fx])%;
return pre[x];
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
memset(pre,-,sizeof(pre));
memset(rank,,sizeof(rank));
tol = ;
Mp.clear();
int x,y,d,u,v,i;
char op[];
bool f = false;
for(i=;i<=m;i++)
{
cin>>x>>y>>op;
if(f) continue;
d = (op[]=='e')?:;
u = insert(x-);
v = insert(y);
int f1 = find(u),f2 = find(v);
if(f1==f2)
{
if((rank[u]+d)%!=rank[v])
{
f = true;
cout<<i-<<endl;
continue;
}
}
else
{
pre[f2] = f1;
rank[f2] = (rank[u]-rank[v]+d+)%;
}
}
if(!f)
cout<<m<<endl;
}
return ;
}

H - Parity game 并查集的更多相关文章

  1. POJ 1733 Parity game (并查集)

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

  2. 并查集——合作网络D306

    合作网络D306             运行时间限制:1000ms: 运行空间限制:51200KB: 试题描述 有n个结点,初始时每个结点的父结点都不存在.你的任务是执行若干次Set操作和Query ...

  3. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  4. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

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

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

  6. POJ 1773 Parity game 带权并查集

    分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...

  7. POJ1733 Parity game 【扩展域并查集】*

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

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

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

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

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

随机推荐

  1. 【杂文】虚拟键码表(供函数GetAsyncKeyState()使用)

    [杂文]虚拟键码表(供函数GetAsyncKeyState()使用) 什么?你说你不知道 \(GetAsyncKeyState()\) ?\(→\)戳我\(←\) (惊奇的发现 \(0\) ~ \(9 ...

  2. jQuery图片区域选择控件_imgAreaSelect

    软考报名时发现可以进行头像区域裁剪功能,F12了一下,发现使用了imgAreaSelect控件. 控件官网: http://odyniec.net/projects/imgareaselect/ 控件 ...

  3. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

  4. Xml学习笔记(2)

    不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...

  5. Dom编程的入门

    <html><head>    <meta http-equiv="Content-Type" content="text/html; ch ...

  6. Sass 主要知识点小记

    Sass 主要知识点小记 以前写样式的时候,每个元素的颜色,背景色都需要重新写一遍,然后就想CSS难道没有变量么?最后就查到Sass.但当时没有静下心来好好的看一下,今天正好有时间,就在这里边看边整理 ...

  7. Spring框架系列(二)--装配和注入Bean

    企业日常开发中,几乎都是Spring系的框架,无论是SSM.还是现在大火的SpringBoot+JPA/MyBatis,使用最大的目的就是简化开发 基本模块: 核心容器:Beans.Core.Cont ...

  8. A useful logger function in C project.

    #cat log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  9. JAVA基础数组

    数组: 数组是一种容器 存储同一类型的一组数据(必须是 类型相同的一组数据) 定义数组的公式:(有两种) 1.静态定义      1)数据类型[ ] 数组名 = {元素1,元素2,元素3,元素4,元素 ...

  10. form表单传输多余参数

    1.使用post提交表单,同时在form的action属性后添加“?参数=参数值”,经验证,可行,但是在浏览器中看不到该参数在form参数中,如下图: 上图未出现courseId属性,form代码如下 ...