题解:P6664 [清华集训 2016] 温暖会指引我们前行
前置知识:LCT (动态树),最小生成树。
题目传送门
一道不错的 LCT
先介绍一个 LCT 维护边权的小技巧:化边为点。
及对于编号为
对于如何建本题的最小生成树:根据题目对于“最温暖的路”的定义,
那么来分析题目的操作要求,无非三点:
find:建立一条u,v 之间的无向边,以及路径的两个权值:t,l ,代表温度和长度。move:求从u 到v 的最温暖的路径的长度,没有输出 -1。change:修改一条边的长度。
对于 find 操作,只需检查 link 连边;若已联通,则比较当前新出现的边与原树上
对于 move 操作,通过 findroot 来判断
对于 change 操作,和 LCT 修改点权的方式一致,只是本题修改的是
易错点:
- 节点和边的编号从 0 开始。
(本蒟蒻因为这个代码直接死机) - 存在"move"询问一个点到自己的距离,此时应为 0。
总体 Code
//码风有点丑,还请见谅
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5; //本题id<m,虚点编号最多到n+m
int n,m,id,u,v,len,t;
int ls[N],rs[N],fa[N],sta[N],tt[N],tl[N]; //LCT用的变量
string opt;
struct edge{
int u,v,t,l;
}e[N];
bool p[N],re[N]; //re是用来记录哪些编号的边在LCT上使用(虽然可能没用)
bool judgment(int now){
if(now!=ls[fa[now]] and now!=rs[fa[now]]){
return true;
}
return false;
}
int r_r(int now){
return now==rs[fa[now]]?1:0;
}
//我们维护的是路径上t的最小值,和路径上边l的总和
void up(int now){
tt[now]=now<=n?0:now-n; //用于判断是否为虚点,不是虚点权值初始权值无意义
if(e[tt[now]].t>e[tt[ls[now]]].t){
tt[now]=tt[ls[now]];
}
if(e[tt[now]].t>e[tt[rs[now]]].t){
tt[now]=tt[rs[now]];
}
tl[now]=now<=n?0:e[now-n].l;
tl[now]+=tl[ls[now]]+tl[rs[now]];
return ;
}
//以下为LCT的模版
void reverse(int now){
if(now!=0){
swap(ls[now],rs[now]);
p[now]=!p[now];
}
return ;
}
void down(int now){
if(p[now]){
reverse(ls[now]);
reverse(rs[now]);
p[now]=false;
}
return ;
}
void rotate(int now){
int f=fa[now],g=fa[f],snow=r_r(now),sf=r_r(f);
if(snow==1){
rs[f]=ls[now];
if(rs[f]!=0){
fa[rs[f]]=f;
}
ls[now]=f;
}
else{
ls[f]=rs[now];
if(ls[f]!=0){
fa[ls[f]]=f;
}
rs[now]=f;
}
if(!judgment(f)){
if(sf==1){rs[g]=now;}
else{ls[g]=now;}
}
fa[f]=now;fa[now]=g;
up(f);up(now);
return ;
}
void splay(int now){
int top=0;
sta[++top]=now;
for(int y=now;!judgment(y);y=fa[y]){
sta[++top]=fa[y];
}
while(top>0){
down(sta[top--]);
}
while(!judgment(now)){
int f=fa[now];
if(!judgment(f)){
if(r_r(now)==r_r(f)){
rotate(f);
}
else{
rotate(now);
}
}
rotate(now);
}
return ;
}
void access(int now){
for(int y=0;now!=0;y=now,now=fa[now]){
splay(now);
rs[now]=y;
up(now);
}
return ;
}
void makeroot(int now){
access(now);
splay(now);
reverse(now);
return ;
}
int findroot(int now){
access(now);
splay(now);
down(now);
while(ls[now]!=0){
now=ls[now];
down(now);
}
splay(now);
return now;
}
void split(int x,int y){
makeroot(x);
access(y);
splay(y);
return ;
}
void link(int x,int y){
makeroot(x);
if(findroot(y)!=x){
fa[x]=y;
}
return ;
}
void cut(int x,int y){
makeroot(x);
if(findroot(y)==x and fa[y]==x and ls[y]==0 and rs[x]==y){
fa[y]=rs[x]=0;
up(x);
}
return ;
}
//用于判断新发现的边与其连接两点间
void changedge(int w){
split(e[w].u,e[w].v);
int minn=tt[e[w].v];
if(e[minn].t<e[w].t){ //若新边的t值更大,则出现了更温暖的路径,需要在LCT上修改
cut(e[minn].u,minn+n);
cut(e[minn].v,minn+n);
link(e[w].u,w+n);
link(e[w].v,w+n);
re[w]=true;re[minn]=false;
}
return ;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
e[0].t=1000000001; //零号节点无意义,赋最大值方便比较
for(int i=1;i<=m;i++){
cin>>opt;
if(opt=="find"){
cin>>id>>u>>v>>t>>len;
id++;u++;v++; //一定要记得编号加一防止出错
e[id].u=u;e[id].v=v;e[id].t=t;e[id].l=len;
if(findroot(u)!=findroot(v)){
link(e[id].u,id+n); //连边时连接的是代表该边虚点和该边原本连接的两点
link(e[id].v,id+n);
re[id]=true;
}
else{
changedge(id);
}
}
else if(opt=="move"){
cin>>u>>v;u++;v++;
if(findroot(u)!=findroot(v)){ //不连通输出-1
cout<<-1<<'\n';
}
else if(u==v){cout<<0<<'\n';} //同一点距离为0
else{
split(u,v);
cout<<tl[v]<<'\n';
}
}
else{
cin>>id>>len;
id++;
if(re[id]){ //用于判断id编号的边是否在使用
splay(id+n);
e[id].l=len;
up(id+n);
}
else{e[id].l=len;}
}
}
return 0;
}
如有问题,还请指出。