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, ...
随机推荐
- java如何集成支付宝移动快捷支付功能
项目需要,需要在客户端集成支付宝接口.第一次集成,过程还是挺简单的,不过由于支付宝官方文档写的不够清晰,也是走了一些弯路,下面把过程写出来分享给大家.就研究了一下:因为使用支付宝接口,就需要到支付宝官 ...
- StretchBlt函数和BitBlt函数的用法
StretchBlt和BitBlt都用在双缓冲视图中,用来显示一幅图像 一.StretchBlt 函数从源矩形中复制一个位图到目标矩形,必要时按目标设备设置的模式进行图像的拉伸或压缩.也即是将内存中的 ...
- Mixin模式
Mixin是JavaScript中用的最普遍的模式,几乎所有流行类库都会有Mixin的实现. Mixin是掺合,混合,糅合的意思,即可以就任意一个对象的全部或部分属性拷贝到另一个对象上. 从提供的接口 ...
- 试题 F: 特别数的和 第十届蓝桥杯
试题 F: 特别数的和时间限制: 1.0s 内存限制: 512.0MB 本题总分: 15 分[问题描述]小明对数位中含有 2. 0. 1. 9 的数字很感兴趣(不包括前导 0),在 1 到40 中这样 ...
- MSP430 G2553 LaunchPad GPIO中断
P1.P2端口上的每个管脚都支持外部中断.P1端口的所有管脚都对应同一个中断向量(Interrupt Vector),类似的,P2端口的所有管脚都对应另一个中断向量:通过PxIFG寄存器来判断中断来源 ...
- Tempdb--Allocation Bottleneck
Alloctaion bottleneck refers to contention in the system pages that store allocation structures. PFS ...
- virtualbox上硬盘安装coreos
网址: http://www.serfdom.cn/index.php/archives/4/ http://www.360doc.com/content/14/1118/10/15077656_42 ...
- .net core执行dotnet ef migrations createmodel等命令出错
.net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...
- WP8.1StoreApp(WP8.1RT)---SystemTray的变化
原Microsoft.Phone.Shell中的SystemTray,已经改到Windows.UI.ViewManagement中StatusBar了. 只能在代码中设置相关属性. 如: 1 2 3 ...
- WebService-php- 2(17)
wsdl实例 <?xml version ='1.0' encoding ='UTF-8' ?> <definitions targetNamespace='http://local ...