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边。对于每个边,问你包含这条边的最小生成树是多少。

题解:

先求最小生成树..查询在树上的边不影响结果,不在树上的边加入会产生环,那么求出这个环上权最大的边,删掉就是包含当前边的最小生成树.这个查询可以用倍增lca做

当然,直接上熟练剖分/LCT也是兹瓷的!

代码

#include<bits/stdc++.h>
using namespace std; const int N=200500;
int n,m;
struct node
{
int x,y,c,no;
}E[N<<1];
int pre[N],to[N<<1],w[N<<1],nxt[N<<1];
int fa[N],lca[N][22],p[N][22],dep[N],cnt;
long long ans[N]; void makeedge(int x,int y,int c)
{
to[cnt]=x;w[cnt]=c;nxt[cnt]=pre[y];pre[y]=cnt++;
to[cnt]=y;w[cnt]=c;nxt[cnt]=pre[x];pre[x]=cnt++;
}
int getfather(int x)
{
if(fa[x]==x) return fa[x];else return fa[x]=getfather(fa[x]);
}
void dfs(int x)
{
for(int it=pre[x];~it;it=nxt[it])
{
int y=to[it],c=w[it];
if(y==lca[x][0]) continue;
dep[y]=dep[x]+1,lca[y][0]=x,p[y][0]=c;
dfs(y);
}
}
int query(int x,int y)
{
int ret=0;
if(dep[x]<dep[y]) swap(x,y);
for(int i=21;i>=0;i--)
if(dep[x]-(1<<i)>=dep[y])
ret=max(ret,p[x][i]),x=lca[x][i];
if(x==y) return ret;
for(int i=21;i>=0;i--)
if(lca[x][i]!=lca[y][i])
ret=max(ret,max(p[x][i],p[y][i])),x=lca[x][i],y=lca[y][i];
ret=max(ret,max(p[y][0],p[x][0]));
return ret;
}
bool cmp(node t1,node t2)
{
return t1.c<t2.c;
} int main()
{
scanf("%d%d",&n,&m);
memset(pre,-1,sizeof(pre));
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].c);
E[i].no=i;
}
sort(E+1,E+m+1,cmp);
long long tot=0;
for(int i=1;i<=m;i++)
{
int x=E[i].x,y=E[i].y;
int f1=getfather(x),f2=getfather(y);
if(f1!=f2)
{
fa[f2]=f1;
tot+=(long long)E[i].c;
makeedge(x,y,E[i].c);
}
}
dfs(1);
for(int j=1;j<=21;j++)
for(int i=1;i<=n;i++)
if(lca[i][j-1])
{
lca[i][j]=lca[lca[i][j-1]][j-1];
p[i][j]=max(p[i][j-1],p[lca[i][j-1]][j-1]);
}
for(int i=1;i<=m;i++)
{
int x=E[i].x,y=E[i].y;
int tt=query(x,y);
ans[E[i].no]=max(0LL,(long long)E[i].c-(long long)tt)+tot;
}
for(int i=1;i<=m;i++)
printf("%I64d\n",ans[i]);
return 0;
}

Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值的更多相关文章

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

  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. 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. Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)

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

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

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

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

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

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

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

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

随机推荐

  1. 存储过程中使用事务与try catch

    一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : ) ) ...

  2. 第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承

    前面有一篇对于常见元素的识别和操作的python自动化脚本,这一篇就接着聊下python的类继承,已经它的第三款unittest框架,和报告收集包HTMLtestRunner的应用. 还是直接上代码吧 ...

  3. Http相应代码及获取方法

    1xx(临时响应)用于表示临时响应并需要请求者执行操作才能继续的状态代码. 代码 说明 100(继续) 请求者应当继续提出请求.服务器返回此代码则意味着,服务器已收到了请求的第一部分,现正在等待接收其 ...

  4. [转]Linux中文件权限目录权限的意义及权限对文件目录的意义

    转自:http://www.jb51.net/article/77458.htm linux中目录与文件权限的意义 一.文件权限的意义 r:可以读这个文件的具体内容: w:可以编辑这个文件的内容,包括 ...

  5. Android 数独游戏 记录

    Android图形编程基本概念 颜色对象 Color 类 int color = Color.bule    //蓝色 int color = Color.argb(255,255,255,255); ...

  6. A.xml

    pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...

  7. Windows Azure 设置虚拟机静态外网IP地址

    官方说法叫做“虚拟公共IP地址保留”,为容易理解,我们称之为静态外网IP地址. 如果在国内使用国际版Windows Azure服务时强烈推荐为虚拟机设置IP地址保留. 由于Windows Azure ...

  8. php基础知识(3)(文件加载include)

    文件加载 综述: 有4个文件加载的语法形式(注意,不是函数): include,  include_once,  require, require_once; 他们的本质是一样的,都是用于加载/引入/ ...

  9. 在IT网站上少花些时间

    我自己关注的IT网站还是蛮多的,经常去的有CSDN,博客园,51CTO,InfoQ,还有微博,微信上关注了IT程序猿,IT技术博客大学习,程序员之家, 开发者头条,还有还有,我还通过邮件订阅了码家周刊 ...

  10. VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】

    Visual Studio 2008 .VS2008.VC2008工程源文件配置.编译配置   有时编写代码时,往往存在这样的需求(或是希望有这样的功能):一个工程经过不共同的配置实现不同的版本或是功 ...