洛谷 P2542 [AHOI2005]航线规划(Link-cut-tree)
题面
题解
离线处理+LCT
有点像星球大战
我们可以倒着做,断边变成连边
我们可以把边变成一个点
连边时,如果两个点本身不联通,就\(val\)赋为\(1\),并连接这条边
如果,两个点本身就联通,那么就不连接这条边,把两点之间的\(val\)全部赋为\(0\)
\(ans\)就是求两点之间的\(sum(val)\)
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
inline int gi() {
RG int 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-'0', c = getchar();
return f ? -x : x;
}
const int N = 52000;
struct node {
int ch[2], f, sum, rev, v, ly;
}t[N<<2];
bool isroot(int x) {
return t[t[x].f].ch[0] != x && t[t[x].f].ch[1] != x;
}
inline int get(int x) {
return t[t[x].f].ch[1] == x;
}
inline void pushup(int x) {
t[x].sum = t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].v;
}
void rotate(int x) {
int y = t[x].f, z = t[y].f, k = get(x);
if (!isroot(y))
t[z].ch[get(y)] = x;
t[x].f = z;
t[t[x].ch[k^1]].f = y; t[y].ch[k] = t[x].ch[k^1];
t[y].f = x; t[x].ch[k^1] = y;
pushup(y);
return ;
}
int top, S[N<<2];
inline void putly(int x) {t[x].ly = 1; t[x].sum = t[x].v = 0;}
void pushdown(int x) {
if (t[x].rev) {
swap(t[x].ch[0], t[x].ch[1]);
if (t[x].ch[0]) t[t[x].ch[0]].rev ^= 1;
if (t[x].ch[1]) t[t[x].ch[1]].rev ^= 1;
t[x].rev = 0;
}
if (t[x].ly) {
if (t[x].ch[0]) putly(t[x].ch[0]);
if (t[x].ch[1]) putly(t[x].ch[1]);
t[x].ly = 0;
}
return ;
}
void splay(int x) {
S[top=1] = x;
for (int i = x; !isroot(i); i = t[i].f) S[++top] = t[i].f;
for (int i = top; i; i--) pushdown(S[i]);
while (!isroot(x)) {
int y = t[x].f;
if (!isroot(y))(get(x)^get(y)) ? rotate(x):rotate(y);
rotate(x);
}
pushup(x);
return ;
}
void access(int x) {for (int y=0;x;y=x,x=t[x].f) splay(x),t[x].ch[1]=y,pushup(x);}
void makeroot(int x){access(x),splay(x),t[x].rev ^= 1;}
void split(int x, int y){makeroot(x),access(y),splay(y);}
void link(int x, int y) {makeroot(x);t[x].f = y;}
int findroot(int x) {access(x); splay(x);while (t[x].ch[0]) x = t[x].ch[0];return x;}
struct Line {
int u, v;
bool flag;
}E[N<<2];
map<pair<int, int>, int> M;
struct question {
int op, ans, u, v;
}q[N];
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n = gi(), m = gi();
for (int i = 1; i <= m; i++) {
int u = gi(), v = gi();
if (u > v) swap(u, v);
E[i] = (Line) {u, v, 0};
t[i+n].v = 1;
M[make_pair(u, v)] = i;
}
int cnt = 0;
for (;;) {
int c = gi(); if (c == -1) break;
int u = gi(), v = gi(); if (u > v) swap(u, v);
q[++cnt] = (question) {c, 0, u, v};
if (!c) E[q[cnt].ans = M[make_pair(u, v)]].flag = 1;
}
for (int i = 1; i <= m; i++)
if (!E[i].flag) {
int u = E[i].u, v = E[i].v;
if (findroot(u) != findroot(v))
link(u, i+n), link(v, i+n);
else {
split(u, v);
putly(v);
}
}
for (int i = cnt; i; i--) {
if (q[i].op) {
int u = q[i].u, v = q[i].v;
split(u, v);
q[i].ans = t[v].sum;
}
else {
int u = q[i].u, v = q[i].v;
if (findroot(u) != findroot(v))
link(u, q[i].ans+n), link(v, q[i].ans+n);
else {
split(u, v);
putly(v);
}
}
}
for (int i = 1; i <= cnt; i++)
if (q[i].op)
printf("%d\n", q[i].ans);
return 0;
}
洛谷 P2542 [AHOI2005]航线规划(Link-cut-tree)的更多相关文章
- 洛谷 P2542 [AHOI2005]航线规划 解题报告
P2542 [AHOI2005]航线规划 题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel星系 ...
- 洛谷P2542 [AHOI2005]航线规划(LCT,双连通分量,并查集)
洛谷题目传送门 太弱了不会树剖,觉得LCT好写一些,就上LCT乱搞,当LCT维护双连通分量的练手题好了 正序删边是不好来维护连通性的,于是就像水管局长那样离线处理,逆序完成操作 显然,每个点可以代表一 ...
- 洛谷 P2542 [AHOI2005]航线规划 树链剖分_线段树_时光倒流_离线
Code: #include <map> #include <cstdio> #include <algorithm> #include <cstring&g ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- 洛谷P3690 【模板】Link Cut Tree (LCT)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- [洛谷P3690]【模板】Link Cut Tree (动态树)
题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...
- 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree
https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...
- P2542 [AHOI2005]航线规划 LCT维护双连通分量
\(\color{#0066ff}{ 题目描述 }\) 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
随机推荐
- 【codevs2822】爱在心中
题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度 ...
- ./run.sh --indir examples/demo/ --outdir examples/results/ --vis
(AlphaPose20180911) luo@luo-ThinkPad-W540:AlphaPose$ ./run.sh --indir examples/demo/ --outdir exampl ...
- pandas sort_values 排序后, index 也发生了改变,不改变的情况下需要 reset_index(drop = True)
shenpi.sort_values(by=['apply_date'],ascending=True,inplace=True)shenpi.reset_index(drop = True)
- 洛谷P2569 [SCOI2010]股票交易
P2569 [SCOI2010]股票交易 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股 ...
- VC中多线程(一)Win32 API对多线程编程的支持
http://blog.sina.com.cn/s/blog_4ae08ad801008yer.html
- CSS 中的 px、em、rem 和 vh
区分 px:Pixel.像素. em:相对长度单位.继承父级元素的 font-size,值是相对于父级元素font-size的倍数. rem:Root em.相对于根元素(即 <html> ...
- 深度学习:原理与应用实践(张重生) - Caffe
如今,深度学习是国际上非常活跃.非常多产的研究领域,它被广泛应用于计算机视觉.图像分析.语音识别和自然语言处理等诸多领域.在多个领域上,深度神经网络已大幅超越了已有算法的性能. 本书是深度学习领域的一 ...
- NAO机器人
NAO机器人是Aldebaran Robotics公司研制的一款人工智能机器人.它拥有着讨人喜欢的外形,并具备有一定程度的人工智能和约一定程度的情感智商并能够和人亲切的互动. 教学研究类/NAO机器人 ...
- gettimeofday()函数的使用方法
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到精确时间.它的精度可以达到微妙,是C标准库的函数. 2.函数原型: #include<sys/time.h> int ...
- React学习笔记4
遇到的问题 目前模板是自己任意定义的,样式不好控制 在组件设计时,可以把页面数据显示的地方,分割父子组件嵌套的结构,比如,商品数据显示列表,把组外层容器看成是父组件,里面是数据显示的渲染模板,看成是子 ...