【Luogu】P4172水管局长(LCT)
有个结论是x到y的路径上最长边权值等于最小生成树上最长边权值,于是问题转化为最小生成树。
再考虑把问题反过来,删边变成加边。
于是变成动态维护最小生成树,LCT可以做到。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<map>
#define maxn 200020
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Edge{
int from,to,val;
bool operator <(const Edge &a)const{ return val<a.val; }
}edge[maxn]; int stack[maxn],top; struct Splay{
struct Node{
int e[],fa,mini,tag,val,maxi;
}tree[maxn];
inline int iden(int x){ return x==tree[tree[x].fa].e[]; }
inline void connect(int x,int fa,int how){ tree[x].fa=fa; tree[fa].e[how]=x; }
inline bool isroot(int x){ return tree[tree[x].fa].e[]!=x&&tree[tree[x].fa].e[]!=x; }
inline void update(int x){
int le=tree[x].e[],ri=tree[x].e[]; tree[x].maxi=tree[x].val;
if(edge[tree[le].maxi].val>edge[tree[x].maxi].val) tree[x].maxi=tree[le].maxi;
if(edge[tree[ri].maxi].val>edge[tree[x].maxi].val) tree[x].maxi=tree[ri].maxi;
}
inline void reverse(int x){
swap(tree[x].e[],tree[x].e[]);
tree[x].tag^=;
}
inline void pushdown(int x){
if(tree[x].tag==) return;
if(tree[x].e[]) reverse(tree[x].e[]);
if(tree[x].e[]) reverse(tree[x].e[]);
tree[x].tag=;
}
void rotate(int x){
int y=tree[x].fa,r=tree[y].fa;
int sony=iden(x),sonr=iden(y);
tree[x].fa=r; if(!isroot(y)) tree[r].e[sonr]=x;
int b=tree[x].e[sony^];
connect(b,y,sony);
connect(y,x,sony^);
update(y);
}
inline void pushto(int x){
top=;
while(!isroot(x)){
stack[++top]=x;
x=tree[x].fa;
}
pushdown(x);
while(top) pushdown(stack[top--]);
}
void splay(int x){
pushto(x);
while(!isroot(x)){
int fa=tree[x].fa;
if(!isroot(fa))
if(iden(fa)==iden(x)) rotate(fa);
else rotate(x);
rotate(x);
}
update(x);
}
inline void access(int x){
int last=;
while(x){
splay(x);
tree[x].e[]=last;
update(x);
last=x; x=tree[x].fa;
}
}
inline void makeroot(int x){
access(x);
splay(x);
reverse(x);
}
inline int findroot(int x){
access(x);
splay(x);
while(tree[x].e[]) x=tree[x].e[];
return x;
}
inline void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
inline void link(int x,int y){
//printf("%d %d\n",x,y);
split(x,y);
tree[x].fa=y;
}
inline void cut(int x,int y){
//printf("%d %d\n",x,y);
split(x,y);
if(tree[y].e[]!=x||tree[x].e[]) return;
tree[x].fa=tree[y].e[]=;
}
}s; struct Que{
int opt,x,y,id,cnt;
}q[maxn];
int cnt; int ans[maxn]; int d[maxn*]; bool vis[maxn]; int main(){
int n=read(),m=read(),e=read();
for(int i=;i<=m;++i){
edge[i]=(Edge){read(),read(),read()};
if(edge[i].from>edge[i].to) swap(edge[i].from,edge[i].to);
}
sort(edge+,edge+m+);
for(int i=;i<=m;++i){
//printf("%d %d>><\n",edge[i].from,edge[i].to);
s.tree[i+n].mini=s.tree[i+n].val=i;
d[edge[i].from*n+edge[i].to]=i;
}
for(int i=;i<=e;++i){
q[i]=(Que){read(),read(),read(),};
if(q[i].x>q[i].y) swap(q[i].x,q[i].y);
if(q[i].opt==) q[i].id=++cnt;
else{
int now=d[q[i].x*n+q[i].y];
q[i].cnt=now;
//printf("%d\n",now);
vis[now]=;
}
}
int sum=;
for(int i=;i<=m;++i){
if(sum==n-) break;
if(vis[i]) continue;
if(s.findroot(edge[i].from)==s.findroot(edge[i].to)) continue;
s.link(edge[i].from,i+n);
s.link(edge[i].to,i+n);
sum++;
}
for(int i=e;i;--i){
if(q[i].opt==){
s.split(q[i].x,q[i].y);
ans[q[i].id]=edge[s.tree[q[i].y].maxi].val;
}
else{
s.split(q[i].x,q[i].y);
int now=q[i].cnt;
int ret=s.tree[q[i].y].maxi;
if(edge[now].val<edge[ret].val){
if(edge[ret].from) s.cut(edge[ret].from,ret+n);
if(edge[ret].to) s.cut(edge[ret].to,ret+n);
s.link(q[i].x,now+n);
s.link(q[i].y,now+n);
}
}
}
for(int i=;i<=cnt;++i) printf("%d\n",ans[i]);
return ;
}
【Luogu】P4172水管局长(LCT)的更多相关文章
- luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...
- P4172 [WC2006]水管局长 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...
- 洛谷.4172.[WC2006]水管局长(LCT Kruskal)
题目链接 洛谷(COGS上也有) 不想去做加强版了..(其实处理一下矩阵就好了) 题意: 有一张图,求一条x->y的路径,使得路径上最长边尽量短并输出它的长度.会有<=5000次删边. 这 ...
- BZOJ 2594 水管局长 - LCT 维护链信息
Solution 由于链信息不好直接维护, 所以新建一个节点存储边的权值, 并把这个节点连向 它所连的节点 $u$, $v$ $pushup$中更新维护的 $mx$ 指向路径上权值最大的边的编号. 由 ...
- 洛谷4172 WC2006水管局长(LCT维护最小生成树)
这个题和魔法森林感觉有很相近的地方啊 同样也是维护一个类似最大边权最小的生成树 但是不同的是,这个题是有\(cut\)和询问,两种操作.... 这可如何是好啊? 我们不妨倒着来考虑,假设所有要\(cu ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
随机推荐
- Python小脚本程序
本文旨在搜集最简单最原子性的代码块,简单清晰容易阅读,然后由用户自己组合.Python代码排版请自行修改. 这里是索引目录: 1. 下载网站文件 2. 下载网站多个文件 1. 下载网站文件 impor ...
- Excel自动从身份证中提取生日、性别、年龄
现在学生的身份证号已经全部都是18位的新一代身份证了,里面的数字都是有规律的.前6位数字是户籍所在地的代码,7-14位就是出生日期.第17位“2”代表的是性别,偶数为女性,奇数为男性.我们要做的就是把 ...
- Vimim是VI中最好的输入法
Vimim是VI中最好的输入法 由于在VI中,normal和insert模式的存在,如果在insert模式下正在输入中文,在通过ESC键返回到normal模式后,系统的中文输入法会与VI的命令相冲突, ...
- java实现单链表归并算法
public class LinkMergeSort {static int number=0;public static void main(String[] args) {int[] a = {1 ...
- 【计数】cf938E. Max History
发现有一种奇怪的方法不能快速预处理? 复习一下常见的凑组合数的套路 You are given an array a of length n. We define fa the following w ...
- 日期增加天数--JS Date
//日期加天数的方法 //dataStr日期字符串 //dayCount 要增加的天数 //return 增加n天后的日期字符串 function dateAddDays(dataStr,dayCou ...
- 小象学院Python数据分析第二期【升级版】
点击了解更多Python课程>>> 小象学院Python数据分析第二期[升级版] 主讲老师: 梁斌 资深算法工程师 查尔斯特大学(Charles Sturt University)计 ...
- 【CodeBase】通过层级键在多维数组中获取目标值
通过层级键在多维数组中获取目标值 /* *Author : @YunGaZeon *Date : 2017.08.09 *param data : Data Array *param keys : K ...
- matplotlib(一)——matplotlib横轴坐标密集字符覆盖
一.问题描述 具体问题是: 用python库matplotlib进行数据的图表展示: 图表展示图形横坐标有将近100个自定义值需要显示: 保存矢量图(svg),保存后发现横坐标过于密集,坐标值之间有覆 ...
- JZOJ 2499. 东风谷早苗
2499. 东风谷早苗 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Goto Pro ...