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
  1. 10
  2. 5
  3. 1 2 even
  4. 3 4 odd
  5. 5 6 even
  6. 1 6 even
  7. 7 10 odd
  8. -1

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

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

  1. #include<bits/stdc++.h>
  2. #define rep(i,a,b) for(int i=a;i<=b;i++)
  3. using namespace std;
  4. const int maxn=;
  5. int L[maxn],R[maxn],val[maxn],fa[maxn],sum[maxn];
  6. int b[maxn],tot,ans; char s[];
  7. int find(int x){
  8. if(x==fa[x]) return x;
  9. int tf=fa[x]; fa[x]=find(fa[x]);
  10. sum[x]^=sum[tf]; return fa[x];
  11. }
  12. int main()
  13. {
  14. int N,M;
  15. while(~scanf("%d",&N)){
  16. if(N==-) break;
  17. scanf("%d",&M); tot=ans=;
  18. rep(i,,M) {
  19. scanf("%d%d%s",&L[i],&R[i],s); L[i]--;
  20. b[++tot]=L[i]; b[++tot]=R[i];
  21. if(s[]=='e') val[i]=;
  22. else val[i]=;
  23. }
  24. sort(b+,b+tot+); tot=unique(b+,b+tot+)-(b+);
  25. rep(i,,M) {
  26. L[i]=lower_bound(b+,b+tot+,L[i])-b;
  27. R[i]=lower_bound(b+,b+tot+,R[i])-b;
  28. }
  29. rep(i,,tot) fa[i]=i,sum[i]=;
  30. rep(i,,M){
  31. int tu=find(L[i]),tv=find(R[i]);
  32. if(tu==tv){
  33. if((sum[L[i]]^sum[R[i]])!=val[i]) break;
  34. }
  35. else {
  36. fa[tu]=tv,sum[tu]=sum[R[i]]^sum[L[i]]^val[i];
  37. }
  38. ans=i;
  39. }
  40. printf("%d\n",ans);
  41. }
  42. return ;
  43. }
  44. //不能有奇环!用带权并查集来优化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. robot 批处理文件

    robot自带的ride工具不好用,就像填表格似的写脚本,太拘束.所以一直在用sublime text写robot脚本,但是也有问题:用sublime text写的脚本,只能运行一个文件的case,并 ...

  2. 拓扑排序 Topological Sort

    2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...

  3. java生产条形码

      一.通过JBarcode(此种方式可以隐藏掉条形码下面的字符串)   1.下载jar包  jbarcode-0.2.8.jar  目前maven中央仓库并没有jbarcode的坐标  如果是mav ...

  4. 浅谈Linux

    Linux系统最初由芬兰赫尔辛基大学的Andrew S.Tanenbaum写的MINIX操作系统演变而来,这是一个小型操作系统,主要用于教学,1991年1月,Tanenbaum的学生Linus Tor ...

  5. 51nod-1534-博弈

    1534 棋子游戏  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 波雷卡普和瓦西里喜欢简单的逻辑游戏.今天他们 ...

  6. UVA-1632 Alibaba (区间DP+滚动数组)

    题目大意:在一条直线上有n件珠宝,已知每件珠宝的位置,并且第 i 件珠宝在 ti 时刻就消失,问能否将所有的珠宝收集起来?如果能,求出最短时间.搜集能瞬间完成. 题目分析:区间DP.dp(i,j,0) ...

  7. 基础最短路(模板 bellman_ford)

    Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...

  8. Google浏览器设置变更默认搜索引擎为百度

  9. 二、为什么要用MapReduce

    一.为什么要用MapReduce? 首先MapReduce被广泛应用于日志分析.海量数据的排序.在海量数据中查找特定模式等 场景.而且它非常简单,易于实现且扩展性强.可以通过它编写同事在多台主机上运行 ...

  10. OC 构造方法(对象初始化)

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情,1)使用alloc方法来分配存储空间(返 ...