51nod 1204 Parity(并查集应用)
1204 Parity
题目来源: Ural
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
第1行:2个数N, Q,N为串的长度,Q为询问的数量。(2 <= N <= 100000, 2 <= Q <= 50000)
第2 - Q + 1行:每行包括两个数以及一个字符串来描述朋友的回答,2个数中间用空格分隔,分别表示区间的起点和终点,后面的字符为"even"或"odd",表示朋友的答案。
输出1个数,表示朋友的答案中第一个错误答案的位置,如果所有答案均不矛盾,则输出-1。
10 5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
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(并查集应用)的更多相关文章
- 51nod 1515 明辨是非 [并查集+set]
今天cb巨巨突然拿题来问,感觉惊讶又开心,希望他早日康复!!坚持学acm!加油! 题目链接:51nod 1515 明辨是非 [并查集] 1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间 ...
- 51Nod 1515 明辨是非 —— 并查集 + 启发式合并
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非 题目来源: 原创 基准时间限制:1 ...
- 51nod 1515 明辨是非 并查集 + set + 启发式合并
给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...
- 51nod 1515 明辨是非 并查集+set维护相等与不等关系
考试时先拿vector瞎搞不等信息,又没离散化,结果好像MLE:后来想起课上讲过用set维护,就开始瞎搞迭代器...QWQ我太菜了.. 用并查集维护相等信息,用set记录不相等的信息: 如果要求变量不 ...
- 1003. Parity(并查集)
1003 看篇国家论文 <从<parity>的解法谈程序优化> 对于区间i,j 如果用sum[i],sum[j]来表示到i的1的个数的奇偶性 那么仔细想下 sum[i-1] 若 ...
- 51nod 1307 绳子与重物(并查集水了一发)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 思路: 可以直接二分答案,然后dfs. 因为标签是并查集, ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- POJ1733 Parity game 【扩展域并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
随机推荐
- 2017-2018-1 Java演绎法 第一周 作业
团队学习:<构建之法> [团队成员]: 学号 姓名 负责工作 20162315 马军 日常统计,项目部分代码 20162316 刘诚昊 项目部分代码,代码质量测试 20162317 袁逸灏 ...
- C语言总结报告
1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当初报考计算机专业,是看到计算机专业在当今社会有良 ...
- jQuery函数学习
函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...
- python 操作SQLAlchemy
SQLAlchemy python链接 pymysql mysql+pymysql://<username>:<password>@<host>/<dbnam ...
- 2018年东北农业大学春季校赛-wyh的吃鸡
BFS: 1. 从起点开始BFS,遇到X点则return: 2. vis[px][py][0]代表经过pxpy这点前还没有找到车: vis[px][py][1]代表经过pxpy这点前已经找到车: 3. ...
- SQL Server 实现递归查询
基础数据/表结构 Sql 语句 ;With cte(id,pid,TName)As ( Select id,pid,TName Union All Select B.i ...
- python 类的绑定方法和非绑定方法
一.绑定方法 1.对象的绑定方法 首先我们明确一个知识点,凡是类中的方法或函数,默认情况下都是绑定给对象使用的.下面,我们通过实例,来慢慢解析绑定方法的应用. class People: def __ ...
- Mego开发文档 - 处理并发冲突
处理并发冲突 数据库并发是指多个进程或用户同时访问或更改数据库中的相同数据的情况.并发控制是指用于确保存在并发更改时数据一致性的特定机制. Mego实现了乐观并发控制,这意味着它可以让多个进程或用户独 ...
- http缓存浅谈
我们在访问百度首页的时候,会发现不管怎么刷新页面,静态资源基本都是返回 200(from cache): 随便点开一个静态资源是酱的: 哎哟有Response报头数据呢,看来服务器也正常返回了etag ...
- keycloak管理用户权限
一.在keycloak中定义基础数据 1.realm 如果多个模块使用不同的用户权限,就分realm 如果多个模块共用一套用户权限,就顶一个一个realm 2.每个模块是一个client-app 3. ...