LCT好题:
这道题和水管局长这道题非常相似,都是一样的套路。
将本题的内容转化之后,问题就变成了:动态维护一棵最大生成树,并求两点之间的距离。
本题一共三个操作,我们一个一个来分析:
1.先看操作,这个比较好办,属于的常规操作,直接后查询点的信息即可。
2.然后是操作。我们想:如果加入这条边后,得到的仍然是一棵树,那我直接即可,更新答案。但如果加入的已经在一个连通块中,我们就要分情况讨论。如果当前的路径上的最小温度都比这个 大,那肯定不动;否则我们可以把那条最小的温度换成这个,答案一定更优。掉那条温度最小的边,上;
3.再考虑操作。实际上,这个操作可以转化为操作。对于边,如果它还存在,我们先把它给删了,这样将边的长度换掉,相当于就是又加入了一条新的边,像操作一样处理就行了。
几个注意的点:
1.这里判断两个点是否在一个连通块内,不适合用并查集,因为我们之后还要删边,应该用中的函数解决。
2.为了判断某条边是否还存在,我用了一个数组记录,说明该边不存在,说明该边存在。
3.本题处理的是边权信息,需要将其转化成点权,在每条边中间建一个新点。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
const int maxn=1200000+5;
const int mod=1000000;
const int INF=2e12;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,q;
struct node
{
int fa;
int son[2];
int wns;
int flag;
int dis;
}tree[maxn];
int len[maxn];
bool nroot(int x)
{
int t=tree[x].fa;
return (tree[t].son[0]==x || tree[t].son[1]==x);
}
int val[maxn];
void update(int x)
{
if(!x)
return;
tree[x].wns=val[x];
if(tree[x].son[0])
tree[x].wns=min(tree[x].wns,tree[tree[x].son[0]].wns);
if(tree[x].son[1])
tree[x].wns=min(tree[x].wns,tree[tree[x].son[1]].wns);
tree[x].dis=tree[tree[x].son[0]].dis+tree[tree[x].son[1]].dis+len[x];
}
void pushdown(int x)
{
if(x && tree[x].flag)
{
tree[tree[x].son[0]].flag ^=1;
tree[tree[x].son[1]].flag ^=1;
swap(tree[x].son[0],tree[x].son[1]);
tree[x].flag=0;
}
}
void rotate(int x)
{
int y=tree[x].fa;
int z=tree[y].fa;
int k=(tree[y].son[1]==x);
//grandfather
if(nroot(y))
tree[z].son[tree[z].son[1]==y]=x;
//grandson
tree[tree[x].son[k ^ 1]].fa=y;
//son
tree[y].fa=x;
tree[y].son[k]=tree[x].son[k ^ 1];
//father
tree[x].fa=z;
tree[x].son[k ^ 1]=y;
//update
update(y);
update(x);
}
int s[maxn];
int tot=0;
void splay(int x)
{
tot=0;
int now=x;
s[++tot]=now;
while(nroot(now))
{
s[++tot]=tree[now].fa;
now=tree[now].fa;
}
for(int i=tot;i>=1;i--)
pushdown(s[i]);
while(nroot(x))
{
int y=tree[x].fa;
int z=tree[y].fa;
if(nroot(y))
((tree[z].son[0]==y) ^ (tree[y].son[0]==x))?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
for(reg int y=0;x;y=x,x=tree[x].fa)
{
splay(x);
tree[x].son[1]=y;
update(x);
}
}
void makeroot(int x)
{
access(x);
splay(x);
tree[x].flag ^=1;
}
int Findroot(int x)
{
access(x);
splay(x);
pushdown(x);
while(tree[x].son[0])
{
x=tree[x].son[0];
pushdown(x);
}
splay(x);
return x;
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
void link(int x,int y)
{
makeroot(x);
if(Findroot(y)!=x)
tree[x].fa=y;
}
void cut(int x,int y)
{
makeroot(x);
if(Findroot(y)!=x || tree[y].fa!=x || tree[y].son[0])
return;
tree[y].fa=tree[x].son[1]=0;
update(x);
}
int Get(int val,int x)
{
while(x)
{
if(tree[x].son[0] && tree[tree[x].son[0]].wns==val)
x=tree[x].son[0];
else
if(tree[x].son[1] && tree[tree[x].son[1]].wns==val)
x=tree[x].son[1];
else
break;
}
return x;
}
char opt[15];
struct vode
{
int x,y;
int t;
int dis;
}edge[maxn];
int vis[maxn];
void add(int id,int x,int y,int t,int l)
{
int nowx=Findroot(x);
int nowy=Findroot(y);
if(nowx!=nowy)
{
vis[id]=0;
val[id+n]=t;
len[id+n]=l;
link(x,id+n);
link(id+n,y);
}
else
{
split(x,y);
int wns=tree[y].wns;
if(t<=wns)
{
vis[id]=1;
}
else
{
vis[id]=0;
int place=Get(wns,y);
vis[place-n]=1;
cut(edge[place-n].x,place);
cut(place,edge[place-n].y);
val[id+n]=t;
len[id+n]=l;
link(x,id+n);
link(id+n,y);
}
}
edge[id].x=x,edge[id].y=y;
edge[id].dis=l;
edge[id].t=t;
}
signed main()
{
n=read(),m=read();
memset(val,0x3f,sizeof(val));
for(int i=1;i<=n+m*3;i++)
vis[i]=1;
for(int i=1;i<=m;i++)
{
scanf("%s",opt+1);
if(opt[1]=='f')
{
int id=read(),x=read(),y=read(),t=read(),l=read();
id++,x++,y++;
if(x>y)
swap(x,y);
add(id,x,y,t,l);
}
else
if(opt[1]=='m')
{
int x=read(),y=read();
x++,y++;
int nowx=Findroot(x);
int nowy=Findroot(y);
if(nowx!=nowy)
{
printf("-1\n");
continue;
}
else
{
split(x,y);
printf("%lld\n",tree[y].dis);
}
}
else
if(opt[1]=='c')
{
int id=read(),l=read();
id++;
if(vis[id]==0)
{
cut(edge[id].x,id+n);
cut(id+n,edge[id].y);
vis[id]=1;
}
add(id,edge[id].x,edge[id].y,edge[id].t,l);
}
}
return 0;
}
/*
*/