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. 原始的生成对抗网络GAN

    论文地址:https://arxiv.org/pdf/1406.2661.pdf 1.简介: GAN的两个模型 判别模型:就是图中右半部分的网络,直观来看就是一个简单的神经网络结构,输入就是一副图像, ...

  2. python数据持久存储-pickle模块

    pickle模块实现了基本的数据序列和反序列化.pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,通过pickle模块的反序列化操作,能够从文件中创建上一次程序保存的对象. 接 ...

  3. HDU 6098 Inversion

    Inversion 思路:从大到小排序后,每次找到第一个下标不整出i的输出. 代码: #include<bits/stdc++.h> using namespace std; #defin ...

  4. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  5. jsp/post中文乱码问题

    在 iso-8859-1,gb2312, utf-8 以及任意一种编码格式下,英文编码格式都是一样的,每个字符占8位,而中文就麻烦了,在gb2312 下一个中文占 16位,两字节,而在utf-8 下一 ...

  6. [.NET源码] EF的增删改查

    EF的增删改查 创建上下文对象:WordBoradEntities db = new WordBoradEntities(); 一.添加: //1.1创建实体对象 User uObj = new Us ...

  7. codeforces 497b// Tennis Game// Codeforces Round #283(Div. 1)

    题意:网球有一方赢t球算一场,先赢s场的获胜.数列arr(长度为n)记录了每场的胜利者,问可能的t和s. 首先,合法的场景必须: 1两方赢的场数不一样多. 2赢多的一方最后一场必须赢. 3最后一场必须 ...

  8. Andorid 之日历控件,可左右滑动,包含公历,农历,节假日等

    公司项目需要日历这个功能,经过查阅资料写了个demo,包含公历,农历,节假日等,还可左右滑动. 效果图: 代码: public class MainActivity extends AppCompat ...

  9. JS冒泡排序的6种写法(武当雄风)

    天下英雄出我辈,一入江湖岁月催.鸿图霸业谈笑间,不胜人生一场醉. 武当山上,一年一度的试道大会又开始了... 众武当弟子摩拳擦掌都想在此次试道大会上一展风采... 张三丰临终前曾留下一句话:试道大会采 ...

  10. mongodb控制台中文乱码

    问题描述: 使用命令行打开mongo,查询的结果里中文都是乱码,检查了文件编码均正常: 解决方法: 该问题是cmd字体引起的,设置cmd的字体即可,cmd的默认字体是“点阵字体”,选择其他两个均可,如 ...