【poj1733】 Parity game
http://poj.org/problem?id=1733 (题目链接)
题意
一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法。
Solution
并查集。
题目中序列的长度有很大,单纯搜索一定会TLE。
我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a-1]的奇偶性相同。a b odd意味着s[b]与s[a-1]的奇偶性不同。于是我们根据奇偶性的不同,用并查集依次处理他们之间的关系。当某条信息出现与并查集中记录的信息不符合时,则此信息不合法。
由于a和b的值可能会非常大,所以我们还需要将它离散化。
代码
// poj1733
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi 3.1415926535898
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; int n,m,p[1000010],fa[1000010],s[1000010],x[1000010],y[1000010],pos[1000010],cnt;
char ch[10]; int binsearch(int x) {
int tl=1,tr=cnt;
while (tl<=tr) {
int m=(tl+tr)>>1;
if (x==pos[m]) return m;
if (x>pos[m]) tl=m+1;
else tr=m-1;
}
}
int find(int x) {
if (fa[x]==x) return x;
int f=find(fa[x]);
s[x]=(s[fa[x]]+s[x])&1;
fa[x]=f;
return fa[x];
}
void unnion(int i) {
s[fa[x[i]]]=(s[x[i]]+p[i])&1;
fa[fa[x[i]]]=y[i];
}
int main() {
scanf("%d%d",&m,&n);
for (int i=1;i<=n;i++) {
scanf("%d%d",&x[i],&y[i]);
scanf("%s",ch);
if (ch[0]=='o') p[i]=1;
x[i]--;
pos[++cnt]=x[i];
pos[++cnt]=y[i];
}
sort(pos+1,pos+1+cnt);
pos[0]=1;
for (int i=2;i<=cnt;i++) if (pos[i]!=pos[pos[0]]) {pos[0]++;pos[pos[0]]=pos[i];}
cnt=pos[0];
for (int i=1;i<=cnt;i++) fa[i]=i;
for (int i=1;i<=n;i++) {
x[i]=binsearch(x[i]);
y[i]=binsearch(y[i]);
if (find(x[i])==find(y[i])) {if (((s[x[i]]+s[y[i]])&1)!=p[i]) {printf("%d",i-1);return 0;}}
else unnion(i);
}
printf("%d",n);
return 0;
}
【poj1733】 Parity game的更多相关文章
- 【POJ1733】Parity game
[POJ1733]Parity game 题面 vjudge 题解 比较简单的分类并查集 将一个查询操作看作前缀和\(s_r-s_{l-1}\)的奇偶性 将每个点拆成一奇一偶然后分别连边即可 如果一个 ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 【Codeforces Global Round 1 A】Parity
[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] im ...
- 【POJ 1733】 Parity Game
[题目链接] http://poj.org/problem?id=1 [算法] 并查集 [代码] #include <algorithm> #include <bitset> ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【C#】串口操作实用类
做工业通 信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但 是目前却没有C#相对成熟的串口操作代码,最近 ...
随机推荐
- android小游戏 飞机躲子弹
最近android老师让每人写一个小东西,因为之前学awt时写过一个java版的飞机躲子弹,所以这次想写成android版的. 文件直接导入就行http://files.cnblogs.com/fil ...
- ASP.NET整理:Cookie,Application,Session,页面生命周期
一.设置Cookie的2种方式 1. Repsonse.Cookie[“名”] = 值; 2. HttpCookie hcCookie = new HttpCookie(“名”,值); h ...
- CoreBluetooth——IOS蓝牙4.0使用心得
原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...
- 字符串匹配(hash算法)
hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...
- [BZOJ1264][AHOI2006]Match(DP+树状数组)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...
- Matlab学习笔记 figure函数
Matlab学习笔记 figure函数 matlab中的 figure 命令,能够创建一个用来显示图形输出的一个窗口对象.每一个这样的窗口都有一些属性,例如窗口的尺寸.位置,等等.下面一一介绍它们. ...
- 取消Git代理设置
昨天由于在用sourceTree上传下拉代码的时候,速度实在太慢,就照着百度上的方法设置了代理,结果导致sourceTree无法访问服务器,经检查排除发现可能是因为公司网络不能使用代理,被防火墙挡住了 ...
- 68 id -显示用户的id
Linux id命令用于显示用户的ID,以及所属群组的ID. id会显示用户以及所属群组的实际与有效ID.若两个ID相同,则仅显示实际ID.若仅指定用户名称,则显示目前用户的ID. 语法 id [-g ...
- display~
- openvpn的部署
http://www.gaohuirong.cn/openvpn/2016/04/12/linux-install-openvpn.html