E. Minimum spanning tree for each edge

题目连接:

http://www.codeforces.com/contest/609/problem/E

Description

Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.

For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v).

The weight of the spanning tree is the sum of weights of all edges included in spanning tree.

Input

First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph.

Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.

Output

Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge.

The edges are numbered from 1 to m in order of their appearing in input.

Sample Input

5 7

1 2 3

1 3 1

1 4 5

2 3 2

2 5 3

3 4 2

4 5 4

Sample Output

9

8

11

8

8

8

9

Hint

题意

给你一个图,n点m边。对于每个边,问你包含这条边的最小生成树是多少。

题解:

先生成一个最小生成树,加入一条边,可能会产生一个环,那么求这个环的最小值即可,

这个用倍增就行,就和求次小生成树一模一样。

今天typora终于可以用搜狗输入法了,我发现终端打开都用不了搜狗输入法,真奇怪呀。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 400050
ll n,m;
ll dp[N],mm[N],fu[N][21],mx[N][21];
ll tot,last[N];
struct Edge
{
ll from,to,val,s;
bool operator < (const Edge&b)
{return val<b.val;}
}a[N],edges[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(ll x,ll y,ll z)
{
edges[++tot]=Edge{x,y,z,last[x]};
last[x]=tot;
}
ll gf(ll x,ll *f)
{
if (x==f[x])return x;
return f[x]=gf(f[x],f);
}
ll MST(Edge *edges)
{
static ll f[N]; static Edge a[N];
for(ll i=1;i<=m;i++)a[i]=edges[i];
ll num=0,sum=0;
sort(a+1,a+m+1);
for(ll i=1;i<=n;i++)f[i]=i;
for(ll i=1;i<=m;i++)
{
Edge e=a[i];//
ll fx=gf(e.from,f),fy=gf(e.to,f);
if (fx!=fy)//
{
f[fx]=fy;
num++;
sum+=e.val;
AddEdge(e.to,e.from,e.val);
AddEdge(e.from,e.to,e.val);
}
if (num==n-1)break;
}
return sum;
}
void dfs(ll x,ll pre)
{
dp[x]=dp[pre]+1;
fu[x][0]=pre;
for(ll i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
mx[e.to][0]=e.val;
dfs(e.to,x);
}
}
void init_ST(ll n)
{
mm[0]=-1;
for(ll i=1;i<=n;i++) mm[i]=(i&(i-1))==0?mm[i-1]+1:mm[i-1];
for(ll i=1;i<=20;i++)
for(ll j=1;j<=n;j++)
{
fu[j][i]=fu[fu[j][i-1]][i-1];
mx[j][i]=max(mx[j][i-1],mx[fu[j][i-1]][i-1]);
}
}
ll get_max(ll x,ll y)
{
ll ans=0;
if (dp[x]<dp[y])swap(x,y);
for(ll i=mm[dp[x]-dp[y]];i>=0;i--)
if (dp[fu[x][i]]>=dp[y])
{
ans=max(ans,mx[x][i]);
x=fu[x][i];
}
if (x==y)return ans;
for(ll i=mm[dp[x]-1];i>=0;i--)
if (fu[x][i]!=fu[y][i])
{
ans=max(ans,mx[x][i]);
ans=max(ans,mx[y][i]);
x=fu[x][i];
y=fu[y][i];
}
ans=max(ans,mx[x][0]);
ans=max(ans,mx[y][0]);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n); read(m);
for(ll i=1;i<=m;i++)
{
ll x,y,z;
read(x); read(y); read(z);
a[i]=Edge{x,y,z,0};
}
ll sum=MST(a);
dfs(1,0);
init_ST(n);
for(ll i=1;i<=m;i++)
{
ll ans=sum-get_max(a[i].from,a[i].to)+a[i].val;
printf("%lld\n",ans);
}
}

Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增的更多相关文章

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

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

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

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

  5. CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种

    题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...

  6. Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)

    题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...

  7. cf609E Minimum Spanning Tree For Each Edge (kruskal+倍增Lca)

    先kruskal求出一个最小生成树,然后对于每条非树边(a,b),从树上找a到b路径上最大的边,来把它替换掉,就是包含这条边的最小生成树 #include<bits/stdc++.h> # ...

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

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

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

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

随机推荐

  1. jQuery系列(十四):jQuery中的ajax

    1.什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascript and XML) 简言之,在不重载整个网页的情况下,AJAX通过后台加载数据,并在 ...

  2. Appium基础教程

    目录 Appium教程 Appium简介 App自动化测试工具对比 Appium实现原理 环境搭建 Andorid介绍 基本架构 常见布局/视图 基本控件 控件常见属性 Adb介绍 Adb常用命令 A ...

  3. javascript数组的增删改和查询

    数组的增删改操作 对数组的增删改操作进行总结,下面(一,二,三)是对数组的增加,修改,删除操作都会改变原来的数组. (一)增加 向末尾增加 push() 返回新增后的数组长度 arr[arr.leng ...

  4. 2018icpc 徐州h题

    题目大意: https://codeforces.com/gym/102012/problem/H?csrf_token=c9d0191a64a241166d54a565b1615125 区间[l , ...

  5. polya置换

    UVA10294 POLYA定理的基本应用 题意:有n个珠子围成的环,有t种颜色可以染这些珠子:如果这个环可以旋转有几种办法:如果这个环可以旋转,且可以翻转,有几种办法: 参考博客 刘汝佳的分析: 等 ...

  6. 如何在linux中发送邮件,使用163邮箱发信。

    linux中,可以使用mail命令往外发送邮件,在使用前,只需要指定如下简单配置即可,这里演示用  163.com    邮箱发送至 qq.com 编辑 /etc/mail.rc,写入下方的参数 se ...

  7. Flume-Exec Source 监控单个本地文件

    实时监控,并上传到 HDFS 中. 一.Flume 要想将数据输出到 HDFS,须持有 Hadoop 相关 jar 包 若 Hadoop 环境和 Flume 在同一节点,那么只要配置 Hadoop 环 ...

  8. dubbo异常filter

    dubbo请求调用过程分析 https://blog.csdn.net/javahongxi/article/details/72876694 浅谈dubbo的ExceptionFilter异常处理  ...

  9. 性能分析 | Linux 内存占用分析

    这篇博客主要介绍 linux 环境下,查看内存占用的两种方式:使用 ps,top等命令:查看/proc/[pid]/下的文件.文章简要介绍了命令的使用方法与一些参数意义,同时对/proc/[pid]/ ...

  10. Ubuntu下使用AMD APP编写OpenCL程序

    对于Ubuntu或其近亲(Lubuntu.Kubuntu.Mint等)编写OpenCL程序也不会太难.由于本例用的是AMD APP SDK,因此需要AMD的GPU以及相关驱动.首先,去AMD官网下载G ...