【bzoj4998】星球联盟 LCT+并查集
题目描述
输入
输出
样例输入
5 3 4
1 2
4 3
4 5
2 3
1 3
4 5
2 4
样例输出
No
3
2
5
题解
LCT+并查集,【bzoj2959】长跑 的简化版。
由于只有加边没有删边,因此可以使用LCT维护连通关系,如果加入的一条边属于同一个连通块内,那么将他们之间的点缩成一个点。使用并查集维护连通关系和属于的点。
注意每次找fa时都需要find一遍,以找到真正的fa。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200010
using namespace std;
int bl[N] , fa[N] , c[2][N] , w[N] , rev[N] , con[N];
int find(int x)
{
return x == bl[x] ? x : bl[x] = find(bl[x]);
}
int getcon(int x)
{
return x == con[x] ? x : con[x] = getcon(con[x]);
}
void pushdown(int x)
{
if(rev[x])
{
int l = c[0][x] , r = c[1][x];
swap(c[0][l] , c[1][l]) , swap(c[0][r] , c[1][r]);
rev[l] ^= 1 , rev[r] ^= 1 , rev[x] = 0;
}
}
bool isroot(int x)
{
return c[0][find(fa[x])] != x && c[1][find(fa[x])] != x;
}
void update(int x)
{
if(!isroot(x)) update(find(fa[x]));
pushdown(x);
}
void rotate(int x)
{
int y = find(fa[x]) , z = find(fa[y]) , l = (c[1][y] == x) , r = l ^ 1;
if(!isroot(y)) c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
}
void splay(int x)
{
int y , z;
update(x);
while(!isroot(x))
{
y = find(fa[x]) , z = find(fa[y]);
if(!isroot(y))
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x) splay(x) , c[1][x] = t , t = x , x = find(fa[x]);
}
void makeroot(int x)
{
access(x) , splay(x) , swap(c[0][x] , c[1][x]) , rev[x] ^= 1;
}
void link(int x , int y)
{
makeroot(x) , fa[x] = y , con[getcon(x)] = getcon(y);
}
void dfs(int x , int y)
{
if(!x) return;
bl[x] = y , w[y] += w[x];
dfs(c[0][x] , y) , dfs(c[1][x] , y);
}
int add(int x , int y)
{
x = find(x) , y = find(y);
if(x == y) return w[x];
else if(getcon(x) != getcon(y))
{
link(x , y);
return -1;
}
makeroot(x) , access(y) , splay(y);
dfs(c[0][y] , y);
return w[y];
}
int main()
{
int n , m , p , i , x , y , t;
scanf("%d%d%d" , &n , &m , &p);
for(i = 1 ; i <= n ; i ++ ) bl[i] = con[i] = i , w[i] = 1;
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y);
for(i = 1 ; i <= p ; i ++ )
{
scanf("%d%d" , &x , &y) , t = add(x , y);
if(~t) printf("%d\n" , t);
else puts("No");
}
return 0;
}
【bzoj4998】星球联盟 LCT+并查集的更多相关文章
- BZOJ4998星球联盟——LCT+并查集(LCT动态维护边双连通分量)
题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成 联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两 ...
- bzoj4998 星球联盟 LCT + 并查集
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4998 题解 根据题意,就是要动态维护点双,求出一个点双的权值和. 所以这道题就是和 bzoj2 ...
- 【bzoj4998】星球联盟(并查集+边双)
题面 传送门 题解 总算有自己的\(bzoj\)账号啦! 话说这题好像\(Scape\)去年暑假就讲过--然而我到现在才会-- \(LCT\)什么的跑得太慢了而且我也不会,所以这里是一个并查集的做法 ...
- bzoj4998 星球联盟
bzoj4998 星球联盟 原题链接 题解 先按照输入顺序建一棵树(森林),然后用一个并查集维护联盟的关系,对于不是树上的边\(a-b\),就把\(a-lca(a,b),b-lca(a,b)\)全部合 ...
- 【bzoj2959】长跑 LCT+并查集
题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...
- BZOJ4998 星球联盟(LCT+双连通分量+并查集)
即要求动态维护边双.出现环时将路径上的点合并即可.LCT维护.具体地,加边成环时makeroot+access+splay一套把这段路径提出来,暴力dfs修改并查集祖先,并将这部分与根断开,视为删除这 ...
- 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集
两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...
- 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测(lct/并查集)
http://www.lydsy.com/JudgeOnline/problem.php?id=2049 bzoj挂了..在wikioi提交,,1A-写lct的速度越来越快了-都不用debug-- 新 ...
- BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2049 给出一个森林,起始互不相连,现在有link和cut两种操作,问x,y是否在一棵树里. 分 ...
随机推荐
- EF core Code First 简单的使用方法
好吧,我又回来了,其实一直都想写一篇关于EF core 的文章去记录自己在开发时候遇到的问题. 为什么要使用EF框架呢,因为原始的ADO.NET需要编写大量的数据访问代码,所以使用EF会更方便.但是今 ...
- HTML5读取input[type=file]中的图片
转载 https://blog.csdn.net/fd214333890/article/details/71250488
- Percona-Tookit工具包之pt-ioprofile
Preface As a matter of fact,disk IO is the most important factor which tremendously influenc ...
- wampserver 服务器报500错误,侦察小结
Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...
- elasticsearch 5.x 系列之三 mapping 映射的时候的各个字段的设置
首先看来创建一个mapping 来show show: curl -XPUT "master:9200/zebra_info?pretty" -H 'Content-Type: a ...
- Kubernetes-深入分析集群安全机制
Kubernetes过一系列机制来实现集群的安全机制,包括API Server的认证授权.准入控制机制及保护敏感信息的Secret机制等.集群的安全性必须考虑以下的几个目标: 保证容器与其所在宿主机的 ...
- 前端面试题目汇总摘录(JS 基础篇 —— 2018.11.02更新)
温故而知新,保持空杯心态 JS 基础 JavaScript 的 typeof 返回那些数据类型 object number function boolean undefined string type ...
- 【转】mui 通过JSON动态的生成列表
<script type="text/template" id="radio-tigan"> <%for(var i=0;i<recor ...
- 成都Uber优步司机奖励政策(3月25日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [Hbase]hbase命令行基本操作
-进入hbase shell hbase shell - 帮助help help - 查看hbase versionversion - 查看hbase 状态 status - 创建表create 't ...