Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7288   Accepted: 2833

Description

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 <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = ;
int a[*MAX],father[*MAX],r[*MAX];
struct Node
{
int u,v;
char str[];
};
Node que[MAX];
int n,m,cnt;
int Bin(int x)
{
int r = cnt - ;
int l = ; while(r >= l)
{
int mid = (r + l) / ;
if(a[mid] > x)
r = mid - ;
else if(a[mid] < x)
l = mid + ;
else
return mid;
}
return -;
}
int find_father(int x)
{
if(father[x] == x)
return x;
int t = find_father(father[x]);
r[x] = r[x] ^ r[father[x]];
return father[x] = t;
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
cnt = ;
for(int i = ; i <= m; i++)
{
scanf("%d%d%s", &que[i].u,&que[i].v,que[i].str);
que[i].u --;
a[cnt++] = que[i].u;
a[cnt++] = que[i].v;
}
sort(a,a + cnt);
cnt = unique(a,a + cnt) - a;
for(int i = ; i <= cnt; i++)
{
father[i] = i;
r[i] = ;
}
int ans = ;
for(int i = ; i <= m; i++)
{
int x = Bin(que[i].u);
int y = Bin(que[i].v);
int fx = find_father(x);
int fy = find_father(y);
if(fx == fy)
{
if(r[x] == r[y] && strcmp(que[i].str,"odd") == )
break;
if(r[x] != r[y] && strcmp(que[i].str,"even") == )
break;
ans++;
}
else
{
if(strcmp(que[i].str,"odd") == )
{
father[fx] = fy;
r[fx] = r[x]^ ^ r[y];
}
else
{
father[fy] = fx;
r[fy] = r[x] ^ r[y] ^ ;
}
ans ++;
}
}
printf("%d\n",ans);
} return ;
}

poj1733Parity game的更多相关文章

随机推荐

  1. 1017. A除以B (20)

    本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...

  2. Wireshark命令行工具tshark

    Wireshark命令行工具tshark 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把Data部 ...

  3. 架构MVC——JS中的理论

    什么是MVC MVC是一种设计模式,它将应用划分为3个部分:数据(模型).展现层(视图)和用户交互层(控制器). 一个事件发生的过程可以描述如下: 用户和应用产生交互 控制器的事件处理器被触发 控制器 ...

  4. JS 之DOM range对象

    DOM范围 DOM中的range对象是DOM2中新定义的接口.通过这个对象可以选择文档中的某个区域,而不必考虑节点的界限. 创建范围 document.createRange()创建一个范围,这个范围 ...

  5. [CareerCup] 8.2 Call Center 电话中心

    8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...

  6. 启动页面设置,icon图标设置

    更多尺寸像素如何放置请看:http://chicun.jammy.cc/ 如何设置App的启动图,也就是Launch Image? 新建一个iosLaunchImage文件夹

  7. IOS开发之——Masonry 只支持OC,暂不支持swift

    前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...

  8. 20145208 实验三 Java面向对象程序设计

    20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...

  9. Jenkins进阶系列之——16一个完整的JENKINS下的ANT BUILD.XML文件

    网上看见的,确实很全,该有的基本都覆盖到了.自己拿来稍微改改就可以用了. 注:property中的value是你自己的一些本地变量.需要改成自己的 <?xml version="1.0 ...

  10. 倒戈了,转投简书 -------->

    深情自白 还记得数月前那个月黑风高的晚上,笔主偶遇简书,被那婀娜多姿的Markdown输出深深吸引不能自拔,从此立下毒誓要两边同时发布.然而天有不测风云(这边的太丑),前思后想寝食难安之后作出决定,正 ...