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 ...
随机推荐
- Python编程-架构、Socket
一.客户端/服务器架构 1.C/S架构 Client/Server架构,即服务器/客户端架构. 客户端和服务器端的程序不同,用户的程序主要在客户端,服务器端主要提供数据管理.数据共享.数据及系统维护和 ...
- ==、equals与hashCode
== 首先,得说明java数据类型分为基本数据类型和引用数据类型, 基本数据类型有8种: 浮点型:float(4 byte), double(8 byte) 整型:byte(1 byte), sho ...
- css异步加载
<link rel="preload" href="mystyles.css" as="style" onload="thi ...
- 获取本地IP并设置到QLineEdit中
#include <QHostAddress> #include <QNetworkInterface> #include <QHostInfo> QString ...
- phpMyAdmin中config.inc.php设置密码和修改密码的方法
phpMyAdmin有3种授权模式: 1. cookie: 显示一个web登录页面,输入mysql的用户名和密码,然后进入管理界面. $cfg['Servers'][$i]['auth_type'] ...
- Spark总结1
安装jdk 下载spark安装包 解压 重点来了: 配置 spark: 进入 conf ----> spark-env.sh.template文件 cd conf/ mv spark-env ...
- java深入探究12-框架之Structs
注意每次修改配置文件后必须项目重启 Structs2=structs1+xwork Struct2框架预先实现了一些功能: 1.请求数据的封装:2.文件上传的功能3.对国际化功能的简化4.文件效验功能 ...
- 【转】Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...
- XPath 文档 解析XMl
http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html 简介 XPath由W3C的 XPath 1.0 标准描述.本教程通过实例 ...
- ajax设置Access-Control-Allow-Origin实现跨域访问
ajax跨域访问 1.jsonp方法,jsonp方法是一种非官方方法,这种方法只支持GET方式, 不如POST方式安全.(即使使用jquery的jsonp方法,type设为POST, 也会自动变为GE ...