【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总结相关部分) 至于从小到大还是从大到小当然无所谓啦,我是从小到大排序,每次枚举边,还没连通就连,已连通就替换环上最 ...
随机推荐
- 51nod 1101 换零钱
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元. ...
- SAP Cloud for Customer客户主数据的地图集成
点击这个按钮可以通过地图的方式查看C4C客户在地图上的地理位置: 只需要在这个客户的地址栏里维护上天府软件园的经度和维度: 就能够在C4C的客户列表页面里显示该客户在地图上的位置: 要获取更多Jerr ...
- Aho-Corasick自动机
在模式匹配问题中,如果模板有很多个,KMP算法就不太适合了.因为每次查找一个模板.都要遍历整个文本串.可不可以只遍历一次文本串呢?可以,方法是把所有模板组成一个大的状态转移图(称为$Aho-Coras ...
- C#之winform实现文件拖拽功能【转】
将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...
- 在DataGridView控件中显示下拉列表
实现效果: 知识运用: DataGridViewComboBoxColumn类 //通过该类可以创建下拉列表样式的列 实现代码: private void Form1_Load(object send ...
- C++ NULL与nullptr的区别
C与C++中空指针的区别 在C里面,由于处处都要使用指针,所以导致NULL遍布各地.我们先来看C99是怎么定义NULL的: NULL can be defined as any null pointe ...
- 读取Exchange的用户未读邮件数的几种方法
[http://www.cnblogs.com/nbpowerboy/p/3539422.html] 可以使用ExchangeServiceBinding获取邮件,他相当于outlook, 来获取服务 ...
- NOIP模拟赛 密室逃脱
密室逃脱(maze.*) 即使czhou没有派出最强篮球阵容,机房篮球队还是暴虐了校篮球队.为了不打击校篮球队信心,czhou决定改变训练后的活动.近来,江大掌门的徒弟徒孙们纷纷事业有成,回到母校为机 ...
- NowCoder 9.9 模拟赛
T1.中位数 二分答案x,原序列大于x的置为1,小于x的置为-1,判断是否存在长度大于m的区间和大于0(也就是大于x的数多于小于x的数),若有,则ans>=x,否则ans<x #inclu ...
- [LUOGU] 4149 [IOI2011]Race
点分治裸题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; i ...