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.

Examples

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 次小生成树模板:
lca+倍增+最小生成树
#define eps 1e-6
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N = ;
//*************************
int n, m;
//kruscal
struct tree
{
int a,b,w;
} p[N],s[N];
int p1[N];
int rk1[N];
//倍增
int fa[N][], max_e[N][], dep[N];
//lca
struct edge
{
int to, nxt,w;
} e[N<<];
int fst[N], tot;
struct query
{
int to,nxt;
int idx;
} Q[N<<];
int h[N],tt;
int p2[N],rk2[N];
int acr[N], ans[N];
bool vis[N];
//************************
void CLS()
{
tot = ;
memset(fst,-,sizeof(fst));
tt = ;
for(int i=; i<=n; i++)p1[i]=i,p2[i]=i;
memset(h,-,sizeof(h));
}
void add(int u,int v,int w)
{
e[++tot].to = v;
e[tot].w = w;
e[tot].nxt = fst[u];
fst[u] = tot;
}
void add_Q(int u,int v,int idx)
{
Q[++tt].to = v;
Q[tt].nxt = h[u];
Q[tt].idx = idx;
h[u] = tt;
}
//kruscal
bool cmp(tree x,tree y)
{
return x.w<y.w;
}
int find_p(int x)
{
return x == p1[x] ? x : p1[x]=find_p(p1[x]);
}
void uone1(int x,int y)
{
int t1=find_p(x);
int t2=find_p(y);
if(t1!=t2)
{
if(rk1[t1]>rk1[t2])p1[t2]=t1;
else p1[t1]=t2;
if(rk1[t1]==rk1[t2])rk1[t2]++;
}
}
ll kruscal()
{
ll res = ;
sort(p+, p+m+, cmp);
int cnt=;
for (int i = ; i <= m; i++)
{
int x=p[i].a;
int y=p[i].b;
int w=p[i].w;
if(find_p(x)!=find_p(y))
{
cnt++;
uone1(x,y);
res+=w;
add(x,y,w);
add(y,x,w);
if(cnt==n-)break;
}
}
return res;
} //倍增
void init_fa(int u, int p, int w)
{
dep[u] = dep[p] + ;
fa[u][] = p;
max_e[u][] = w;
for (int i = ; fa[u][i-]; i++)
{
fa[u][i] = fa[ fa[u][i-] ][i-];
max_e[u][i] = max(max_e[u][i-], max_e[ fa[u][i-] ][i-]);
}
} int cal(int u, int lca)
{
int d = dep[u] - dep[lca];
int res = ;
for(int i = ; i >= ; i--)
{
if ((<<i) <= d)
{
d -= (<<i);
res = max(res, max_e[u][i]);
u = fa[u][i];
}
}
return res;
} //LCA
int find_q(int x)
{
return x == p2[x] ? x : p2[x]=find_q(p2[x]);
}
void uone2(int x,int y)
{
int t1=find_q(x);
int t2=find_q(y);
if(t1!=t2)
{
if(rk2[t1]>rk2[t2])p2[t2]=t1;
else p2[t1]=t2;
if(rk2[t1]==rk2[t2])rk2[t2]++;
}
}
void LCA(int u)
{
vis[u] = ;
acr[u] = u;
for(int p = fst[u]; p != -; p = e[p].nxt)
{
int v = e[p].to;
if(vis[v]) continue;
init_fa(v, u, e[p].w);
LCA(v);
uone2(u,v);
acr[find_q(u)] = u;
}
for(int p = h[u]; p != -; p = Q[p].nxt)
{
int v = Q[p].to;
if(vis[v]) ans[Q[p].idx] = acr[find_q(v)];
}
} int main()
{
// freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &m);
CLS();
for (int i = ; i <= m; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
p[i].a=s[i].a=a;
p[i].b=s[i].b=b;
p[i].w=s[i].w=w;
add_Q(a,b,i);
add_Q(b,a,i);
}
ll tmp = kruscal();
LCA();
for (int i = ; i <= m; i++)
printf("%I64d\n", tmp+s[i].w-max(cal(s[i].a, ans[i]), cal(s[i].b, ans[i])));
return ;
}

609E- Minimum spanning tree for each edge的更多相关文章

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

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

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

  3. Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge

    题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2 ...

  4. codeforces 609E. Minimum spanning tree for each edge 树链剖分

    题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...

  5. cf 609E.Minimum spanning tree for each edge

    最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...

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

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

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

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

  10. 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. HDU3625(SummerTrainingDay05-N 第一类斯特林数)

    Examining the Rooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. Flask中request参数

    首先要明确一件事,Request这是个对象,不管使用PHP还是python还是什么java语言,虽然request这个对象可能叫的名字不一样,(在其他语言中可能叫什么HttpRequest),但是原理 ...

  3. 【代码笔记】iOS-增加右侧按钮

    一,工程图. 二,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup ...

  4. 【代码笔记】iOS-SDWebImage的使用

    一,工程图. 二,代码. RootViewController.m #import "RootViewController.h" //加入头文件 #import "UII ...

  5. AsyncTask POST请求

    布局: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint. ...

  6. Android应用程序进程启动过程(前篇)

    在此前我讲过Android系统的启动流程,系统启动后,我们就比较关心应用程序是如何启动的,这一篇我们来一起学习Android7.0 应用程序进程启动过程,需要注意的是“应用程序进程启动过程”,而不是应 ...

  7. mongodb3.X权限配置

    环境: CentOS6.8  mongodb3.4.1 1.连接mongodb数据库(如果mongo命令没有做环境变量配置,需要定位到有mongo命令的目录) [root@VM_118_34_cent ...

  8. Python+Selenium笔记(四):unittest的Test Suite(测试套件)

    (一) Test Suite测试套件 一个测试套件是多个测试或测试用例的集合,是针对被测程序的对应的功能和模块创建的一组测试,一个测试套件内的测试用例将一起执行. 应用unittest的TestSui ...

  9. 留言板0.4_model中的数据库(2)

    今天就讲讲:如何将后台数据呈现在HTML页面中,以及url配置时的两点技巧吧. 1.首先在"views.py"中提取出后台数据 def getform(request): mess ...

  10. 2. DAS,NAS,SAN在数据库存储上的应用

    一. 硬盘接口类型1. 并行接口还是串行接口(1) 并行接口,指的是并行传输的接口,比如有0~9十个数字,用10条传输线,那么每根线只需要传输一位数字,即可完成.从理论上看,并行传输效率很高,但是由于 ...