poj1733 Parity game[带权并查集or扩展域]
连通性判定问题。(具体参考lyd并查集专题该题的转化方法,反正我菜我没想出来)。转化后就是一个经典的并查集问题了。
带权:要求两点奇偶性不同,即连边权为1,否则为0,压缩路径时不断异或,可以通过0或1得到两点的关系。合并时解一个位运算的方程,可得一个根连向另一个根的权值,看code,就不细讲了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define ddbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
struct kishin_sagume{
int l,r,p;
}q[N];
int a[N<<],fa[N<<],d[N<<];
int L,n,m,x,y,fx,fy,flag,i;
inline int Get(int x){
if(fa[x]==x)return x;
int ret=Get(fa[x]);d[x]^=d[fa[x]];
return fa[x]=ret;
} int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
read(L),read(n);char s[];
for(i=;i<=n;++i){
read(q[i].l),read(q[i].r),a[++m]=--q[i].l,a[++m]=q[i].r;
scanf("%s",s);q[i].p=s[]=='o';
}
sort(a+,a+m+),m=unique(a+,a+m+)-a-;
for(i=;i<=m;++i)fa[i]=i;
for(i=;i<=n;++i){
x=lower_bound(a+,a+m+,q[i].l)-a,y=lower_bound(a+,a+m+,q[i].r)-a;
fx=Get(x),fy=Get(y);
if(fx^fy)fa[fx]=fy,d[fx]=q[i].p^d[x]^d[y];
else if(d[x]^d[y]^q[i].p)break;
}
return printf("%d\n",i-),;
}
扩展域:同奇偶时连奇数域和偶数域分别连边,否则交错,边的含义是可以产生关系,或者说可以推出。已知他是满足传递性的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define ddbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
struct matara_okina{
int l,r,p;
}q[N];
int a[N<<],fa[N<<];
int L,n,m,x,y,i;
inline int Get(int x){
return fa[x]^x?fa[x]=Get(fa[x]):x;
} int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
read(L),read(n);char s[];
for(i=;i<=n;++i){
read(q[i].l),read(q[i].r),a[++m]=--q[i].l,a[++m]=q[i].r;
scanf("%s",s);q[i].p=s[]=='o';
}
sort(a+,a+m+),m=unique(a+,a+m+)-a-;
for(i=;i<=(m<<);++i)fa[i]=i;
for(i=;i<=n;++i){
x=lower_bound(a+,a+m+,q[i].l)-a,y=lower_bound(a+,a+m+,q[i].r)-a;
if(q[i].p){
if(Get(x)==Get(y))break;
fa[Get(x)]=Get(y+m),fa[Get(x+m)]=Get(y);
}
else{
if(Get(x)==Get(y+m))break;
fa[Get(x)]=Get(y),fa[Get(x+m)]=Get(y+m);
}
}
return printf("%d\n",i-),;
}
poj1733 Parity game[带权并查集or扩展域]的更多相关文章
- POJ1733 Party game [带权并查集or扩展域并查集]
题目传送 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10870 Accepted: 4182 ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- POJ 1773 Parity game 带权并查集
分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- POJ1733 Parity game 【带权并查集】*
POJ1733 Parity game Description Now and then you play the following game with your friend. Your frie ...
- POJ1733:Parity Game(离散化+带权并查集)
Parity Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12853 Accepted: 4957 题目链接 ...
- Poj1733 Parity Game(带权并查集)
题面 Poj 题解 反正只要你判断是否满足区间的奇偶性,假设每一位要么是\(1\)要么是\(0\)好了. 假设有\(S\)的前缀和为\(sum[]\),则有: 若\(S[l...r]\)中有奇数个\( ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
随机推荐
- cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
----我的生活,我的点点滴滴!! 最后一个LabelBMFont了,字体图集LabelBMFont,LabelBMFont类是一个基于位图的字体图集.是一个包括全部你须要于坐标数据一起显示在屏幕上的 ...
- Spring学习九----------Bean的配置之Bean的定义及作用域的注解实现
© 版权声明:本文为博主原创文章,转载请注明出处 Spring Bean常用注解 @Component:通常注解,可用于任何Bean @Repository:通常用于注解DAO层,即持久层 @Serv ...
- 图片压缩CompressUtil解析
CompressUtil 流程图: CompressUtil 类 具体解释 public class CompressUtil { /** * 终于封装的压缩方法 * @param imgPath * ...
- A和B是好友,他们经常在空闲时间聊天,A的空闲时间为[a1 ,b1 ],[a2 ,b2 ]..[ap ,bp ]。B的空闲时间是[c1 +t,d1 +t]..[cq +t,dq +t],这里t为B的起床时间。这些时间包括了边界点。B的起床时间为[l,r]的一个时刻。若一个起床时间能使两人在任意时刻聊天,那么这个时间就是合适的,问有多少个合适的起床时间?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- diy文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- centOS7 安装nginx+php+mysql
nginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automa ...
- iOS开发 两个内存错误的一般处理方法
本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxlj.html 由于iOS5.0之前没有自动应用计数机制,也没有Java那样的垃圾回收功能.我们都 ...
- PHP部分--图片上传服务器、图片路径存入数据库,并读取
html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- hdu 4667 Building Fence < 计算几何模板>
//大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <string ...
- mysql insert返回主键
使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...