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. Shell循环输入符合条件为止

    提供用户输入,直到输入d/D/r/R为止. #!/bin/bash ]; do echo -n "(D)ebug or (R)elease?" read select_build_ ...

  2. GitHub 中国区前 100 名到底是什么样的人?

    转一下CSDN的文章, 这里有些人挺厉害的. http://geek.csdn.net/news/detail/66000

  3. Java 写数据到文件

    private boolean writeToFile(BusGpsBean gpsBean) { String dataStr = DateUtil.date2String(new Date(), ...

  4. Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、ArrayUtil、PropsUtil

    字符串工具类 StringUtil.java package com.***.util; /** * StringUtil * @description: 字符串工具类 **/ public clas ...

  5. 51 jquery 节点操作和 bootstrapt

    jquery 和 bootstrapt1.jquery each 函数 1.each 循环方式一: 可循环对象: var arr =["alex","deng" ...

  6. JQuery $未定义

    ---恢复内容开始--- JQuery $未定义 转载▼   jquery是Yii集成的,利用jquery写的代码$(document).ready(function(){// 操作列表$('.ope ...

  7. vs2012团队连接(Team Foundation Server)连接不上的怎么办?

    项目管理的Team Foundation Server有时总是连接不上,报连接有误或没有权限,那怎么解决呢?

  8. js框架封装简单实例

    (function(){ window["event"] = {} //event注册到window上面 function init(data){ // 定义一个init内部函数 ...

  9. (C#基础) ref 和out练习

    对于C#中这两个关键字的用法,常常混淆,有点不清楚,今天又一次看到.遂把它们都记录下来,希望能有所用.这些都是他人写的,我只是搬过来一次,加深印象. 代码 using System; using Sy ...

  10. POJ 3308 Paratroopers 最大流,乘积化和 难度:2

    Paratroopers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7267   Accepted: 2194 Desc ...