POJ 1733 Parity game (并查集)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6816 | Accepted: 2636 |
Description
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
Output
Sample Input
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3 和上一题很类似,但是这题给的范围太大,数组开不下,所以直接用map映射一下,还有个性质就是 奇数+奇数=偶数 奇数+偶数=奇数 偶数+偶数=偶数。
括号打偏了WA了一发,囧
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; map<int,int> ROOT;
map<int,int> SUM;
int N,M,ANS;
bool FLAG; int find_father(int);
void unite(int,int,int);
int main(void)
{
int x,y,w;
char box[]; while(scanf("%d%d",&N,&M) != EOF)
{
ROOT.clear();
SUM.clear();
FLAG = false;
for(int i = ;i <= M;i ++)
{
scanf("%d%d%s",&x,&y,box);
y ++;
if(FLAG)
continue;
w = box[] == 'e' ? : ;
unite(x,y,w);
if(FLAG)
ANS = i - ;
}
if(!FLAG)
ANS = M;
printf("%d\n",ANS);
} return ;
} int find_father(int n)
{
if(!ROOT[n])
return n;
int temp = ROOT[n];
ROOT[n] = find_father(ROOT[n]);
SUM[n] = (SUM[n] + SUM[temp]) % ;
return ROOT[n];
} void unite(int x,int y,int w)
{
int fx = find_father(x);
int fy = find_father(y); if(fx == fy)
{
if((SUM[x] + - SUM[y]) % != w)
FLAG = true;
return ;
}
if(fx < fy)
{
ROOT[fx] = fy;
SUM[fx] = (-SUM[x] + w + SUM[y]) % ;
}
else
{
ROOT[fy] = fx;
SUM[fy] = (-SUM[y] - w + SUM[x]) % ;
}
}
POJ 1733 Parity game (并查集)的更多相关文章
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- poj 1733 Parity game(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...
- POJ 1733 Parity game(种类并查集)
http://poj.org/problem?id=1733 题意: 给出一个01串,有多次询问,每次回答[l,r]这个区间内1的个数的奇偶性,但是其中有一些回答是错误的,问到第几个回答时与前面的回答 ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- POJ - 1733 Parity game 种类并查集+离散化
思路:d(i, j)表示区间(i, j]的1的个数的奇偶性.输入最多共有5000*2个点,需要离散化处理一下.剩下的就是并查集判冲突. AC代码 #include <cstdio> #in ...
- POJ 1733 Parity game 【带权并查集】+【离散化】
<题目链接> 题目大意: 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. 解题分析: 我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a- ...
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
随机推荐
- installshield Basic 工程每次安装完提示重启电脑
将Sequence中的ScheduleReboot Action的Condition清空即可.
- thinkphp 3+ 观后详解 (4)
static public function run() { // 应用初始化标签 Hook::listen('app_init'); App::init(); // 应用开始标签 Hook::lis ...
- Bit Twiddling Hacks
http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks By Sean Eron Andersonseander ...
- PostgreSQL中 AnyElement AnyArray AnynonArray的区别与联系
http://www.postgresql.org/docs/current/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC 先看一个例 ...
- C# 程序员最常犯的 10 个错误
关于C# C#是达成微软公共语言运行库(CLR)的少数语言中的一种.达成CLR的语言可以受益于其带来的特性,如跨语言集成.异常处理.安全性增强.部件组合的简易模型以及调试和分析服务.作为现代的CLR语 ...
- 【JavaScript】关于JS中的constructor与prototype
最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...
- 【JavsScript】推荐五款流行的JavaScript模板引擎
摘要:Javascript模板引擎作为数据与界面分离工作中最重要一环,受到开发者广泛关注.本文通过开发实例解析五款流行模板引擎:Mustache.Underscore Templates.Embedd ...
- [CSS] Introduction to CSS Columns
Learn how to use CSS columns to quickly lay out fluid columns that are responsive, degrade gracefull ...
- iframe自适应高度的多种方法小结
转自:http://www.jb51.net/article/15780.htm 不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的大小 ...
- C++ Socket超时设置
用winsocket时,send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,可以设置收发时限:int nNetTimeout = 1000; //1秒//发送时限setsocko ...