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. 5050 [JL] 他爱上了鸭蛋

    5050 [JL] 他爱上了鸭蛋  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 小明爱上了零鸭蛋.他喜欢输 ...

  2. max_allowed_packet自动恢复

    https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html http://blog.chinaunix.net/uid-20304801 ...

  3. MySQL基础 - 外键和约束

    在工作中经常会遇到不少不同的观点,比如对于数据库来说那就是是否要设置外键,设置外键的理由自然不必多说,而不设置外键的理由多半为设置外键影响性能,但就目前工作来讲,还没有涉及到因为外键而引发的数据库瓶颈 ...

  4. VSS迁移备忘

    今天早上服务器down掉了,没办法,只能把vss数据文件目录一并压缩,拷贝到本机.然后准备利用本机做服务端.下面是操作步骤: 1.将拷贝下来的文件夹设置为共享. 2.打开Microsoft Visua ...

  5. Caffe学习系列(6):Blob,Layer and Net以及对应配置文件的编写

    深度网络(net)是一个组合模型,它由许多相互连接的层(layers)组合而成.Caffe就是组建深度网络的这样一种工具,它按照一定的策略,一层一层的搭建出自己的模型.它将所有的信息数据定义为blob ...

  6. 在opencv3中的机器学习算法

    在opencv3.0中,提供了一个ml.cpp的文件,这里面全是机器学习的算法,共提供了这么几种: 1.正态贝叶斯:normal Bayessian classifier    我已在另外一篇博文中介 ...

  7. word 2010自定义快捷键提高工作效率

    经常使用word处理文档, 做笔记的时候会把word文档框缩小,以便同时看pdf同时记录笔记,但是缩小的word框不能把所有的菜单项显示出来,我比较常用那个插入边框下面的那个横线来做分割符,但是缩小了 ...

  8. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  9. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  10. 工作随笔——tar命令批量解压

    由于linux的tar命令不支持批量解压,所以很多网友编写了好多支持批量解压的shell命令,收集了一下,供大家分享: 第一: for tar in *.tar.gz; do tar xvf $tar ...