先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)的更多相关文章

  1. Minimum spanning tree for each edge(倍增LCA)

    https://vjudge.net/contest/320992#problem/J 暑期训练的题. 题意:给你一个n个点,m条边的无向图.对于每一条边,求包括该边的最小生成树. 思路:首先想到求一 ...

  2. CF609E Minimum spanning tree for each edge

    原来觉得是一个LCT,感觉自己瞬间傻掉…… 考虑到先做一个最小生成树求出做最小生成树的代价$ans$,顺便标记一下树边和非树边,把边按照输入$id$排序回去之后扫,如果扫到一条树边,那么此时的答案就是 ...

  3. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 线上分享-- 基于DDD的.NET开发框架-ABP介绍

    前言 为了能够帮助.Net开发者开拓视野,更好的把最新的技术应用到工作中,我在3月底受邀到如鹏网.net训练营直播间为各位学弟学妹们进行ABP框架的直播分享.同时为了让更多的.NET开发者了解ABP框 ...

  2. B. Views Matter

    链接 [http://codeforces.com/contest/1061/problem/B] 题意 问你最多去掉多少块使得从上和右看,投影图不变 分析 注意细节,尤其第一列 代码 #includ ...

  3. 美食应用 吃了么 beta 测试报告

    为了更好的测试我们应用的兼容性和性能,我们借助了网上的平台Testin云测和百度MTC平台来测试我们的应用,一下是我们的测试结果. 一.兼容性测试 我们对119台终端机器进行了测试,通过测试的有99台 ...

  4. 第六周分析Linux内核创建一个新进程的过程

    潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...

  5. 去掉ambiguous expansion of macro警告

    查看原文:http://www.heyuan110.com/?p=1221 用pod install后,pod工程里出现ambiguous expansion of macro的warning,对于有 ...

  6. [问题解决]基于注解配置dubbo遇到ConnectionLoss for /dubbo/xxx问题解决

    今天升级spring版本的时候,同时升级dubbo的版本,采用的是dubbo的基于注解的配置方法,采用curator作为dubbo的客户端, curator版本为4.1.0,启动之后,发现一直报错 C ...

  7. 同步或者重构Activiti Identify用户数据的多种方案比较

    http://www.kafeitu.me/activiti/2012/04/23/synchronize-or-redesign-user-and-role-for-activiti.html 如何 ...

  8. Java 修饰符顺序问题

    What is a reasonable order of Java modifiers (abstract, final, public, static, etc.)? http://stackov ...

  9. 安装wamp提示You dont't have permission to accesson on this server的解决方案

    展示一下安装好的效果图 首先找到安装目录下的路径[wamp\bin\apache\Apache2.2.21\conf\] § 找到httpd.conf,用记事本打开httpd.conf,然后将 1. ...

  10. Android控件第5类——ViewAnimator

    1.ViewAnimator,继承自FrameLayout ViewAnimator是一个基类,它继承自FrameLayout.它的子类有ViewSwitcher和ViewFlipper:ViewSw ...