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 ...
随机推荐
- 九度oj-题目1103:二次方程计算器
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2799 解决:633 题目描述: 设计一个二次方程计算器 输入: 每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式. 输 ...
- 10个超有趣的linux命令
本文展示了 10 个有趣的 Linux 动态命令,这些命令和实用功能无关,仅供娱乐!看完此文,你会对 Linux 有个全新的认识,谁说 IT 男就没有屌丝娱乐的一面呢?还等什么,就让我们开始看文章吧~ ...
- Optional int parameter 'time' is present but cannot be translated into a null value due to being decla
今天在操作redis的时候报了这个错:Optional int parameter 'time' is present but cannot be translated into a null val ...
- java基础(2)-面向对象(2)
构造方法 构造方法特点 方法名与类名相同 方法名前没有返回值类型的声明(void也没有) 方法中不能使用return语句返回一个值 创建对象时自动调用并执行 如果类中没有自定义构造方法,则java调用 ...
- Apache Phoenix数据类型
数据类型 Java Map 占用大小 (byte) 范围 INTEGER java.lang.Integer 4 -2147483648 to 2147483647 UNSIGNED_INT java ...
- 【转】Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...
- Mysql -- SQL常用命令实例
sql: structured query language(结构化查询语言) 用户名和密码:root 创建一个名称为mydb1的数据库. create database mydb1; 查看所有数据库 ...
- numpy加权平均
import numpy as np a = np.arange(15).reshape(3,5) a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], ...
- libvirt/qemu特性之numa
博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! Numa 查看主机node情况 使用virsh命令查看 virsh # c ...
- 读写properties文件方法
按key读取properties文件中的value public static String readSystemConfig(String key){ Properties prop = new P ...