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. 搭建一个Web应用

    因为EasyUI会涉及到与后台数据的交互,所以使用Spring MVC作为后台,搭建一个完整的Web环境 使用gradle作为构建工具 build.gradle group 'org.zln.lkd' ...

  2. IIS mime类型

    参考网站:http://www.iwms.net/n1381c2.aspx 以下例子为iis6.0 下载安卓.苹果安装包时候,需要添加mime类型才可以下载,否则访问不到 安卓 .apk  appli ...

  3. 【C#】IDispose接口的应用

    .net的GC机制有两个问题: 一.GC并不能释放所有资源,它更不能释放非托管资源. 二.GC也不是实时的,所有GC存在不确定性.所以需要使用析构函数,但是为了不重复GC,需要做一些处理. publi ...

  4. Android Studio使用中的异常

    Android studio教程:[4]真机测试 1.连不上手机 Android Studio识别不了手机(最后还是恢复成勾中的状态),重启,Android Studio连接真机没反应? 2.连上手机 ...

  5. C++ c++与C语言的区别(实用性增强,register关键字增强,全局变量检测增强)

    //区别①:实用性增强 #include<iostream> using namespace std; //C语言中的变量都必须在作用域开始的位置定义!! //C++中更强调语言的“实用性 ...

  6. Linq To Entities 及其相关(进阶)

    上篇我们讲解了Linq To Entities的一些基本操作,这篇我们主要是讲解一些比较高级的东西:存储过程查询,SQL语句查询以及表达式树. 存储过程 首先来讲解存储过程查询. //Query a ...

  7. Arduino智能小车制作报告

    Arduino智能小车制作报告 制作成员:20135224陈实  20135208贺邦  20135207王国伊 前提: Arduino,是一个开源的单板机控制器,采用了基于开放源代码的软硬件平台,构 ...

  8. 编写高质量iOS代码与OS X代码的effective 方法小结

    一.熟悉OC: 了解OC的起源: OC和C++,Java等面向对象语言类似,不过有很方面差别.因为该语言使用  消息结构而非函数调用. 消息结构和函数调用的区别:前者是在其运行时所应执行的代码由运行环 ...

  9. ios UILocalNotification的使用

    iOS下的Notification的使用 Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iO ...

  10. 那些不好的Socket服务器设计

    基础Socket 自强的程序猿们都喜欢搞Socket,而且觉得最好自己来封装个组件出来,如果再往上,加入某种数据协议,让上层服务器开发照着此协议走,就是一个小小的框架了.于是,从头开始,最开始的服务器 ...