题面

洛谷

bzoj

题解

离线处理+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)的更多相关文章

  1. 洛谷 P2542 [AHOI2005]航线规划 解题报告

    P2542 [AHOI2005]航线规划 题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel星系 ...

  2. 洛谷P2542 [AHOI2005]航线规划(LCT,双连通分量,并查集)

    洛谷题目传送门 太弱了不会树剖,觉得LCT好写一些,就上LCT乱搞,当LCT维护双连通分量的练手题好了 正序删边是不好来维护连通性的,于是就像水管局长那样离线处理,逆序完成操作 显然,每个点可以代表一 ...

  3. 洛谷 P2542 [AHOI2005]航线规划 树链剖分_线段树_时光倒流_离线

    Code: #include <map> #include <cstdio> #include <algorithm> #include <cstring&g ...

  4. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

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

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  6. [洛谷P3690]【模板】Link Cut Tree (动态树)

    题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...

  7. 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree

    https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...

  8. P2542 [AHOI2005]航线规划 LCT维护双连通分量

    \(\color{#0066ff}{ 题目描述 }\) 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel ...

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

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

随机推荐

  1. 使用RandomAccessFile读写数据

    ------------siwuxie095 工程名:TestRandomAccessFile 包名:com.siwuxie095.file 类名:MultiWriteFile.java(主类).Wr ...

  2. php扩展开发1--添加函数

    目标:便携php扩展 要求实现 输出hello word 首先用的是php7.0.3   centos7.1或者centos6.+ 1.1 RPM安装PHP rpm -Uvh https://mirr ...

  3. qt数据库有效插件为空的情况

    打了一周的环境,从ubuntu到win7,搭建环境的时间比写代码的时间都多.先简单的介绍一下我搭建的环境不是纯QT环境,是一个芬兰的软件开发商开发出来的SDK里面完全融合qt,其中qt是以单独的目录存 ...

  4. opencv3读取视频并保存为图片

    #include <iostream> #include <vector> #include <opencv2/opencv.hpp> using namespac ...

  5. 258. Add Digits 数位相加到只剩一位数

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  6. 10-python中的requests应用

    使用request方便: #_*_ coding: utf-8 _*_ ''' Created on 2018年7月14日 @author: sss ''' import requests impor ...

  7. IP协议、ARP协议等之温故知新

    今天才知道: 1.IP协议的固定部分长度为20字节.(貌似有一家运维工程师面试我的时候,问过我这个问题呢.) 2.IP数据包首部中的协议?? 答:协议:占8位,指出此数据报携带的数据使用何种协议以便目 ...

  8. 第八课 ROS的空间描述和变换

    1.tf的实际应用 1)在机器人的配置中 从上面可以看出激光雷达中心距离机器人底座的中心有20cm,激光雷达的中心距机器人底座中心有10cm,如果激光雷达在障碍物前面0.3米,那么机器人底座离障碍物多 ...

  9. Python基础-5

    目录 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 hashlib re正则表达式 模块 ...

  10. Python基础入门-元祖

    其实,元组合列表的特性和使用几乎差不太多,今天我们重点来看下元组的一些操作和使用. 1.元祖的定义和特点 定义:元组是以小括号包围,元素以逗号分隔,不可变的序列之一. 特点: 1)元祖内的元素不可以增 ...