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

Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 10 9. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. 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). The input is ended with a line containing −1 .

Output

Each line of 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.

Example

input output
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1

题意:N个点,给出M组关系,每组给出[L,R]的奇偶,问前几个是没有矛盾的。

思路:我们用[L-1,R]连边边权为其奇偶性,如果出现了全为1的奇环,那么就出现矛盾了。所以可以用带权并查集来做。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int L[maxn],R[maxn],val[maxn],fa[maxn],sum[maxn];
int b[maxn],tot,ans; char s[];
int find(int x){
if(x==fa[x]) return x;
int tf=fa[x]; fa[x]=find(fa[x]);
sum[x]^=sum[tf]; return fa[x];
}
int main()
{
int N,M;
while(~scanf("%d",&N)){
if(N==-) break;
scanf("%d",&M); tot=ans=;
rep(i,,M) {
scanf("%d%d%s",&L[i],&R[i],s); L[i]--;
b[++tot]=L[i]; b[++tot]=R[i];
if(s[]=='e') val[i]=;
else val[i]=;
}
sort(b+,b+tot+); tot=unique(b+,b+tot+)-(b+);
rep(i,,M) {
L[i]=lower_bound(b+,b+tot+,L[i])-b;
R[i]=lower_bound(b+,b+tot+,R[i])-b;
}
rep(i,,tot) fa[i]=i,sum[i]=;
rep(i,,M){
int tu=find(L[i]),tv=find(R[i]);
if(tu==tv){
if((sum[L[i]]^sum[R[i]])!=val[i]) break;
}
else {
fa[tu]=tv,sum[tu]=sum[R[i]]^sum[L[i]]^val[i];
}
ans=i;
}
printf("%d\n",ans);
}
return ;
}
//不能有奇环!用带权并查集来优化2-sat就是这么来的。

URAL - 1003:Parity (带权并查集&2-sat)的更多相关文章

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

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

  2. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

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

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

  4. POJ 1773 Parity game 带权并查集

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

  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. Poj1733 Parity Game(带权并查集)

    题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...

  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. 家里各台机器的php性能测试

    所用脚本: <?php $before = microtime(true); $list= array( "keya" => "the value a&quo ...

  2. sql一些语句性能及开销优化

    1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分开的操作尽量分开处理,提高每次的响应速度:在数据窗口使用 ...

  3. Vue.js 渲染函数, JSX(未掌握,未学完)

    渲染函数 , JSX(没完成学习) 基础: 实例属性:vm.$slots default 属性包括了所有没有被包含在具名插槽中的节点. 渲染函数: render: function(createEle ...

  4. spoj The Next Palindrome

    题意:比给出的数大的最小回文数. 先用前n/2长对称到后面,如果没变大,在中间加1,进位,再对称. //#pragma comment(linker,"/STACK:1024000000,1 ...

  5. 『cs231n』线性分类器损失函数

    代码部分 SVM损失函数 & SoftMax损失函数: 注意一下softmax损失的用法: SVM损失函数: import numpy as np def L_i(x, y, W): ''' ...

  6. hdu-1892-二维BIT

    See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Su ...

  7. 在Windows下为PHP安装redis扩展

    1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.选择http://windows.php.net/downloads/pecl/snaps/redis/2.2.5/ ht ...

  8. CentOS下tar解压 gz解压 bz2等各种解压文件使用方法

    .tar  解包:tar xvf FileName.tar  打包:tar cvf FileName.tar DirName  (注:tar是打包,不是压缩!)  ———————————————  . ...

  9. 为何 Delphi的 Local Variables 突然没有值显示了

    可能是上次编译后  code未再修改过. 试试 随便 输入一个空格,然后F9

  10. js 怎样获取div 图片等的宽度,只要值,不要px

    给你的div命名id=“abc”,js中用下面的语句就能获取到js的宽度并赋值给a a=document.all.abc.offsetWidth; 然后通过 alert(a); 就能弹出这个值来了.