POJ1733
题目链接:https://vjudge.net/problem/POJ-1733
解题思路:并查集+离散化
AC代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=;
struct query{
int l,r;
int oe;
}q[maxn];
map<int,int> lists;
int v[maxn],fa[maxn];
int finds(int t){
if(fa[t]==-)
return t;
int tmp=finds(fa[t]);
v[t]^=v[fa[t]];
return fa[t]=tmp;
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int vals[maxn<<];
char ooe[];
int ques,len;
memset(fa,-,sizeof(fa));
memset(v,,sizeof(v));
scanf("%d",&len);
scanf("%d",&ques);
int j=;
for(int i=;i<=ques;i++){
scanf("%d%d%s",&q[i].l,&q[i].r,ooe);
q[i].l-=;
if(ooe[]=='e') q[i].oe=;
else q[i].oe=;
vals[j]=q[i].l; j++;
vals[j]=q[i].r; j++;
}
sort(vals+,vals+j);
int m=unique(vals+,vals+j)-vals-;
for(int i=;i<=m;i++){
lists[vals[i]]=i;
}
int ending=;
for(int i=;i<=ques;i++){
int t1=finds(lists[q[i].l]),t2=finds(lists[q[i].r]);
int vl=v[lists[q[i].l]],vr=v[lists[q[i].r]];
if(t1==t2){
if(vr^vl!=q[i].oe){
printf("%d\n",i-);
ending=;
break;
}
}
else{
fa[t2]=t1;
v[t2]=q[i].oe^vl^vr;
}
}
if(!ending) printf("%d\n",ques);
return ;
}
额,先这样吧,晚上或者明天再来详谈。
POJ1733的更多相关文章
- 【poj1733】 Parity game
http://poj.org/problem?id=1733 (题目链接) 题意 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. Solution 并查集. 题目中序列的长度有很 ...
- 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)
并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构, 可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...
- 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 ...
- 【POJ1733】Parity game
[POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
- poj1733 带权并查集
题意:有一个 0/1 数列,现在有n组询问和回答,表示某个区间内有奇数或者偶数个1,问到前多少个都没有逻辑错误,而下一个就不满足 可以定奇数为 1 偶数为 0作为每个元素的权值,表示它与它的祖先元素的 ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- poj1733(并查集+离散化)
题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...
随机推荐
- Scala教程之:PartialFunction
Scala中有一个很有用的traits叫PartialFunction,我看了下别人的翻译叫做偏函数,但是我觉得部分函数更加确切. 那么PartialFunction是做什么用的呢?简单点说Parti ...
- 【Linux常见命令】cut命令
cut - remove sections from each line of files 参数: -b 可以按字节来查看文件中的内容 -b参数用在中文上,容易出现乱码问题.因为中文字符一个字符占两个 ...
- F查询,Q查询,事物,only与defer
F查询 之前的单表查询多表查询筛选条件都是一个固定的值,那么如何用字段来筛选呢? 比如 : 查询卖出数大于库存数的商品 这个时候我们就可以用到django里面的F查询了 查询示例表 ...
- 2019 ICPC 南京网络赛 H-Holy Grail
As the current heir of a wizarding family with a long history,unfortunately, you find yourself force ...
- UVA-2【NOI2014】起床困难综合症
#2. [NOI2014]起床困难综合症 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过 ...
- python-函数变量与方法公有、私有整理
关于函数与方法的区别: 函数: def funname(): 括号里面可以有形参,也可以没有,为空 在函数里面的参数整理: 方法: def funcname(self): 括号里面必须有个self,因 ...
- undef用法
#undef的语法 定义:#undef 标识符,用来将前面定义的宏标识符取消定义. 整理了如下几种#undef的常见用法. 1. 防止宏定义冲突在一个程序块中用完宏定义后,为防止后面标识符冲突需要取消 ...
- shell之路 Linux核心命令【第一篇】管道符与重定向
输出重定向 命令输出重定向的语法为: command > file 或 command >> file 这样,输出到显示器的内容就可以被重定向到文件.果不希望文件内容被覆盖,可以使用 ...
- Programming Languages_04 Deferred Substitution
Deferred Substitution 在执行出现with时,利用"substitution",每次with的出现,它都绕着整个body置换.这一方式是由F1WAE到env再到 ...
- IoTClientTool自动升级更新
IoTClientTool是什么 IoTClientTool是什么,IoTClientTool是IoTClient开源组件的可视化操的作实现.方便对plc设备和ModBusRtu.BACnet.串口等 ...