题目大意:给出一个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 ;
}

最新文章

  1. PRINCE2
  2. Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析
  3. 启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required
  4. sublime 自动编译
  5. ios项目绕过证书访问https程序
  6. JavaScript中关于创建对象的笔记
  7. C# 语言规范_版本5.0 (第6章 转换)
  8. android checkbox 未选中状态 已选中状态 替换成自己的图片
  9. Java静态代码块、构造代码块执行顺序问题
  10. es6的理解
  11. centos7,zabbix3.2通过zabbix_java_gateway监控jmx[java/tomcat]
  12. XXX银行项目部署
  13. from…import 语句
  14. Jmeter(四)NO-GUI模式运行
  15. tkinter widget
  16. AMQP协议与RabbitMQ、MQ消息队列的应用场景
  17. paxos协议
  18. 【 js 基础 】【读书笔记】关于this
  19. [leetcode] 14. Climbing Stairs
  20. python 基础篇01

热门文章

  1. 互联网大规模数据分析技术(自主模式)第五章 大数据平台与技术 第10讲 大数据处理平台Hadoop
  2. centOS系统安装MySQL教程
  3. jmeter-plugins-dubbo &amp; DevToolBox
  4. 关于super关键字
  5. 在linux下设置定时任务
  6. IPMI命令
  7. 1、概率vs统计
  8. kubernetes使用ceph
  9. TinyMCE3.x整合教程-Xproer.WordPaster
  10. 机器学习及其matlab实现—从基础到实践