【BZOJ1969】航线规划(Link-Cut Tree)
【BZOJ1969】航线规划(Link-Cut Tree)
题面
题解
删边操作
套路呀
离线读入倒过来做
变成加边操作
现在考虑怎么确定两点直接的关键路径条数
如果是一棵树,那么每条边都是关键路径
在一棵树的基础上
如果连接了两点
那么,两点之间原来的路径一定都不是关键路径
所以,弄一个\(LCT\)
每次维护加边
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 52000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
map<pair<int,int>,int> M;
struct qq{int opt,ans;int u,v;}q[MAX];
struct Line{int v,next;}e[MAX*4];
struct edge{int u,v,br;}E[MAX*4];
int h[MAX],cnt=1,tot,n,m;
int S[MAX<<2],top;
inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}
struct Node
{
int ch[2],ff;
int sum,rev,v;
int ly;
}t[MAX<<2];
bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
void pushup(int x){t[x].sum=t[lson].sum+t[rson].sum+t[x].v;}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff;
int k=t[y].ch[1]==x;
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
void putrev(int x){swap(lson,rson);t[x].rev^=1;}
void putly(int x){t[x].ly=1;t[x].sum=t[x].v=0;}
void pushdown(int x)
{
if(t[x].rev)
{
if(lson)putrev(lson);
if(rson)putrev(rson);
t[x].rev^=1;
}
if(t[x].ly)
{
if(lson)putly(lson);
if(rson)putly(rson);
t[x].ly=0;
}
}
void Splay(int x)
{
S[top=1]=x;
for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
while(top)pushdown(S[top--]);
while(!isroot(x))
{
int y=t[x].ff,z=t[y].ff;
if(!isroot(y))
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),t[x].ch[1]=y,pushup(x);}
void makeroot(int x){access(x);Splay(x);putrev(x);}
void split(int x,int y){makeroot(x);access(y);Splay(y);}
void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;pushup(y);}
void link(int x,int y){makeroot(x);t[x].ff=y;}
int findroot(int x){access(x);Splay(x);while(lson)x=lson;return x;}
int main()
{
n=read();m=read();
for(int i=1;i<=m;++i)
{
int u=read(),v=read();
if(u>v)swap(u,v);
E[i]=(edge){u,v};
M[make_pair(u,v)]=i;
t[i+n].v=1;
}
while(233)
{
int c=read();if(c==-1)break;
int u=read(),v=read();if(u>v)swap(u,v);
q[++tot]=(qq){c,0,u,v};
if(!c)E[q[tot].ans=M[make_pair(u,v)]].br=1;
}
for(int i=1;i<=m;++i)
if(!E[i].br)
{
if(findroot(E[i].u)!=findroot(E[i].v))
link(E[i].u,i+n),link(E[i].v,i+n);
else split(E[i].u,E[i].v),putly(E[i].v);
}
for(int i=tot;i;--i)
{
if(q[i].opt)
{
split(q[i].u,q[i].v);
q[i].ans=t[q[i].v].sum;
}
else
{
if(findroot(q[i].u)!=findroot(q[i].v))
link(q[i].u,q[i].ans+n),link(q[i].v,q[i].ans+n);
else split(q[i].u,q[i].v),putly(q[i].v);
}
}
for(int i=1;i<=tot;++i)
if(q[i].opt)printf("%d\n",q[i].ans);
return 0;
}
【BZOJ1969】航线规划(Link-Cut Tree)的更多相关文章
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
随机推荐
- git使用基本故障
warning: LF will be replaced by CRLF in README.md. The file will have its original line endings in y ...
- dedecms判断当前页面是否为首页 织梦设置首页高亮
做织梦网站导航栏时,我们一般需要设置当前栏目高亮显示,这个使用currentstyle就能直接实现,但是如果在首页时怎么让首页模块高亮呢? 织梦当前栏目高亮: <style>.hover{ ...
- 自制PHP高防防盗链(不是一般的高)(思路)
原理:根据IP,资源ID,时间戳,一次性Access_Token,APPKEY(暴露在前台)和APPSERECT(后台)来生成参数,具体见下面: 浏览器请求页面=>后台引用防盗链代码=>生 ...
- Ajax疯狂讲义
Ajax: async javascript and xml 异步的JS和XML AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这 ...
- 来了解一下Mysql索引的相关知识:基础概念、性能影响、索引类型、创建原则、注意事项
索引的基础概念索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录,定位对应的页码:存储引擎使用类似的方式进行数据查询,先去索引当中找到对应的值,然后根据匹配的索引找到对应的数据行 ...
- hihoCoder 1044 : 状态压缩·一 状压dp
思路:状态压缩,dp(i, j)表示考虑前i个数且[i-m+1, i]的选择情况为j.如果要选择当前这个数并且,数位1的个数不超过q,则dp[i+1][nex] = max(dp[i+1][nex], ...
- JAVA 处理 Spring data mongodb 时区问题
Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的 但是我们查询的时候,并不会自动 + 8小时,需要自己处理 解决方法 1 @JsonFormat ...
- Flask Ansible自动化平台搭建(持续更新)
一:简介 使用Ansible + Flask + Celery搭建web平台. 目录结构 . ├── ansible_api │ ├── ansible_playbook_inventory.py ...
- Windows Subsystem for Linux (WSL)挂载移动硬盘U盘
WSL想通过移动硬盘处理一些数据,结果进去了无法发现移动硬盘,于是搜了好久也没有一个正确的解决办法,终于找到一个,现在贡献出来与大家共享. WSL比起linux挂载硬盘简单一些.而且windows本身 ...
- dojo省份地市级联之地市封装类(二)
dojo省份地市级联之地市封装类 City.java: /** * 地市封装类 */ package com.you.model; import java.io.Serializable; /** * ...