1204 Parity

题目来源: Ural

基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题

 
你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾。给出所有你朋友的答案,请你找出第一个出现矛盾的答案。

 
Input
第1行:2个数N, Q,N为串的长度,Q为询问的数量。(2 <= N <= 100000, 2 <= Q <= 50000)
第2 - Q + 1行:每行包括两个数以及一个字符串来描述朋友的回答,2个数中间用空格分隔,分别表示区间的起点和终点,后面的字符为"even"或"odd",表示朋友的答案。
Output
输出1个数,表示朋友的答案中第一个错误答案的位置,如果所有答案均不矛盾,则输出-1。
Input示例
10 5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Output示例
4
/*
51nod 1204 Parity(并查集应用) problem:
你可以从中选择一个连续的01子串,然后是q个询问和回答. 表示区间[l,r]中有奇数或者偶数个1. 求第一个出现矛盾的位置 solve:
考虑了下线段树什么的,发现并不怎么靠谱. 没做过类似的,完全没想到并查集,卒.....
如果[l,r]是even,那么[1,l-1]和[1,r]中1的个数都是偶数 or 奇数.
如果[l,r]是odd,那么 [1,l-1] 和 [1,r]的奇偶性不同.
就转换成了判断前缀和的问题
所以用[1,n]表示'相同'关系,[n+1,2*n]虚拟表示'不同'关系.建立两个集合
判断 (l-1+n,r) && (l-1,b+n) 就能确定 l-1和r是否为'相同'关系. 其它同理 hhh-2016/09/06-20:47:14
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define scanfd(a) scanf("%lf",&a)
#define key_val ch[ch[root][1]][0]
#define eps 1e-7
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const ll mod = 1000000007;
const int maxn = 300050;
const double PI = acos(-1.0);
const int limit = 33;
int pre[maxn];
template<class T> void read(T&num)
{
char CH;
bool F=false;
for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
if(!p)
{
puts("0");
return;
}
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} int fin(int x)
{
if(pre[x ]== x) return x;
return pre[x] = fin(pre[x]);
} void unio(int a,int b)
{
int ta= fin(a);
int tb = fin(b);
pre[ta] = tb;
}
char op[10]; int main()
{
// freopen("in.txt","r",stdin);
int n,q,flag = 0;
int a,b;
read(n);
read(q);
for(int i = 0;i <= 2*n;i++)
pre[i] = i;
for(int i = 1; i <= q; i++)
{
scanf("%d%d%s",&a,&b,op);
if(flag)
continue;
if(op[0] == 'e')
{
if(fin(a-1+n) == fin(b) && fin(a-1) == fin(b + n))
{
flag = i;
}
unio(a-1,b);
unio(a-1+n,b+n);
}
else
{
if(fin(a-1) == fin(b) && fin(a-1+n) == fin(b+n))
{
flag = i;
}
unio(a-1+n,b);
unio(a-1,b+n);
}
}
if(flag)
printf("%d\n",flag);
else
printf("-1\n");
return 0;
}

  

51nod 1204 Parity(并查集应用)的更多相关文章

  1. 51nod 1515 明辨是非 [并查集+set]

    今天cb巨巨突然拿题来问,感觉惊讶又开心,希望他早日康复!!坚持学acm!加油! 题目链接:51nod 1515 明辨是非 [并查集] 1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间 ...

  2. 51Nod 1515 明辨是非 —— 并查集 + 启发式合并

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非  题目来源: 原创 基准时间限制:1 ...

  3. 51nod 1515 明辨是非 并查集 + set + 启发式合并

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

  4. 51nod 1515 明辨是非 并查集+set维护相等与不等关系

    考试时先拿vector瞎搞不等信息,又没离散化,结果好像MLE:后来想起课上讲过用set维护,就开始瞎搞迭代器...QWQ我太菜了.. 用并查集维护相等信息,用set记录不相等的信息: 如果要求变量不 ...

  5. 1003. Parity(并查集)

    1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...

  6. 51nod 1307 绳子与重物(并查集水了一发)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 思路: 可以直接二分答案,然后dfs. 因为标签是并查集, ...

  7. URAL - 1003:Parity (带权并查集&2-sat)

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

  8. POJ1733 Parity game 【扩展域并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

  9. POJ1733 Parity game 【带权并查集】*

    POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...

随机推荐

  1. (转)如何在Eclipse中查看JDK类库的源代码

    在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...

  2. 团队作业4——第一次项目冲刺(Alpha版本)11.14

    a. 提供当天站立式会议照片一张 举行站立式会议,讨论项目安排: PM对整个项目的需求进行讲解: 全队对整个项目的细节进行沟通: 对整个项目的开发计划进行分析,分配每天的任务: 统一确定项目的开发环境 ...

  3. android批量打包

    http://blog.csdn.net/johnny901114/article/details/48714849

  4. bzoj 4399 魔法少女LJJ

    4399: 魔法少女LJJ Time Limit: 20 Sec  Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/problem.php?i ...

  5. 【nodejs】安装browser-sync 遇到错误提示

    首先我用的是mac电脑在我执行安装browser-sync时遇到如下问题: 因为不被允许所以我只能不安装全局了: 但是又出现了如下的新问题 纠结了半个小时,终于知道为什么会出现这个问题了, node只 ...

  6. 微信支付 chooseWXPay:fail

    本来以为解决了微信支付get_brand_wcpay_request:faill这个问题后就万事大吉了,结果又迈入了另一个坑... 问题原因: 1.生成签名的时间戳参数名timestamp的s大小写问 ...

  7. python使用tesseract-ocr完成验证码识别(安装部分)

    一.tesseract-ocr安装 Ubuntu版本: 1.tesseract-ocr安装 sudo apt-get install tesseract-ocr 2.pytesseract安装 sud ...

  8. Mego(03) - ORM框架的新选择

    前言 从之前的两遍文章可以看出ORM的现状. Mego(01) - NET中主流ORM框架性能对比 Mego(02) - NET主流ORM框架分析 首先我们先谈下一个我们希望的ORM框架是什么样子的: ...

  9. Python-字符串及列表操作-Day2

    1.数据类型 1.1 变量引出数据类型 变量:用来记录状态变量值的变化就是状态的变化,程序运行的本质就是来处理一系列的变化 1.2 五大基本数据类型: 数字 字符串 列表 元组 字典 1.2.1 数字 ...

  10. SpringBoot单元测试中的测试方法执行顺序

    一.忽略方法@ignore 二.执行顺序@FixMethodOrder(MethodSorter.JVM) 我们在执行JUnit测试用例时,有时需要按照定义顺序执行单元测试方法,比如如在测试数据库相关 ...