【Luogu】P4234最小差值生成树(LCT)
能把LCT打得每个函数都恰有一个错误也是挺令我惊讶的。
本题使用LCT维护生成树,具体做法是对原图中的每个边建一个点,然后连边的时候相当于是将边的起点跟“边”这个点连起来,边的终点也跟它连起来。
放个图。

比如这是原边。
然后我们搞成这样。

那个小点就是原来那条边啦。
然后我们要连边的时候就连成这样

比如小点的编号为new,我们就要link(from,new),link(to,new)
切掉的时候就cut(from,new)cut(to,new)。
之后呢,我们令splay的每个节点维护以自己为根的splay中连的权值最小的边是哪条,这样连成一个环的时候我们就可以很方便的把环中最小的那个边切掉,就可以维护啦
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<queue>
#define maxn 500000
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){
return val<a.val;
}
}edge[maxn]; bool cmp(Edge a,Edge b){ return a.val<b.val; }
int stack[maxn],top; struct Splay{
struct Node{
int e[],fa,val,mini,tag,id;
}tree[maxn];
inline int iden(int x){ return x==tree[tree[x].fa].e[]; }
inline bool isroot(int x){ return x!=tree[tree[x].fa].e[]&&x!=tree[tree[x].fa].e[]; }
inline void update(int x){
tree[x].mini=tree[x].id;
if(edge[tree[tree[x].e[]].mini].val<edge[tree[x].mini].val) tree[x].mini=tree[tree[x].e[]].mini;
if(edge[tree[tree[x].e[]].mini].val<edge[tree[x].mini].val) tree[x].mini=tree[tree[x].e[]].mini;
}
inline void connect(int x,int fa,int how){ tree[x].fa=fa; tree[fa].e[how]=x; }
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){
int from=stack[top--];
pushdown(from);
}
}
inline void splay(int x){
pushto(x);
while(!isroot(x)){
if(!isroot(tree[x].fa))
if(iden(x)==iden(tree[x].fa)) rotate(tree[x].fa);
else rotate(x);
rotate(x);
}
update(x);
}
void access(int x){
int last=;
while(x){
splay(x);
tree[x].e[]=last;
update(x);
last=x; x=tree[x].fa;
}
return;
}
inline void makeroot(int x){
access(x);
splay(x);
reverse(x);
}
inline void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
inline void link(int x,int y){
split(x,y);
tree[x].fa=y;
}
inline void cut(int x,int y){
split(x,y);
if(tree[y].e[]!=x||tree[x].e[]) return;
tree[x].fa=tree[y].e[]=;
}
inline int findroot(int x){
access(x);
splay(x);
while(tree[x].e[]) x=tree[x].e[];
return x;
}
}s; bool vis[maxn]; int main(){
int n=read(),m=read();
for(int i=;i<=m;++i) edge[i]=(Edge){read(),read(),read()};
sort(edge+,edge+m+,cmp); edge[].val=0x7fffffff;
for(int i=;i<=m;++i){
s.tree[i+n].val=edge[i].val;
s.tree[i+n].id=i;
}
int cnt=,now=;
int ans=0x7fffffff;
for(int i=;i<=m;++i){
int from=edge[i].from,to=edge[i].to,val=edge[i].val;
if(from==to){
vis[i]=;
continue;
}
//printf("D");
if(s.findroot(from)==s.findroot(to)){
s.split(from,to);
int mini=s.tree[to].mini;
//printf("%d>>>>\n",mini);
s.cut(edge[mini].from,mini+n);
s.cut(edge[mini].to,mini+n);
vis[mini]=;
cnt--;
} s.link(from,i+n); s.link(to,i+n);
cnt++;
if(cnt>=n-){
while(vis[now]) now++;//printf("S");
ans=min(ans,val-edge[now].val);
} }
printf("%d\n",ans);
return ;
}
【Luogu】P4234最小差值生成树(LCT)的更多相关文章
- P4234 最小差值生成树 LCT维护边权
\(\color{#0066ff}{ 题目描述 }\) 给定一个标号为从 \(1\) 到 \(n\) 的.有 \(m\) 条边的无向图,求边权最大值与最小值的差值最小的生成树. \(\color{#0 ...
- Luogu P4234 最小差值生成树
题意 给定一个 \(n\) 个点 \(m\) 条边的有权无向图,求出原图的一棵生成树使得该树上最大边权与最小边权的差值最小. \(\texttt{Data Range:}1\leq n\leq 5\t ...
- 洛谷 P4234 最小差值生成树(LCT)
题面 luogu 题解 LCT 动态树Link-cut tree(LCT)总结 考虑先按边权排序,从小到大加边 如果构成一颗树了,就更新答案 当加入一条边,会形成环. 贪心地想,我们要最大边权-最小边 ...
- Luogu 4234 最小差值生成树 - LCT 维护链信息
Solution 将边从小到大排序, 添新边$(u, v)$时 若$u,v$不连通则直接添, 若连通则 把链上最小的边去掉 再添边. 若已经加入了 $N - 1$条边则更新答案. Code #incl ...
- luogu 4234 最小差值生成树 LCT
感觉码力严重下降~ #include <bits/stdc++.h> #define N 400006 #define inf 1000000000 #define setIO(s) fr ...
- P4234 最小差值生成树
题目 P4234 最小差值生成树 做法 和这题解法差不多,稍微变了一点,还不懂就直接看代码吧 \(update(2019.2):\)还是具体说一下吧,排序,直接加入,到了成环情况下,显然我们要把此边代 ...
- 洛谷.4234.最小差值生成树(LCT)
题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树 ...
- 洛谷P4234 最小差值生成树(lct动态维护最小生成树)
题目描述 给定一个标号为从 11 到 nn 的.有 mm 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式: 第一行两个数 n, mn,m ,表示图的点和边的数量. ...
- 洛谷P4234 最小差值生成树(LCT,生成树)
洛谷题目传送门 和魔法森林有点像,都是动态维护最小生成树(可参考一下Blog的LCT总结相关部分) 至于从小到大还是从大到小当然无所谓啦,我是从小到大排序,每次枚举边,还没连通就连,已连通就替换环上最 ...
随机推荐
- Python Select模型
IO多路复用 IO多路复用就是我们经常说的select epoll.select和epoll的好处是单个process就可以同时处理多个网络IO.基本原理是select\epoll会不断的轮询所负责的 ...
- RuntimeError: cryptography is required for sha256_password or caching_sha2_p
报错原因:mysql版本身份验证出现问题引起的 我这里报错的地方是在Django里,pycharm连接数据库时出现的 解决办法,安装安装cryptography即可:pip install crypt ...
- cocos2dx for lua A*寻路算法实现2
关于A*算法的实现过程,简单来说就是一个计算权限的过程. 首先,创建一个地图节点类,"MapNode.lua" local MapNode = class("MapNod ...
- on() 和 click() 的区别
on() 和 click() 的区别: 二者在绑定静态控件时没有区别,但是如果面对动态产生的控件,只有 on() 能成功的绑定到动态控件中. 以下实例中原先的 HTML 元素点击其身后的 Delete ...
- 【树形dp】bzoj4726: [POI2017]Sabota?
找点概率期望的题做一做 Description 某个公司有n个人, 上下级关系构成了一个有根树.其中有个人是叛徒(这个人不知道是谁).对于一个人, 如果他 下属(直接或者间接, 不包括他自己)中叛徒占 ...
- JS - 生成UUID
function uuid(len, radix) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw ...
- pm2日志记录和日志分割
pm2日志记录和日志分割 pm2介绍 pm2是nodejs进程管理工具,现在基本是node生产服务器的标准选择,可以帮助我们实现node多进程服务,开启的多个实例自动实现负载均衡. 最重要的是保证no ...
- NoSQL 数据库之MongoDB
1.MongoDB简介 1.1什么是MongoDB MongoDB 是一个跨平台的,面向文档的数据库,是当前 NoSQL 数据库产品中最热门的一种.它介于关系数据库和非关系数据库之间,是非关系数据库当 ...
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- Linux学习-循环执行的例行性工作排程
循环执行的例行性工作排程则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的例行性工作,因此这个系统服务是默认启动的. 另外, 由于使用者自己也可以进行例行性工 ...