首次采用了压行,感觉还不错。

Code:

// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
void setIO(string a){
freopen((a+".in").c_str(),"r",stdin);
//freopen((a+".out").c_str(),"w",stdout);
}
#define inf 1000000000
#define maxn 300007
int ch[maxn][2],f[maxn],val[maxn],max1[maxn],max2[maxn];
int sta[maxn],tag[maxn];
int lson(int x){ return ch[x][0]; }
int rson(int x){ return ch[x][1]; }
int get(int x){ return ch[f[x]][1]==x; }
int isRoot(int x){ return !(ch[f[x]][0]==x||ch[f[x]][1]==x); }
void mark(int x){ if(x) swap(ch[x][0],ch[x][1]),tag[x]^=1; }
void pushdown(int x){ if(tag[x]) mark(lson(x)), mark(rson(x)), tag[x]=0; }
void pushup(int x){
if(max1[lson(x)]>max1[rson(x)]) max1[x]=max1[lson(x)], max2[x]=max(max2[lson(x)],max1[rson(x)]);
else if(max1[lson(x)]<max1[rson(x)]) max1[x]=max1[rson(x)], max2[x]=max(max2[rson(x)],max1[lson(x)]);
else max1[x]=max1[lson(x)], max2[x]=max(max2[lson(x)],max2[rson(x)]); if(val[x]>max1[x]) max2[x]=max1[x],max1[x]=val[x];
else if(val[x]!=max1[x]) max2[x]=max(max2[x],val[x]);
} void rotate(int x) {
int old=f[x],oldf=f[old],which=get(x);
if(!isRoot(old)) ch[oldf][ch[oldf][1]==old]=x;
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=oldf;
pushup(old),pushup(x);
} void splay(int x){
int v=0,u=x;
sta[++v]=u;
while(!isRoot(u)) sta[++v]=f[u],u=f[u];
while(v) pushdown(sta[v--]);
u=f[u];
for(int fa;(fa=f[x])!=u;rotate(x))
if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
} void Access(int x){ for(int y=0;x;y=x,x=f[x]) splay(x),ch[x][1]=y,pushup(x); }
void makeRoot(int x){ Access(x),splay(x),mark(x); }
void split(int x,int y){ makeRoot(x),Access(y),splay(y); }
struct Union_Find{
int p[maxn];
int find(int x){ return p[x]==x?x:p[x]=find(p[x]);}
void init(){for(int i=1;i<maxn;++i)p[i]=i;}
void merge(int x,int y){ int a=find(x),b=find(y); if(a!=b) p[a]=b; }
}tree;
struct Edge{
int u,v,c;
Edge(int u=0,int v=0,int c=0):u(u),v(v),c(c){}
}edges[maxn];
bool cmp(Edge e1,Edge e2){ return e1.c<e2.c; }
int main(){
//setIO("input");
tree.init();
int n,m,cnt,x,y,ans=inf;
long long sum=0;
scanf("%d%d",&n,&m),cnt=n;
for(int i=1;i<=m;++i) scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].c);
sort(edges+1,edges+1+m,cmp);
for(int i=1;i<=m;++i){
x=edges[i].u,y=edges[i].v;
if(tree.find(x)==tree.find(y)){
split(x,y);
ans=min(ans,edges[i].c-(max1[y]>=edges[i].c?max2[y]:max1[y]));
}
else{
makeRoot(x);
f[f[x]=++cnt]=y;
max1[cnt]=val[cnt]=edges[i].c;
sum+=edges[i].c;
tree.merge(x,y);
}
}
printf("%lld",sum+ans);
return 0;
}

  

最新文章

  1. Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。
  2. JS 格式化当前时间
  3. 转载:java 中对类中的属性使用set/get方法的意义和用法
  4. 微信网站设置右上角发送、分享的内容——.net版本
  5. MAC下搭建web开发环境
  6. 使用gulp、yeoman、bower建站
  7. 深入理解JVM--类的执行机制
  8. valgrind用于检测内存泄露
  9. android sdk 下载缓慢的问题
  10. C# mongodb 1
  11. Chapter 14_4 使用_ENV
  12. Spring Boot 学习笔记--整合Redis
  13. POJ1850-Code 递推数学
  14. 017 多对多关联映射 双向(many-to-many)
  15. RPC服务和HTTP服务
  16. 使用CocoaPods创建Pod
  17. python验证码简单识别
  18. Servlet实践--HelloWorld
  19. hibernate入门一
  20. Java常见的几种内存溢出及解决方案

热门文章

  1. 子文件夹的遍历(python、matlab)
  2. window如何在命令行关掉端口
  3. SPOJ 694/705 后缀数组
  4. .NET框架详解
  5. DBMS_XPLAN详细说明
  6. Codeforces 986B. Petr and Permutations(没想到这道2250分的题这么简单,早知道就先做了)
  7. 新疆大学(新大)OJ xju 1009: 一带一路 prim求最短路径+O(n)素数筛选
  8. hdu1045 - 贪心,二分图
  9. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
  10. DNS BIND之dnssec安全介绍