BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 587 Solved: 259
[Submit][Status][Discuss]
Description

Input
Output
Sample Input
1 2
1 3
3 4
4 5
4 2
1 1 5
0 4 2
1 5 1
-1
Sample Output
3
HINT
Source
时间倒流
首先把所有的操作全都读进来
然后把最终形态的树建出来,
这样删边操作就变成了加边操作
维护联通性用LCT,缩点的话用并查集维护祖先,然后暴力改祖先即可
access操作稍微有一些改动,具体看代码吧
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') {x = x * + c - ''; c = getchar();}
return x * f;
}
int N, M;
int father[MAXN];
int find(int x) {
if(father[x] == x) return father[x];
else return father[x] = find(father[x]);
}
int unionn(int x, int y) {
int fx = find(x), fy = find(y);
father[fx] = fy;
}
#define ls(x) T[x].ch[0]
#define rs(x) T[x].ch[1]
#define fa(x) T[x].f
struct node {
int f, ch[], r, siz;
}T[MAXN];
int ident(int x) {
return T[fa(x)].ch[] == x ? : ;
}
void update(int x) {
T[x].siz = T[ls(x)].siz + T[rs(x)].siz + ;
}
void pushdown(int x) {
if(T[x].r) {
T[ls(x)].r ^= , T[rs(x)].r ^= ;
swap(ls(x), rs(x));
T[x].r = ;
}
}
void connect(int x, int fa, int how) {
T[x].f = fa;
T[fa].ch[how] = x;
}
bool IsRoot(int x) {
return T[fa(x)].ch[] != x && T[fa(x)].ch[] != x;
}
void rotate(int x) {
int Y = fa(x), R = fa(Y), Yson = ident(x), Rson = ident(Y);
int B = T[x].ch[Yson ^ ];
T[x].f = R;
if(!IsRoot(Y)) connect(x, R, Rson);
connect(B, Y, Yson);
connect(Y, x, Yson ^ );
update(Y);update(x);
}
int st[MAXN];
void splay(int x) {
int y = x, top = ;
st[++top] = y;
while(!IsRoot(y)) st[++top] = y = fa(y);
while(top) pushdown(st[top--]);
for(int y = fa(x); !IsRoot(x); rotate(x), y = fa(x))
if(!IsRoot(y))
rotate( ident(x) == ident(y) ? y : x);
}
void access(int x) {
for(int y = ; x;y = x, x = fa(y) = find(fa(x)))//这里要改一下
splay(x), rs(x) = y, update(x);
}
void makeroot(int x) {
access(x); splay(x);
T[x].r ^= ;
}
void link(int x, int y) {
//makeroot(x);
T[x].f = y;
}
int findroot(int x) {
//makeroot(x);
access(x);splay(x);
pushdown(x);
while(ls(x)) pushdown(x = ls(x));
splay(x);
return x;
}
void delet(int x, int to) {
if(x) father[x] = to, delet(ls(x), to), delet(rs(x), to);
}
void merge(int x, int y) {
if(x == y) return ;
makeroot(x);
if(findroot(y) != x) {
link(x, y); return ;
}
delet(rs(x), x); rs(x) = ;
update(x);
}
int split(int x,int y) {
makeroot(x);
access(y);
splay(y);
}
struct Edge {
int u,v;
bool operator < (const Edge &rhs) const{
return u < rhs.u || (u == rhs.u && v < rhs.v);
}
}E[MAXN];
int vis[MAXN];
struct Query {
int opt, x, y;
}Q[MAXN];
int Qnum = ;
int ans[MAXN];
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#else
//freopen("lane.in","r",stdin);
//freopen("lane.out","w",stdout);
#endif
N = read(), M = read();
for(int i = ; i <= N; i++) father[i] = i;
for(int i = ; i <= M; i++) {
E[i].u = read(), E[i].v = read();
if(E[i].u > E[i].v) swap(E[i].u, E[i].v);
} sort(E + , E + M + );
while(scanf("%d", &Q[++Qnum].opt) && Q[Qnum].opt != -) {
Q[Qnum].x = read(),
Q[Qnum].y = read();
if(Q[Qnum].x > Q[Qnum].y) swap(Q[Qnum].x, Q[Qnum].y);
if(Q[Qnum].opt == )
vis[lower_bound(E + , E + M + , (Edge){Q[Qnum].x, Q[Qnum].y} ) - E] = ;
}
Qnum--;
for(int i = ; i <= M; i++)
if(!vis[i])
merge(
find(E[i].u),
find(E[i].v)
); int ansnum = ;
for(int i = Qnum; i >= ; i--) {
Q[i].x = find(Q[i].x); Q[i].y = find(Q[i].y);
if(Q[i].opt == )
merge(Q[i].x, Q[i].y);
else
split(Q[i].x, Q[i].y), ans[++ansnum] = T[Q[i].y].siz - ;
}
while(ansnum) printf("%d\n", ans[ansnum--]);
return ;
}
BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)的更多相关文章
- [bzoj1969] [Ahoi2005]LANE 航线规划
tarjan.并查集.树状数组.树链剖分. 时间倒流,变删边为加边. 先求一波边双联通分量,缩点. 题目保证最后还是整张图联通的..所以就是一棵树. 现在的操作就是,将路径上的边权置0(加边时),查询 ...
- bzoj1969: [Ahoi2005]LANE 航线规划(树链剖分)
只有删边,想到时间倒流. 倒着加边,因为保证图连通,所以一开始一定至少是一棵树.我们先建一棵树出来,对于每一条非树边,两个端点在树上这段路径的边就不会变成关键边了,所以将它们对答案的贡献删去,那么直接 ...
- bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1969 题解 如果我们把整个图边双联通地缩点,那么最终会形成一棵树的样子. 那么在这棵树上,\( ...
- 【BZOJ1969】[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树
[BZOJ1969][Ahoi2005]LANE 航线规划 Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由 ...
- BZOJ 1969: [Ahoi2005]LANE 航线规划( 树链剖分 )
首先我们要时光倒流, 倒着做, 变成加边操作维护关键边. 先随意搞出一颗树, 树上每条边都是关键边(因为是树, 去掉就不连通了)....然后加边(u, v)时, 路径(u, v)上的所有边都变成非关键 ...
- 【BZOJ 1969】 1969: [Ahoi2005]LANE 航线规划 (树链剖分+线段树)
1969: [Ahoi2005]LANE 航线规划 Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星 ...
- [Ahoi2005]LANE 航线规划
题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...
- 【刷题】BZOJ 1969 [Ahoi2005]LANE 航线规划
Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系--一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel ...
- 【bzoj1959】[Ahoi2005]LANE 航线规划 树链剖分+线段树
题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...
随机推荐
- 转:问题解决:The project cannot be built until build path errors are resolved
转自:http://blog.csdn.net/marty_zhu/article/details/2566299 今天在eclipse里遇到这个问题,之前也遇到过,不过,通过clean一下项目,或者 ...
- Androidpdf
https://www.jb51.net/article/110238.htm https://blog.csdn.net/u010046908/article/details/53927157 &l ...
- 转:Java Socket编程
对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了.首先ServerSocket将 ...
- FTP上传和WEB上传的区别
说区别之前,咱先说说什么是上传?上传就是将信息从个人计算机(本地计算机)传递到中央计算机(远程计算机)系统上,让网络上的人都能看到.将制作好的网页.文字.图片等发布到互联网上去,以便让其他人浏览 ...
- DockerFile简介以及使用
DockerFile是用来构建docker镜像的构建文件,是有一系列命令和参数构成的脚本 构建的三步骤:编写dockerfile文件→build构建→docker run dockerfile保留字指 ...
- 转载:VMWARE虚拟机无法访问的三种方法分析
bridged(桥接模式).NAT(网络地址转换模式)host-only(主机模式).理论认识:1.bridged(桥接模式)在这个地方模式.虚拟机等同于网络内的一台物理主机,可对手动设置IP,子网掩 ...
- Python 及其基础语法
重新开始玩 Python,打算就是学完实验楼的"Python3 简明教程",然后就可以玩点小项目,先前学了点 Python2 就不管它啦. 以上. 认识 Python Python ...
- maven相关基础
0. 本文主要参考一下良心maven原创文摘: 0.0 maven官网传送门 http://maven.apache.org/ 0.1 maven日常 http://www.cnblogs.com/x ...
- Anaconda安装及pygame的安装
python有很多版本,还是Anaconda最好用啦,因为它有强大而方便的包管理与环境管理的功能... Pygame是Python最经典的2D游戏开发第三方库,也支持3D游戏开发,,最近课余开始学这个 ...
- prority_queue自定义类型使用
struct Tower{ Tower(int h, int p){ height = h; pos = p; } bool operator < (Tower &t) { if (he ...