题面

bzoj

题解

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)的更多相关文章

  1. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  5. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  6. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  7. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  8. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  9. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  10. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. 转载:字符串hash总结(hash是一门优雅的暴力!)

    转载自:远航休息栈 字符串Hash总结 Hash是什么意思呢?某度翻译告诉我们: hash 英[hæʃ] 美[hæʃ]n. 剁碎的食物; #号; 蔬菜肉丁;vt. 把…弄乱; 切碎; 反复推敲; 搞糟 ...

  2. 分布式java应用基础与实践

      始读于2014年4月30日,完成于2014年6月6日15:43:39. 阿里巴巴高级研究员林昊早年的书了,这些理论放到今天估计工作一两年的人都耳熟能详了,我个人很早以前就知道此书一直没有找到资源, ...

  3. 常见的http response

    200            //OK                                 400            //bad request 401           //Una ...

  4. 【译】微型ORM:PetaPoco【不完整的翻译】(转)

    出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...

  5. medusa爆破路由

    medusa –M http -h 192.168.10.1 -u admin -P /usr/share/wfuzz/ wordlist/fuzzdb/wordlists-user-passwd/p ...

  6. 怎样将word中的图片插入到CSDN博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. 团体程序设计天梯赛L1-018 大笨钟 2017-03-22 17:29 79人阅读 评论(0) 收藏

    L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...

  8. kali下firefox的安装

    在kali的系统中自带了一个firefox分支下的浏览器Iceweasel(Iceweasel是Mozilla Firefox浏览器的Debian再发布版),但是怎么说也配不上kali的强悍气势.还是 ...

  9. Oracle EBS Color 色彩设置

    Oracle EBS配色方案的截图 If the Java Look and Feel profile option is set to Oracle, the Java Color Scheme c ...

  10. C#基础入门 九

    C#基础入门 九 集合 对于很多应用程序,需要创建和管理相关对象组,有两种方式可以将对象分组,一是创建对象数组,如 object[] obj=new object[3]{1,2.33,"st ...