浅谈并查集: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的更多相关文章

  1. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

  2. 【POJ1733】Parity game

    [POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...

  3. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  4. 【poj1733】Parity game--边带权并查集

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15776   Accepted: 5964 Description Now ...

  5. 【poj1733】 Parity game

    http://poj.org/problem?id=1733 (题目链接) 题意 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. Solution 并查集. 题目中序列的长度有很 ...

  6. Universal Asynchronous Receiver/Transmitter

    USART簡介與特性 NRZ標準資料格式(Mark/Space) 半雙工/全雙工 Synchronous 同步傳輸 CLOCK SKEW Asynchronous 非同步傳輸 半/全雙工.同步/非同步 ...

  7. the security of smart contract- 2

    出处:https://cloud.tencent.com/developer/article/1192548 深度解析Solidity的17个坑及超详细避坑指南 写的很好,好好学习 1. Re-Ent ...

  8. 比特币全节点(bitcoind) eth 全节点

    运行全节点的用途:  1.挖矿  2.钱包   运行全节点,可以做关于btc的任何事情,例如创建钱包地址.管理钱包地址.发送交易.查询全网的交易信息等等 选个节点钱包:bitcoind 1.配置文件: ...

  9. linux中serial driver理解【转】

    转自:http://blog.csdn.net/laoliu_lcl/article/details/39967225 英文文档地址:myandroid/kernel_imx/Documentatio ...

随机推荐

  1. Raspberry Pi开发之旅-土壤湿度检测

    一.土壤传感器 传感器四个针脚:  针脚 含义 AO 模拟信号输出 DO 数字信号输出 GND 电源负极 VCC 电源正极 二.接线 YL-38和YL69 之间直接用2根母对母线连接. YL-38和树 ...

  2. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  3. 如何在IAR中配置CRC参数(转)

    源:如何在IAR中配置CRC参数 前言 STM32全系列产品都具有CRC外设,对CRC的计算提供硬件支持,为应用程序节省了代码空间.CRC校验值可以用于数据传输中的数据正确性的验证,也可用于数据存储时 ...

  4. 模型融合之blending和stacking

    1. blending 需要得到各个模型结果集的权重,然后再线性组合. """Kaggle competition: Predicting a Biological Re ...

  5. Go goroutine (协程)

    在Go语言中goroutine是一个协程,但是跟Python里面的协程有很大的不同: 在任何函数前只需要加上go关键字就可以定义为协程; 不需要在定义时区分是否是异步函数  VS  async def ...

  6. codeforces Codeforces Round #318 div2 A. Bear and Elections 【优先队列】

    A. Bear and Elections time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Java I/O 小结

    主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...

  8. Struts2全局异常处理

    1.在struts.xml中配置全局异常处理 在Action中抛出异常,此异常可以是action自己抛的,也可以是Service抛出来的,都会跳转到全局异常中,只有在当前Action中配置域全局异常返 ...

  9. 轻松掌握XMLHttpRequest对象------【这是.net 版本】

    轻松掌握XMLHttpRequest对象 XmlHttp是什么? 最通用的定义为:XmlHttp是一套可以在Javascript.VbScript.Jscript等脚本语言中通过http协议传送或从接 ...

  10. <转载>获取运行中的TeamViewer的账号和密码

    #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <iostream> #pragma comment( li ...