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. Linux系统编程---文件I/O(open、read、write、lseek、close)

    文件描述符 定义:对内核而言,文件描述符相当于一个文件的标识,它是一个非负整数,当打开(open)一个现有文件或者创建(creat)一个新文件时,内核会向进程返回一个文件描述符 在unix中(文件描述 ...

  2. 图片分离,试用于各种文件跨站传输,post方法传输

    主要思想:把不通形式的文件或者文字,以字节编码流的形式传递过去然后反解析后重新生成原文件 //------------------------------发送部分------------------- ...

  3. EasyUI系列学习(九)-Panel(面板)

    一.加载方式 1.class加载 <div class="easyui-panel" title="面板一" style="width:500p ...

  4. WebForm vs MVC

    What is ASP.NET? ASP.NET is a Microsoft’s Web application framework built on Common language runtime ...

  5. [ POI 2005 ] Bank Notes

    \(\\\) Description 给出 \(N\) 种货币的面值 \(b_i\) 和个数 \(c_i\) ,求最少需要用多少个硬币凑出 \(Q\) 元钱,并输出任意一种方案. \(n\le 200 ...

  6. python实现qq机器人qqbot

    title: python实现qq机器人qqbot tags: python date: 2018-6-1 10:19:00 --- 以下内容为转载 一.介绍 qqbot 是一个用 python 实现 ...

  7. UVM基础之------uvm phases机制

    代码的书写顺序会影响代码的实现,在不同的时间做不同的事情,这是UVM phase的设计哲学,UVM phase提供了一个通用的TB phase 解决方案.支持显示的隐式的同步方案,运行时刻的线程控制和 ...

  8. UVM基础之---------uvm report 机制分析

    uvm 中的信息报告机制相对来说比较简单,功能上来说主要分为两部分: 第一通过ID对component的信息报告冗余级别进行控制,针对每个冗余级别进行不同的行为控制.这部分工作主要由uvm_repor ...

  9. Reuse a SSL socket

    It's possible to reuse a SSL socket after proper cleanup. See SSL Socket free and shutdown on stacko ...

  10. SQL SERVER 2008 在某表中新增一列时失败

    背景:新增列语句如:“alter table 表名 add 列名 float default 0 with values”(用VS2010做网站,这句话是在C#代码里执行的) 报错提示: 警告: 已经 ...