洛谷 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的讲课) 链剖分,是指一类 ...
随机推荐
- opencv掩膜操作
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...
- Linux扩展根目录
一.简介 使用linux系统的过程中,有时发现系统根目录(/)的空间不足,导致系统运行很慢,针对该现象,本文详细介绍根目录(/)的空间扩展方法. 二.操作步骤 1)查看根目录大小 df 2)查找系 ...
- 卡2-SLAM
---恢复内容开始--- 1.下载 首先需要从github上Git以下两个包: git clonehttps://github.com/ros-perception/open_karto(开源的kar ...
- .NET读取服务器或本地文件
//把本地文件信息读入数据流中 FileStream stream = new FileStream(path, FileMode.Open, FileAcces ...
- 构建使用 Azure 网站的云
Apurva JoshiSunitha Muthukrishna 在设计云解决方案时,设计始终要为故障做好准备.这一点很重要,应牢记. 然而,许多应用程序并非按照这种方式构建. 出现这种情况的主要原因 ...
- Schwartz kernel theorem施瓦兹核定理
In mathematics, the Schwartz kernel theorem is a foundational result in the theory of generalized fu ...
- 设计模式03: Builder 生成器模式(创建型模式)
Builder生成器模式(创建型模式) Builder模式缘起假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分富于变化.如果使用最直观的设计方法,每个房屋部分的变化,都将 ...
- Maven 项目使用开源中国镜像
从maven中央库下载jar非常缓慢甚至有时候会下载不下来. 可以采用中国的maven镜像.目前主要是 开源中国的镜像. 找到maven配置文件setting.xml,打开 中间添加开源中国的配置: ...
- spring的一些配置和重要的接口和类
spring的配置文件 通常是applicationContext.xml(具体的bean配置会在后面内容中详解) setter方法注入: <property name=“” value=“ja ...
- Oracle 已连接到空闲例程或ORA-01034: ORACLE not available
因为是本地数据库没有重要资料,所以可以随便自己折腾. 出现问题原因:从生产数据库导入一个表到本地库测试,因数据量过大,在导入4-5个小时后,手动中断导入.是否异常关机不能确定. 之后再打开数据库出现一 ...