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. 014医疗项目-模块一:删除用户的功能l

    删除用户的功能我们还是按照:Dao->Service->Action->页面调试这种顺序来写. Dao: 我们使用逆向工程生成的方法就好: SysuserMapper sysuser ...

  2. 第二章 rabbitmq在mac上的安装

    下载页: http://www.rabbitmq.com/install-standalone-mac.html 1.下载页面首部的文件(页面下载可能比较慢,使用迅雷下载就好),之后解压到一个合适的路 ...

  3. VMware 不可恢复错误(svga)”解决方法

    虚拟机VMware 文件在迁移到另一台计算机时出现"VMware Workstation 不可恢复错误(svga)"  将另一台机器的 VMware 文件拷贝至本机,打开虚拟机出现 ...

  4. C语言 常用的时间函数

    //时间函数的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include& ...

  5. yield(C# 参考)

    yield(C# 参考) 在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.   通过使用 yield 定义迭代器,可在实现自定义集合类型的 IEnumer ...

  6. [CareerCup] 9.2 Robot Moving 机器人移动

    9.2 Imagine a robot sitting on the upper left corner of an X by Y grid. The robot can only move in t ...

  7. 20135316王剑桥 linux第十一周课实验笔记

    getenv函数 1.获得环境变量值的函数 2.参数是环境变量名name,例如"HOME"或者"PATH".如果环境变量存在,那么getenv函数会返回环境变量 ...

  8. WDCP安装并配置php5.4和mongodb

    记录一下,免得忘了.全部都是自己测试用过的安装过程没有问题. linux常用命令 mv 移动文件 mkdir 创建文件夹 rm 删除 cp 复制 netstat 网络状态 tar 解压 wget 下载 ...

  9. [bzoj 1503][NOI 2004]郁闷的出纳员(平衡树)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 分析: 经典的平衡树题,我用Treap做的 下面有几点注意的: 1.可能出现新加入的人的 ...

  10. 线段树好题(2004集训队林涛PPT中的3题)

    1.snake:主要是要意识到全局的可能连法只有一种= =(略坑,题目的最小长度是唬人的……),所以关键就是能否构造出符合题意的图,可以考虑搜索解决,搜出一个就OK了,但是会发现那些满足条件中线段在非 ...