题目链接: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. Ubuntu 16.04安装JDK7/JDK8的两种方式

    ubuntu 安装jdk 的两种方式:1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用ppa ...

  2. bzoj4027 [HEOI2015]兔子与樱花 树上贪心

    [HEOI2015]兔子与樱花 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1320  Solved: 762[Submit][Status][Di ...

  3. [HNOI2015]实验比较 树形dp+组合数学

    在合并的时候有可以加等于,或者继续用小于, 比如siz[x]和siz[y]合并,小于的区间为max(siz[x],siz[y])<=k<=siz[x]+siz[y], 然后就是合并成多少个 ...

  4. PHP文件属性相关函数

    <meta charset= "utf-8"><?php //获取文件属性的函数 function getFilePro($filename) { //检测文件是 ...

  5. IntelliJ IDEA平台下JNI编程—HelloWorld篇

    转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53906237] JNI(Java Native I ...

  6. PHP错误处理函数set_error_handler()的用法[转载]

    定义和用法 set_error_handler() 函数设置用户自定义的错误处理函数. 该函数用于创建运行时期间的用户自己的错误处理方法. 该函数会返回旧的错误处理程序,若失败,则返回 null. 语 ...

  7. js闭包的用途[转载]

    通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1 匿名自执行函数我们知道所有的变量,如果不加上var关键字,则默认的会添加到 ...

  8. [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp

    管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...

  9. T2627 村村通 codevs

    http://codevs.cn/problem/2627/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 农民约翰被选为他们 ...

  10. loj515 贪心只能过样例(bitset)

    题目: https://loj.ac/problem/515 分析: 所有可能和的最大值是1e6 如果dp的话,dp[i][j]表示前i个数能否凑出和为j的数 这样是O(n^5)的 考虑到[j]可以用 ...