总时间限制: 10000ms
单个测试点时间限制: 1000ms
内存限制: 65536kB

描述
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.

输入
The first line of input file PARITY.IN 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' orodd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even' means an even number of ones andodd’ means an odd number).

输出
There is only one line in output file PARITY.OUT 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.

样例输入
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
样例输出
3

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;int parent[10005];
int rel[10005]; /* relation to the parent */
map<int, int> pos2idx;
vector<int> pos;
int length;
int N; /* number of answers */
int M; /* number of indexs */
int l[5005], r[5005];
bool v[5005];
char buf[5];int getroot(int cur)
{if (cur == parent[cur])return cur;int root = getroot(parent[cur]);rel[cur] = rel[parent[cur]] ^ rel[cur];parent[cur] = root;return root;
}/*** @return -1 if fail, 0 if normal*/
int merge(int i, int j, bool r_i_j)
{int root_i = getroot(i);int root_j = getroot(j);if (root_i == root_j) {/* check */if (r_i_j != (rel[i] ^ rel[j]))return -1;}else {/* merge */parent[root_i] = root_j; /* Attention: not parent[i] = j; !!! */rel[root_i] = rel[i] ^ rel[j] ^ r_i_j;}return 0;
}int main()
{cin >> length >> N;for (int i = 0; i < N; ++i) {scanf("%d %d %s", &l[i], &r[i], buf);++r[i];v[i] = buf[0] == 'o';pos.push_back(l[i]);pos.push_back(r[i]);}sort(pos.begin(), pos.end());auto it = unique(pos.begin(), pos.end());pos.erase(it, pos.end());M = pos.size();for (int i = 0; i < M; ++i) {pos2idx[pos[i]] = i;parent[i] = i;}int i;for (i = 0; i < N; ++i) {if (merge(pos2idx[l[i]], pos2idx[r[i]], v[i]) == -1)break;}printf("%d\n", i);system("pause");return 0;
}

Parity Game(并查集)相关推荐

  1. P5937 [CEOI1999]Parity Game-扩展域并查集与离散化处理

    题目链接[CEOI1999]Parity Game - 洛谷 单调队列优化多重背包(全网最详细解析)_秦三马和他的CF生涯的博客-CSDN博客 考察内容,扩展域并查集,本题中把奇偶性相同归为一个集合, ...

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

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

  3. Ural_1003 Parity(并查集)

    /*发现并查集的应用太巧妙了...大体思路:从题中可以看出来如果(i, j)是even的话,sum(i-1) 和 sum(j)的奇偶性相同.(i, j)如果是odd的话则其奇偶行不同.定义奇偶性为朋友 ...

  4. 并查集——奇偶性(Parity)

    题目描述 • 有一个 01 序列 , 长度 <=1000000000, 现在有 n 条 信息 , 每条信息的形式是- a b even/odd .表示 第 a 位到第 b 位元素之间的元素总和是 ...

  5. Parity(带权值的并查集)

    你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案 ...

  6. POJ 并查集 题目汇总 ——czyuan原创(转)

    继续数据结构的复习,本次的专题是:并查集. 并查集,顾名思义,干的就是"并"和"查"两件事.很多与集合相关的操作都可以用并查集高效的解决. 两个操作代码:    ...

  7. 【无码专区9】序列统计(带权并查集 + 前缀和建边 + dp)

    因为只有std,没有自我实现,所以是无码专区 主要是为了训练思维能力 solution才是dls正解,但是因为只有潦草几句,所以大部分会有我自己基于正解上面的算法实现过程,可能选择的算法跟std中dl ...

  8. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  9. [kuangbin带你飞]专题五 并查集 题解+总结

    kuangbin带你飞:点击进入新世界 总结: 本人算是初学者中的初学者,欢迎交流~ 并查集的接触过的不多,大概只有普通并查集,带权并查集,种族并查集,传说中的可持续化并查集只是听说过还没有接触,不过 ...

  10. 我知道并查集的可爱之处

    请开始您的并查集之旅 迈入门槛 文字概念 故事配图辅助理解 浅尝辄止 畅通工程 程序自动分析 supermarket 慢慢深入 银河英雄传说 parity game 1.带权并查集 2.扩展域并查集 ...

最新文章

  1. Linux笔记16.磁盘管理
  2. 猜数字游戏python程序_python实现的简单猜数字游戏
  3. locate用主动还是被动_用英文形容地理位置lie、sit、locate、situate 怎样区别?
  4. 对话机器人70年:科幻与现实的交融
  5. JAVA中向量类Vector
  6. python3档案管理程序源码_php人事档案管理系统,源码免费分享
  7. sql server中case的简单示例
  8. oauth2基本概念
  9. c语言空白不占位置的符号,不占位置的符号_空白代码不占位置
  10. 计算机用户名起什么好,如何随机取名计算机名-如何改计算机用户名
  11. has leaked IntentReceiver ...that was originally registerd here.Are you missing a call to unregister
  12. 基于WFP的windows驱动对TCP数据的抓取,修改以及注意事项
  13. 笔记本电脑不显示WIFI列表无法连接到网络的解决办法
  14. python处理adb截屏_《自拍教程38》Python_adb一键截屏
  15. 搭积木的诀窍(数学题)
  16. 小学生计算机课程的简报,《让故事动起来》——信息技术公开课简报
  17. delphi 控件的安装
  18. 05. JavaMail 回复邮件
  19. [Luogu P4228] [LOJ 2330] 榕树之心
  20. Python解释器整数运算向下圆整问题

热门文章

  1. 分享一个好东西(一天精通MongoDB数据库)
  2. 2005年中兴软件校园招聘笔试题
  3. 【基本操作】RouterOS-安装和使用RouterOS(想要搭简易DHCP服务器和PPPoE服务器的看过来)
  4. AMI BIOS开机LOGO添加分离过程
  5. 非线性系统【二】Lyapunov稳定性
  6. java并发编程实战wwj----------------------第二阶段-------不可变对象-------19-20
  7. Java实现License许可证控制(详细过程)
  8. 【问链-EOS公开课】第十一课 EOS 智能合约相互调用
  9. LTDC-DMA2D液晶显示 代码详解(二)
  10. Word里面如何修图