time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
5 7
1 2 3
1 3 1
1 4 5
2 3 2
2 5 3
3 4 2
4 5 4
output
9
8
11
8
8
8
9 题意:给你一个n个点,m条边的无向图。对于每一条边,求包括该边的最小生成树
我们首先想到的是,求一次整图的MST后,对于每一条边(u,v),如果该边在整图的最小生成树上,答案就是MST,否则,加入的边(u,v)就会使原来的最小生成树成环,可以通过LCA确定该环,那么我们只要求出点u到LCA(u,v)路径上的最大边权和v到LCA(u,v)路径上的最大边权中的最大值mx,MST - mx + w[u,v]就是答案了
其中gx[u][i]表示节点u到其第2^i个祖先之间路径上的最大边权
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + ;
const int DEG = ;
typedef long long ll;
struct edge {
int v, w, next;
edge() {}
edge(int v, int w, int next) : v(v), w(w), next(next){}
}e[N << ]; int head[N], tot;
int fa[N][DEG], deg[N];
int gx[N][DEG];
void init() {
memset(head, -, sizeof head);
tot = ;
}
void addedge(int u, int v, int w) {
e[tot] = edge(v, w, head[u]);
head[u] = tot++;
}
void BFS(int root) {
queue<int> que;
deg[root] = ;
fa[root][] = root;
gx[root][] = ;
que.push(root);
while(!que.empty()) {
int tmp = que.front();
que.pop();
for(int i = ; i < DEG; ++i) {
fa[tmp][i] = fa[ fa[tmp][i - ] ][i - ];
gx[tmp][i] = max(gx[tmp][i - ], gx[ fa[tmp][i - ] ][i - ]);
// printf("[%d %d] ", tmp, gx[tmp][i]);
}
// puts("");
for(int i = head[tmp]; ~i; i = e[i].next) {
int v = e[i].v;
int w = e[i].w;
if(v == fa[tmp][]) continue;
deg[v] = deg[tmp] + ;
fa[v][] = tmp;
gx[v][] = w;
que.push(v);
}
}
}
int Mu, Mv;
ll LCA(int u, int v) {
Mu = Mv = -;
if(deg[u] > deg[v]) swap(u, v);
int hu = deg[u], hv = deg[v];
int tu = u, tv = v;
for(int det = hv - hu, i = ; det; det >>= , ++i)
if(det & ) { Mv = max(Mv, gx[tv][i]); tv = fa[tv][i]; }
if(tu == tv) return Mv;
for(int i = DEG - ; i >= ; --i) {
if(fa[tu][i] == fa[tv][i]) continue;
Mu = max(Mu, gx[tu][i]);
Mv = max(Mv, gx[tv][i]);
tu = fa[tu][i];
tv = fa[tv][i]; }
return max(max(Mu, gx[tu][]), max(Mv, gx[tv][]));
} int U[N], V[N], w[N], r[N], f[N];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool cmp(int a, int b) { return w[a] < w[b]; }
ll MST;
int n, m;
void mst() { scanf("%d%d", &n, &m);
for(int i = ; i <= m; ++i) {
scanf("%d%d%d", &U[i], &V[i], &w[i]);
r[i] = i;
f[i] = i;
}
sort(r + , r + m + , cmp);
MST = ;
for(int i = ; i <= m; ++i)
{
int id = r[i];
int fu = find(U[id]);
int fv = find(V[id]);
if(fu != fv) {
MST += w[id];
f[ fu ] = fv;
addedge(U[id], V[id], w[id]);
addedge(V[id], U[id], w[id]);
}
}
}
int main() {
init();
mst();
BFS(); for(int i = ; i <= m; ++i) {
printf("%I64d\n", MST - LCA(U[i], V[i]) + w[i]);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. nodejs链接mongodb数据库

    nodeJs链接mongodb数据库有两种方式,第一种是利用官方自己开发的npm包mongodb链接,第二种是利用第三方npm包mongoose链接:这里如果是window操作系统,建议用mongoo ...

  2. JS_ECMA基本语法中的几种封装的小函数-2

    大家好!今天继续给大家写一下ECMA中的剩下的小函数以及实用的实例: 首先先给大家说一下字符串.数组.数学方法以及json的一点小知识点: 字符串方法: str.length str.charAt(i ...

  3. 常用shell命令操作

    1.找出系统中所有的*.c 和*.h 文件 (-o 或者) $find / -name "*.cpp" -o -name "*.h" 2.设定 eth0 的 I ...

  4. JavaScript基础——创建函数

    JavaScript的最重要的一个部分是制作其他代码可以重用的代码.要做到这一点,你可以把代码组织成执行特定任务的函数.函数是结合在一个单一的块中,并给予一个名称的一系列代码语句.然后,你就可以通过引 ...

  5. case/casez/casex 的区分与使用

    参考:http://www.cnblogs.com/poiu-elab/archive/2012/11/02/2751323.html 与  verilog数字系统设计基础 一般来说,使用最多的是CA ...

  6. 基于Bootstrap简单实用的tags标签插件

    http://www.htmleaf.com/jQuery/ jQuery之家 自由分享jQuery.html5和css3的插件库 基于Bootstrap简单实用的tags标签插件

  7. 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载

    参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...

  8. 【PHP数组的使用】

    PHP数组使用关键字array标识,数组内的元素可以是任意类型,而且可以不是同一种类型,这和c.java不同. 遍历数组的方法可以使用foreach,也可以使用for循环 可以使用print_r或者v ...

  9. MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏

    业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...

  10. PHP实现上一篇、下一篇

    //php实现上一篇.下一篇 获取当前浏览文章id $id = isset($_GET[ ? intval($_GET['id']) : ""; 下一篇文章 $query = my ...