POJ1733:Parity game
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html
题目传送门:http://poj.org/problem?id=1733
带权并查集裸题。区间和可以由前缀和相减得到,如果区间\([l,r]\)为奇数,那么\(sum[l-1]\)与\(sum[r]\)奇偶性就不同,否则反之。
用并查集合并\(l-1\)与\(r\)的同时维护一个\(d\)数组,\(d[x]\)表示\(x\)与祖先的奇偶性是否相同,可以直接异或求得。
时间复杂度:\(O(\alpha{m})\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e4+5;
char s[5];
int n,m,cnt;
int l[maxn],r[maxn],opt[maxn];
int fa[maxn],d[maxn],tmp[maxn<<1];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int find(int x) {
if(fa[x]==x)return x;
int tmp=fa[x];
fa[x]=find(fa[x]);
d[x]^=d[tmp];return fa[x];
}
int main() {
n=read(),m=read();
for(int i=1;i<=m;i++) {
tmp[i]=l[i]=read()-1;
tmp[i+m]=r[i]=read();
scanf("%s",s+1);
if(s[1]=='e')opt[i]=0;
else opt[i]=1;
}
sort(tmp+1,tmp+(m<<1)+1);
cnt=unique(tmp+1,tmp+(m<<1)+1)-tmp-1;
for(int i=1;i<=m;i++) {
l[i]=lower_bound(tmp+1,tmp+cnt+1,l[i])-tmp;
r[i]=lower_bound(tmp+1,tmp+cnt+1,r[i])-tmp;
}
for(int i=1;i<=cnt;i++)fa[i]=i;
for(int i=1;i<=m;i++) {
int p=find(l[i]),q=find(r[i]);
if(p==q&&(d[l[i]]^d[r[i]])!=opt[i]) {
printf("%d\n",i-1);return 0;
}
else if(p!=q)fa[p]=q,d[p]=opt[i]^d[l[i]]^d[r[i]];
}
printf("%d\n",m);
return 0;
}
POJ1733:Parity game的更多相关文章
- POJ1733:Parity Game(离散化+带权并查集)
Parity Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12853 Accepted: 4957 题目链接 ...
- 【POJ1733】Parity game
[POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...
- 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--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- 【poj1733】 Parity game
http://poj.org/problem?id=1733 (题目链接) 题意 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. Solution 并查集. 题目中序列的长度有很 ...
- Universal Asynchronous Receiver/Transmitter
USART簡介與特性 NRZ標準資料格式(Mark/Space) 半雙工/全雙工 Synchronous 同步傳輸 CLOCK SKEW Asynchronous 非同步傳輸 半/全雙工.同步/非同步 ...
- the security of smart contract- 2
出处:https://cloud.tencent.com/developer/article/1192548 深度解析Solidity的17个坑及超详细避坑指南 写的很好,好好学习 1. Re-Ent ...
- 比特币全节点(bitcoind) eth 全节点
运行全节点的用途: 1.挖矿 2.钱包 运行全节点,可以做关于btc的任何事情,例如创建钱包地址.管理钱包地址.发送交易.查询全网的交易信息等等 选个节点钱包:bitcoind 1.配置文件: ...
- linux中serial driver理解【转】
转自:http://blog.csdn.net/laoliu_lcl/article/details/39967225 英文文档地址:myandroid/kernel_imx/Documentatio ...
随机推荐
- 【Tech】mac下svn和scp使用笔记
1.命令行从svn下载代码 mac本身自带svn,所以使用非常简单,在本地创建代码存放的文件夹,然后cd到该文件夹下,运行: svn checkout svn://ip地址/文件路径 . 然后出现要求 ...
- 【Head First Servlets and JSP】笔记3:Servlet的生命周期
1.servlet的存在就是要为客户服务.servlet的任务就是得到一个用户的请求,再发回一些响应. 请求可能很复杂,也可能很简单,例如,“为我的购物车结账”,这个请求携带了一些重要的数据,你必须知 ...
- iOS开发过程中常见错误问题及解决方案
错误原因:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed with exit co ...
- C语言中static的使用方法【转】
本文转自:http://blog.csdn.net/renren900207/article/details/21609649 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量 ...
- [Android]开源中国源码分析之二---DrawerLayout
从启动界面到主界面之后的效果如图所示,采用的是v4包下的DrawerLayout, activity_main.xml文件如下: <!-- A DrawerLayout is intended ...
- 【转载】有向图强连通分量的Tarjan算法
转载地址:https://www.byvoid.com/blog/scc-tarjan [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly conn ...
- 多校hdu5754(博弈)
©此题中在N×M的棋盘中从(1,1)走到(N,M)B先走G后走,谁先到(N,M)谁赢,走法分为4中分别是国际象棋中的国王,车,马,王后的发,在四种走法下谁能赢: 我们依次分析每一种棋子. ①王. 首先 ...
- MAC 系列 之XCode7.1 + HBuilder MUI 离线打包 ipa 上次application leader 问题:ERROR ITMS - 90032
90032 解决方法:
- lua闭包浅析及项目应用
lua函数与闭包: 原文地址:http://www.doc88.com/p-6681238341344.html 近日查阅关于lua的一些资料,找到了我能理解的关于lua函数与闭包的解析,我觉得这个程 ...
- Linux ls命令参数详解
-a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录. -l -- 长(long).列举目 ...