bzoj4998: 星球联盟(link-cut-tree)
题面
题解
bzoj2959: 长跑的弱化版
产生了环就并查集维护一下
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 200010;
int ch[N][2], w[N], fa[N], S[N], top, bl[N];
bool rev[N];
int find(int x) {
return bl[x] == x ? x : bl[x] = find(bl[x]);
}
#define get(x) (ch[find(fa[x])][1] == x)
bool isroot(int x) {
return ch[find(fa[x])][0] != x && ch[find(fa[x])][1] != x;
}
void rotate(int x) {
int y = find(fa[x]), z = find(fa[y]), k = get(x);
if (!isroot(y)) ch[z][get(y)] = x;
fa[x] = z;
ch[y][k] = ch[x][k ^ 1]; fa[ch[x][k ^ 1]] = y;
ch[x][k ^ 1] = y; fa[y] = x;
}
void putrev(int x) {
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
}
void pushdown(int x) {
if (rev[x]) {
if (ch[x][0]) putrev(ch[x][0]);
if (ch[x][1]) putrev(ch[x][1]);
rev[x] = 0;
}
}
void splay(int x) {
S[top = 1] = x;
for (int i = x; !isroot(i); i = find(fa[i])) S[++top] = find(fa[i]);
for (int i = top; i; i--) pushdown(S[i]);
while (!isroot(x)) {
int y = find(fa[x]);
if (!isroot(y))
(get(x) ^ get(y)) ? rotate(x) : rotate(y);
rotate(x);
}
}
void access(int x) { for(int y = 0; x; y = x, x = find(fa[x])) splay(x), ch[x][1] = y; }
void makeroot(int x) { access(x); splay(x); putrev(x); }
void split(int x, int y) { makeroot(x); access(y); splay(y); }
void link(int x, int y) { makeroot(x); fa[x] = y; }
int pa[N];
int getf(int x) {
return pa[x] == x ? x : pa[x] = getf(pa[x]);
}
void dfs(int x, int y) {
w[y] += w[x];
bl[x] = y;
if (ch[x][0]) dfs(ch[x][0], y);
if (ch[x][1]) dfs(ch[x][1], y);
}
int main() {
int n, m, p;
read(n), read(m), read(p);
for (int i = 1; i <= n; i++) pa[i] = bl[i] = i, w[i] = 1;
for (int i = 1, u, v; i <= m; i++) {
read(u), read(v);
u = find(u), v = find(v);
if (u == v) continue;
if (getf(u) == getf(v)) {
split(u, v);
dfs(ch[v][0], v);
continue;
}
pa[getf(v)] = getf(u);
link(u, v);
}
for (int i = 1, x, y; i <= p; i++) {
read(x), read(y);
x = find(x), y = find(y);
if (x == y) {
printf("%d\n", w[x]);
continue;
}
if (getf(x) != getf(y)) {
puts("No");
link(x, y);
pa[getf(y)] = getf(x);
continue;
}
split(x, y);
dfs(ch[y][0], y);
printf("%d\n", w[y]);
}
return 0;
}
bzoj4998: 星球联盟(link-cut-tree)的更多相关文章
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
随机推荐
- MySQL redo log及recover过程浅析
写在前面:作者水平有限,欢迎不吝赐教,一切以最新源码为准. InnoDB redo log 首先介绍下Innodb redo log是什么,为什么需要记录redo log,以及redo log的作用都 ...
- SOCKET句柄泄露带来的内存灾难
前些时候游戏莫名其妙出现大量内存泄露,我感到很诧异,当然一般情况下游戏的内存管理是极其严苛的,出现如此大量的内存泄露到底是怎么回事? 句柄滥用导致的内存泄露会多夸张呢,尤其SOCKET,在某些客户端系 ...
- PHP(十二)文件操作
- StretchBlt函数和BitBlt函数的用法
StretchBlt和BitBlt都用在双缓冲视图中,用来显示一幅图像 一.StretchBlt 函数从源矩形中复制一个位图到目标矩形,必要时按目标设备设置的模式进行图像的拉伸或压缩.也即是将内存中的 ...
- VS中的Debug 和 Release 编译方式的本质区别
VS中的Debug 和 Release 编译方式的本质区别 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进行了各种优化,使 ...
- Apps: Help > Diagnostics > Custom Code > Personalize 查看LOV中的查询语句
Apps Menu Path: Help > Diagnostics > Custom Code > Personalize 查看LOV中的查询语句 一直有实施顾问询问我XXFo ...
- java-01 JAVA三大版本比较&JDK、JRE、JVM 的关系
1. java三大版本比较 1.1 java SE javaSE 是java标准版的简称,其定位是个人计算机应用(应用原生界面比较ugly) 全称:Java Platform Standard Edi ...
- vue实现随机验证码功能
效果图: 1.html代码 <div class="form-group" style="display: flex;"> <div> ...
- 自定义Team Foundation Server (TFS) 与Project Professional的集成字段
用户可以象使用Office Excel一样,使用Project Professional连接TFS,将数据下载到本地修改,并且发布到TFS服务器上,如果你习惯使用Project来计划你的项目,那么Pr ...
- Tempdb--Snapshot
The insert operation does not cause a row version to be generated because there is really no prvious ...