题目链接: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. shell实现ftp命令示例

    一.shell脚本示例: [plain] view plaincopy cd /PATH_YOU_WANT_TO_UPLOAD(DOWNLOAD) ftp -niv <<- EOF ope ...

  2. 用spring annotation声明的bean,当打包在jar中时,无法被扫描到

    发现一个问题,十分蛋疼. 我们项目是由N个工程组成的,外围工程是web工程,内部的工程打包成jar,放入外围工程的WEB-INF/lib 内部的工程用到了spring的注解,例如@Service.@C ...

  3. luogu4168 [Violet]蒲公英

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  4. cf837d Round Subset

    设dp[i][j][k]表示前i个数中选j个并且因子含有k个2的能获得的最多的5的个数 则dp[i][j][k]=max(dp[i-1][j][k],dp[i-1][j-1][k-cnt2]+cnt5 ...

  5. hdu3709 Balanced Number 树形dp

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  6. 使用WaveOut API播放WAV音频文件(解决卡顿)

    虽然waveout已经过时,但是其api简单,有些时候也还是需要用到. 其实还是自己上msdn查阅相应api最靠谱,waveout也有提供暂停.设置音量等接口的,这里给个链接,需要的可以自己查找: h ...

  7. redis安装【三】

    目录介绍: 0.Windows下下载安装包: 下载地址: https://redis.io/ 1.上传到linux服务器 将文件上传到192.168.2.128主机的usr/local目录下: C:\ ...

  8. mybatis结合generator进行分页插件PluginAdapter开发

    使用org.mybatis.generator生成UserExample时,无法进行分页,使用下面这个类运行generator便可以生成分页相关的属性了 package org.mybatis.gen ...

  9. android手机rootROM下载地址

    https://download.mokeedev.com/ https://download.lineageos.org/

  10. 学习Android从青铜到王者之第一天

    1.Android四层架构 一.Linux Kernel 二.Libraries和Android Runtime 三.Application Framework 四.Applications 一.Li ...