题目链接:http://poj.org/problem?id=1733

Parity game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9806   Accepted: 3795

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

Source

 
 
 
题解:
1.由于数据的范围很大(1e9),但是个数最多只有1e4,所以可以用map对其进行离散化。
2.此题相当于 POJ3038POJ2492 两题的综合。在此不作过多解释。
3.注意:种类并查集的r[]必须全部初始化为0,而不能是其他数。换句话说,根节点的r[]必须保持恒为0
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, m;
int fa[MAXN], r[MAXN];
map<int, int> M; int find(int x)
{
if(fa[x]==-) return x;
int pre = find(fa[x]);
r[x] = (r[x]+r[fa[x]])%;
return fa[x] = pre;
} bool Union(int u, int v, int w)
{
int fu = find(u);
int fv = find(v);
if(fu==fv)
return (r[v]-r[u]+)% != w; fa[fv] = fu;
r[fv] = (-r[v]+w+r[u]+)%;
return false;
} int main()
{
scanf("%d%d", &n, &m);
M.clear();
memset(fa, -, sizeof(fa));
memset(r, , sizeof(r)); int ans = m, cnt = ;
for(int i = ; i<=m; i++)
{
int u, v; char s[];
scanf("%d%d%s", &u, &v, s); u--;
if(M.find(u)==M.end()) M[u] = ++cnt;
if(M.find(v)==M.end()) M[v] = ++cnt;
/**注意,0代表偶数, 1代表奇数。否则的话所有r[]都需初始化为1,
这样也就导致了根节点的r[]也为1,而根节点的r[]必须为0,
因为在路径压缩的时候会加上根节点的r[],如果根节点的
r[]不为0, 那么路径上所有的点与根节点的关系都将发生了变化。
**/
if(ans==m && Union(M[u], M[v], (int)(s[]=='o')))
ans = i-;
}
printf("%d\n", ans);
}

POJ1733 Parity game —— 种类并查集的更多相关文章

  1. POJ - 1733 Parity game 种类并查集+离散化

    思路:d(i, j)表示区间(i, j]的1的个数的奇偶性.输入最多共有5000*2个点,需要离散化处理一下.剩下的就是并查集判冲突. AC代码 #include <cstdio> #in ...

  2. [POJ1733]Parity game(并查集 + 离散化)

    传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...

  3. poj1733 Parity Game(扩展域并查集)

    描述 Now and then you play the following game with your friend. Your friend writes down a sequence con ...

  4. poj1733(种类并查集+离散化)

    题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...

  5. POJ 1733 Parity game(种类并查集)

    http://poj.org/problem?id=1733 题意: 给出一个01串,有多次询问,每次回答[l,r]这个区间内1的个数的奇偶性,但是其中有一些回答是错误的,问到第几个回答时与前面的回答 ...

  6. poj1733(区间上的种类并查集)

    题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数.一共会告诉你几组这样的数 要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输 ...

  7. 并查集例题01. 种类并查集(poj1733)

    题目: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第y个 ...

  8. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

  9. NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...

随机推荐

  1. PS学习笔记(04)

    Photoshop滤镜的安装 Photoshop滤镜的默认格式为.8bf(也有些滤镜为exe格式的可执行文件),如果你下载的是压缩包,请解压之后再安装. 方法一: 如果你下载的滤镜为exe的可执行文件 ...

  2. 自定义UDF函数应用异常

    自定义UDF函数应用异常 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 ...

  3. python3--shelve 模块

    shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve d = shelve.open('shelve_t ...

  4. HDU 5245

    题目大意: 每次随机选择两个点,便把这两个点之间形成的子矩阵上的每一个方块涂色,问随机选择k次,整个m*n的矩阵中有多少个小方块被涂上了颜色 这道题不难,但自己智商实在捉急,一直想不出来... 因为这 ...

  5. 【git】git回退到某个历史版本(强行推送代码)

    1. 使用git log命令查看所有的历史版本,获取某个历史版本的id,假设查到历史版本的id是139dcfaa558e3276b30b6b2e5cbbb9c00bbdca96. 2. 3. 把修改推 ...

  6. POJ 2337 【字典序】【欧拉回路】

    题意: 给你一些单词,判断这些单词能否在保证首尾单词相同的情况下连成一排. 如果有多组解,输出字典序最小的一组解. 这题... WA了两天. 错误有以下: 1.没有初始化好起始位置,默认起始位置是a了 ...

  7. Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  8. java的异常与记录日志

    今天在<java编程思想>一书中看到了异常与记录日志,发现学会将异常记录进日志中还是很有必要的,以下是书中的例子: import java.io.PrintWriter; import j ...

  9. 关于Java第一次实验的对课后问题自己的理解--验证码实现及其四则运算

    问题一.对于课上ppt中EnumTest所提出的的问题进行解答 将这段代码放到文件中进行运行后发现 1.对应的Size中不同元素的并不是同一个对象 2.以其中一个枚举类型s来说,不是原始数据,即他们都 ...

  10. jdk8 stream可以与list,map等数据结构互相转换

    前面我们使用过collect(toList()),在流中生成列表.实际开发过程中,List又是我们经常用到的数据结构,但是有时候我们也希望Stream能够转换生成其他的值,比如Map或者set,甚至希 ...