POJ 3522 最小差值生成树(LCT)
题目大意:给出一个n个节点的图,求最大边权值减去最小边权值最小的生成树。
题解
Flash Hu大佬一如既往地强
先把边从小到大排序
然后依次加入每一条边
如果已经连通就把路径上权值最小的边删去
然后记得更新答案
ps:不是很明白为啥我洛谷上吸了氧还跑得更慢了233
//minamoto
#include<cstdio>
#include<algorithm>
#include<iostream>
#define inf 0x3f3f3f3f
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=,M=,K=N+M;
struct edge{
int u,v,e;
inline bool operator <(const edge &b)const
{return e<b.e;}
}e[M];
int fa[K],ch[K][],s[K],mn[K],rev[K],top,v[K],vis[M],f[N];
int ff(int x){return f[x]==x?x:f[x]=ff(f[x]);}
inline bool isroot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline int get(int x,int y){return v[x]<v[y]?x:y;}
inline void pushup(int x){mn[x]=get(x,get(mn[ch[x][]],mn[ch[x][]]));}
inline void pushdown(int x){
if(x&&rev[x]){
swap(ch[x][],ch[x][]);
rev[ch[x][]]^=,rev[ch[x][]]^=;
rev[x]=;
}
}
void rotate(int x){
int y=fa[x],z=fa[y],d=ch[y][]==x;
if(!isroot(y)) ch[z][ch[z][]==y]=x;
fa[x]=z,fa[y]=x,fa[ch[x][d^]]=y,ch[y][d]=ch[x][d^],ch[x][d^]=y,pushup(y);
}
void splay(int x){
s[top=]=x;for(int i=x;!isroot(i);i=fa[i]) s[++top]=fa[i];
while(top) pushdown(s[top--]);
for(int y=fa[x],z=fa[y];!isroot(x);y=fa[x],z=fa[y]){
if(!isroot(y))
((ch[y][]==x)^(ch[z][]==y))?rotate(x):rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x){
for(int y=;x;x=fa[y=x])
splay(x),ch[x][]=y,pushup(x);
}
inline void makeroot(int x){
access(x),splay(x),rev[x]^=;
}
inline void link(int x){
makeroot(e[x].u);
fa[fa[e[x].u]=N+x]=e[x].v;
}
inline void cut(int x){
access(e[x-N].u),splay(x);
ch[x][]=ch[x][]=fa[ch[x][]]=fa[ch[x][]]=;
/*因为link的时候把u的父亲设为边,所以u的深度最低
把它到根节点的路径打通就好了*/
}
int main(){
//freopen("testdata.in","r",stdin);
int n=read(),m=read();
int ans=;
for(int i=;i<=n;++i) f[i]=i,v[i]=inf;
for(int i=;i<=m;++i){
int u=read(),v=read(),k=read();
e[i]=(edge){u,v,k};
}
sort(e+,e++m);
for(int cnt=,h=,i=;i<=m;++i){
v[i+N]=e[i].e;
int u,v;
if(ff(u=e[i].u)!=ff(v=e[i].v)){
vis[i]=,link(i),f[f[u]]=f[v],++cnt;
if(cnt==n) ans=e[i].e-e[h].e;
}
else{
if(u==v) continue;
vis[i]=;
makeroot(u),access(v),splay(v);
vis[mn[v]-N]=;while(!vis[h]) ++h;
cut(mn[v]),link(i);
if(cnt==n) cmin(ans,e[i].e-e[h].e);
}
}
printf("%d\n",ans);
return ;
}
POJ 3522 最小差值生成树(LCT)的更多相关文章
- 洛谷.4234.最小差值生成树(LCT)
题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树 ...
- P4234 最小差值生成树 LCT维护边权
\(\color{#0066ff}{ 题目描述 }\) 给定一个标号为从 \(1\) 到 \(n\) 的.有 \(m\) 条边的无向图,求边权最大值与最小值的差值最小的生成树. \(\color{#0 ...
- Luogu 4234 最小差值生成树 - LCT 维护链信息
Solution 将边从小到大排序, 添新边$(u, v)$时 若$u,v$不连通则直接添, 若连通则 把链上最小的边去掉 再添边. 若已经加入了 $N - 1$条边则更新答案. Code #incl ...
- 洛谷 P4234 最小差值生成树(LCT)
题面 luogu 题解 LCT 动态树Link-cut tree(LCT)总结 考虑先按边权排序,从小到大加边 如果构成一颗树了,就更新答案 当加入一条边,会形成环. 贪心地想,我们要最大边权-最小边 ...
- luogu 4234 最小差值生成树 LCT
感觉码力严重下降~ #include <bits/stdc++.h> #define N 400006 #define inf 1000000000 #define setIO(s) fr ...
- LuoguP4234_最小差值生成树_LCT
LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...
- [luogu4234]最小差值生成树
[luogu4234]最小差值生成树 luogu 从小到大枚举边,并连接,如果已连通就删掉路径上最小边 lct维护 \(ans=min(E_{max}-E_{min})\) #include<b ...
- P4234 最小差值生成树
题目 P4234 最小差值生成树 做法 和这题解法差不多,稍微变了一点,还不懂就直接看代码吧 \(update(2019.2):\)还是具体说一下吧,排序,直接加入,到了成环情况下,显然我们要把此边代 ...
- POJ 3522 Slim Span 最小差值生成树
Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Gi ...
随机推荐
- go_数组
go语言中数组是值类型 [10]int 和 [20]int 是不同类型,不能用作参数传递 调用func f(arr [10]int)会拷贝数组 go语言一般不用数组用切片slice package m ...
- 657. Judge Route Circle机器人能否返回
[抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...
- 669. Trim a Binary Search Tree修剪二叉搜索树
[抄题]: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so ...
- 深入理解mysql的隔离级别
建表插入测试数据A> create table test(id int ,num int) ;Query OK, 0 rows affected (0.53 sec) A> insert ...
- 在centos6.3_64bit 上的GO语言开发环境搭建
1.下载go安装包 http://golang.org/ go1.2.linux-amd64.tar.gz 2.配置环境变量 3.编写helloworld package main import ...
- mybatis思维导图(二)
写在前面 上一篇文章写了mybatis的基本原理和配置文件的基本使用,这一篇写mybatis的使用,主要包括与sping集成.动态sql.还有mapper的xml文件一下复杂配置等.值得注意的是,导图 ...
- dbcp的销毁
使用commons-dbcp-1.2.2.jar的DataSource,发现每次动态编译后连接池中的连接不会释放,新的连接池建立有mssql多出一组连接,只有重新启动tomcat或weblogic才可 ...
- SQL的Join语法
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- Memcache+Cookie替代Session解决方案(MVC版)
阅读目录 开始 通过IHttpModule注册过滤管道方式 通过BaseController 关于滑动过期 两种方式 回到顶部 通过IHttpModule注册过滤管道方式 具体实现如下: 声明一个类C ...
- 设计模式15---Android 观察者模式(转载自:“http://blog.csdn.net/fangchongbory/article/details/7774044”)
/* * 观察者模式 * 定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...