luogu 4234 最小差值生成树 LCT
感觉码力严重下降~
#include <bits/stdc++.h>
#define N 400006
#define inf 1000000000
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
multiset<int>S;
multiset<int>::iterator it;
struct Edge
{
int u,v,c;
Edge(int u=0,int v=0,int c=0):u(u),v(v),c(c){}
}e[N];
bool cmp(Edge a,Edge b)
{
return a.c<b.c;
}
struct Union
{
int p[N];
void init()
{
for(int i=1;i<N;++i) p[i]=i;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
int merge(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)
{
p[x]=y;
return 1;
}
return 0;
}
}ufs;
struct Link_Cut_Tree
{
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int sta[N];
struct Node
{
int ch[2],f,min,id,val,rev;
}t[N];
int isrt(int x)
{
return !(t[t[x].f].ch[0]==x||t[t[x].f].ch[1]==x);
}
int get(int x)
{
return t[t[x].f].ch[1]==x;
}
void pushup(int x)
{
t[x].min=t[x].val, t[x].id=x;
if(lson && t[lson].min<t[x].min) t[x].min=t[lson].min,t[x].id=t[lson].id;
if(rson && t[rson].min<t[x].min) t[x].min=t[rson].min,t[x].id=t[rson].id;
}
void mark(int x)
{
if(x) t[x].rev^=1,swap(lson,rson);
}
void pushdown(int x)
{
if(t[x].rev)
{
if(lson) mark(lson);
if(rson) mark(rson);
t[x].rev=0;
}
}
void rotate(int x)
{
int old=t[x].f,fold=t[old].f,which=get(x);
if(!isrt(old)) t[fold].ch[t[fold].ch[1]==old]=x;
t[old].ch[which]=t[x].ch[which^1], t[t[old].ch[which]].f=old;
t[x].ch[which^1]=old,t[old].f=x,t[x].f=fold;
pushup(old),pushup(x);
}
void splay(int x)
{
int v=0,u=x,fa;
for(sta[++v]=u;!isrt(u);u=t[u].f) sta[++v]=t[u].f;
for(;v;--v) pushdown(sta[v]);
for(u=t[u].f;(fa=t[x].f)!=u;rotate(x))
if(t[fa].f!=u)
rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=t[x].f)
splay(x),rson=y,pushup(x);
}
void makeroot(int x)
{
Access(x),splay(x),mark(x);
}
void link(int x,int y)
{
makeroot(x),t[x].f=y;
}
void cut(int x,int y)
{
makeroot(x),Access(y),splay(y);
t[t[y].ch[0]].f=0;
t[y].ch[0]=0;
pushup(y);
}
void split(int x,int y)
{
makeroot(x),Access(y),splay(y);
}
#undef lson
#undef rson
}op;
int main()
{
int i,j,n,m,ans=inf;
// setIO("input");
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
sort(e+1,e+1+m,cmp);
ufs.init();
for(i=1;i<=n;++i) op.t[i].val=inf;
for(i=1;i<=m;++i)
{
int x=e[i].u,y=e[i].v,c=e[i].c,_new=i+n;
if(x==y) continue;
if(ufs.merge(x,y))
{
op.t[_new].val=c;
op.link(x,_new);
op.link(_new,y);
S.insert(c);
}
else
{
op.split(x,y);
if(op.t[y].min<c)
{
S.erase(S.find(op.t[y].min));
S.insert(c);
int kk=op.t[y].id;
int xx=e[kk-n].u;
int yy=e[kk-n].v;
op.cut(xx,kk);
op.cut(yy,kk);
op.t[_new].val=c;
op.link(x,_new);
op.link(_new,y);
}
}
if(S.size()>=n-1)
{
it=S.end();
it--;
ans=min(ans,*(it)-*(S.begin()));
}
}
printf("%d\n",ans);
return 0;
}
luogu 4234 最小差值生成树 LCT的更多相关文章
- Luogu 4234 最小差值生成树 - LCT 维护链信息
Solution 将边从小到大排序, 添新边$(u, v)$时 若$u,v$不连通则直接添, 若连通则 把链上最小的边去掉 再添边. 若已经加入了 $N - 1$条边则更新答案. Code #incl ...
- 洛谷.4234.最小差值生成树(LCT)
题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树 ...
- 洛谷4234最小差值生成树 (LCT维护生成树)
这也是一道LCT维护生成树的题. 那么我们还是按照套路,先对边进行排序,然后顺次加入. 不过和别的题有所不同的是: 在本题中,我们需要保证LCT中正好有\(n-1\)条边的时候,才能更新\(ans\) ...
- 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)总结 考虑先按边权排序,从小到大加边 如果构成一颗树了,就更新答案 当加入一条边,会形成环. 贪心地想,我们要最大边权-最小边 ...
- [luogu4234]最小差值生成树
[luogu4234]最小差值生成树 luogu 从小到大枚举边,并连接,如果已连通就删掉路径上最小边 lct维护 \(ans=min(E_{max}-E_{min})\) #include<b ...
- LuoguP4234_最小差值生成树_LCT
LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...
- P4234 最小差值生成树
题目 P4234 最小差值生成树 做法 和这题解法差不多,稍微变了一点,还不懂就直接看代码吧 \(update(2019.2):\)还是具体说一下吧,排序,直接加入,到了成环情况下,显然我们要把此边代 ...
随机推荐
- 护卫神等IIS设置Thinkphp框架的public目录为根目录的解决办法
最近碰到一个棘手的问题,在使用护卫神或者主机宝等IIS环境配置PHP的时候,不能把public设置为网站根目录(因为Thinkphp的安全要求:要将public设置为对外公开目录),这个问题无法搜索到 ...
- not or and 的优先级是不同的
not or and 的优先级是不同的: not > and > or 请用最快速度说出答案: not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 an ...
- 怎样快捷获取网页的window对象
使用document.defaultView; document.defaultView === window 注意: 1. 如果当前文档不属于window对象, 则返回null; 2. docume ...
- el-table el-column selection disable
几个要点: 1.通过 selectable 绑定 2.绑定的方法只能返回0/1 <el-table-column type="selection" width="5 ...
- BASE64 Encode Decode
package com.humi.encryption; import java.io.IOException; import java.io.UnsupportedEncodingException ...
- gitlab 搭建
一.ubuntu搭建gitlab 1. 如果以前有安装过gitlab请根据以下步骤来删除 https://www.cnblogs.com/shansongxian/p/6678110.htm ...
- fetch---基本使用
一.fetch fetch是一种XMLHttpRequest的一种替代方案,在工作当中除了用ajax获取后台数据外我们还可以使用fetch.axios来替代ajax 二.安装 执行npm instal ...
- Android项目笔记整理(1)
第二部分 工作项目中以及平时看视频.看书或者看博客时整理的个人觉得挺有用的笔记 1.Activity界面切换: if(条件1){ setContentView(R.layout.ma ...
- MySQL存储引擎MyISAM和InnoDB,索引结构优缺点
MySQL存储引擎MyISAM和InnoDB底层索引结构 深入理解MySQL索引底层数据结构与算法 (各种索引结构优缺点) Myisam和Innodb索引实现的不同(存储结构) 存储引擎作用于什么对象 ...
- Java在常见的spring面试问题TOP50
1. 一般问题 1.1. 不同版本的 Spring Framework 有哪些主要功能? Version Feature Spring 2.5 发布于 2007 年.这是第一个支持注解的版本. Spr ...