cf609E Minimum Spanning Tree For Each Edge (kruskal+倍增Lca)
先kruskal求出一个最小生成树,然后对于每条非树边(a,b),从树上找a到b路径上最大的边,来把它替换掉,就是包含这条边的最小生成树
#include<bits/stdc++.h>
#define pa pair<int,int>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=2e5+; inline ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} struct Edge{
int a,b,l,ne;
}eg[maxn*],eg0[maxn];
int egh[maxn],ect;
int N,M;
int fa[maxn],f[maxn][],ma[maxn][],dep[maxn];
ll ans[maxn]; inline void adeg(int a,int b,int c){
eg[++ect].b=b;eg[ect].l=c;eg[ect].ne=egh[a];egh[a]=ect;
} inline bool cmp(Edge a,Edge b){return a.l<b.l;}
int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);} void dfs(int x){
for(int i=;f[x][i]&&f[f[x][i]][i];i++){
f[x][i+]=f[f[x][i]][i];
ma[x][i+]=max(ma[x][i],ma[f[x][i]][i]);
}
for(int i=egh[x];i;i=eg[i].ne){
int b=eg[i].b;
if(b==f[x][]) continue;
dep[b]=dep[x]+;
f[b][]=x;ma[b][]=eg[i].l;
dfs(b);
}
} ll get(int x,int y){
int re=;
if(dep[x]<dep[y]) swap(x,y);
for(int i=log2(dep[x]-dep[y]);i>=&&dep[x]!=dep[y];i--){
if(dep[f[x][i]]>=dep[y])
re=max(re,ma[x][i]),x=f[x][i];
}
if(x==y) return re;
for(int i=log2(dep[x]);i>=;i--){
if(f[x][i]!=f[y][i])
re=max(re,max(ma[y][i],ma[x][i])),x=f[x][i],y=f[y][i];
}
return max(re,max(ma[y][],ma[x][]));
} int main(){
//freopen("","r",stdin);
int i,j,k;
N=rd(),M=rd();
for(i=;i<=M;i++){
eg0[i].a=rd(),eg0[i].b=rd(),eg0[i].l=rd();
eg0[i].ne=i;
}sort(eg0+,eg0+M+,cmp);
ll dis=;
for(i=;i<=N;i++) fa[i]=i;
for(i=,j=;i<=M&&j<N-;i++){
int aa=getf(eg0[i].a),bb=getf(eg0[i].b);
if(aa!=bb){
adeg(eg0[i].a,eg0[i].b,eg0[i].l);
adeg(eg0[i].b,eg0[i].a,eg0[i].l);
dis+=eg0[i].l;
fa[aa]=bb;j++;
}
}
dep[]=;dfs();
for(i=;i<=M;i++){
ans[eg0[i].ne]=dis+eg0[i].l-get(eg0[i].a,eg0[i].b);
}
for(i=;i<=M;i++){
printf("%I64d\n",ans[i]);
}
return ;
}
cf609E Minimum Spanning Tree For Each Edge (kruskal+倍增Lca)的更多相关文章
- Minimum spanning tree for each edge(倍增LCA)
https://vjudge.net/contest/320992#problem/J 暑期训练的题. 题意:给你一个n个点,m条边的无向图.对于每一条边,求包括该边的最小生成树. 思路:首先想到求一 ...
- CF609E Minimum spanning tree for each edge
原来觉得是一个LCT,感觉自己瞬间傻掉…… 考虑到先做一个最小生成树求出做最小生成树的代价$ans$,顺便标记一下树边和非树边,把边按照输入$id$排序回去之后扫,如果扫到一条树边,那么此时的答案就是 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- codeforces 609E Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
随机推荐
- Http指南(1)
网关:是一种特殊的服务器,作为其他服务器的中间实体使用; Agent代理:所有发布web请求的应用程序都是HTTP Agent代理.Web浏览器其实就是一种代理; HTTP报文是在HTTP应用程序之间 ...
- 虚拟机console基础环境配置——系统镜像站点配置
1. 概述2. 部署HTTP服务器2.1 YUM安装httpd2.2 配置httpd2.3 启动httpdf2.4 测试httpd3. 部署FTP服务器3.1 YUM安装vsftpd3.2 配置vsf ...
- MySQL针对Swap分区的运维注意点
Linux有很多很好的内存.IO调度机制,但是并不会适用于所有场景.对于运维人员来说,Linux比较让人头疼的一个地方是:它不会因为MySQL很重要就避免将分配给MySQL的地址空间映射到swap上. ...
- Linux内核分析 读书笔记 (第四章)
第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间.进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有通过调度程序的合理调度,系统资源才能最大限 ...
- 20135327郭皓--Linux内核分析第六周 进程的描述和进程的创建
进程的描述和进程的创建 一.进程的描述 操作系统三大功能: 进程管理 内存管理 文件系统 进程描述符task_struct数据结构 task _ struct:为了管理进程,内核必须对每个进程进行清晰 ...
- answer my questions from the book<构建之法>.
1)何为文档:文档时在一个项目进行的一生中所有记忆的集合.有需求分析.功能设计.在实现功能过程中也可以有一系列文档记录.测试文档等等. 2)结对工作等找队友会花费大量时间致耽误项目否:正如老师所讲,从 ...
- Alpha冲刺之事后诸葛亮
组长博客 作业博客 项目Postmortem 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件针对的是福大学子来到食堂会犹豫不决无法决定吃什么 ...
- np.array与np.ndarray区别
(Numpy中ndarray和array的区别是什么?我在哪儿能够找到numpy中相应的实现?) 答:Well, np.array is just a convenience function to ...
- [读书笔记]Linux命令行与shell编程读书笔记04 安装软件,编辑器注意事项
1. debian以及redhat两种主流的linux发行版用的包管理工具 debian的包管理工具是 dpkg 再现安装的是 apt apt的工具主要有 apt-get apt-cache apti ...
- 解决vmware与主机无法连通的问题
我们选择NAT方式,来实现Ubuntu的静态IP地址配置. 打开VMware,在顶部依次选择:编辑 > 虚拟网路编辑器,打开虚拟网路编辑器:去掉VMnet0和VMnet1,只保留VMnet8.然 ...