codeforces 609E Minimum spanning tree for each edge
2 seconds
256 megabytes
standard input
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.
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.
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.
5 7
1 2 3
1 3 1
1 4 5
2 3 2
2 5 3
3 4 2
4 5 4
9
8
11
8
8
8
9
保证某条边e存在的MST就是普通Kruskal把e优先到了最前面。
先求一遍MST,如果e不再MST上,是因为形成了环,把环上除了e的最大权边去掉就好了。
(以前的LCA:用ST来RMQ,查询O(1)
(向祖先结点倍增其实和ST差不多,查询O(logn),维护信息灵活
(一开始想的是树剖,复杂度稍高
#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int N = 2e5+, M = N*; int pa[N], rak[N];
int fd(int x){ return pa[x] ? pa[x] = fd(pa[x]) : x; }
bool unite(int x,int y)
{
int a = fd(x), b = fd(y);
if(a == b) return false;
if(rak[a] < rak[b]){
pa[a] = b;
}
else {
pa[b] = a;
if(rak[a] == rak[b]) rak[a]++;
}
return true;
} int fro[N], to[N], we[N]; int hd[N];
int nx[M], ver[M], wei[M];
int ec; void add_e(int u,int v,int w)
{
ver[++ec] = v;
wei[ec] = w;
nx[ec] = hd[u];
hd[u] = ec;
} int n, m;
int *cmp_c;
bool cmp_id(int i,int j){ return cmp_c[i] < cmp_c[j]; } int r[N];
ll kruskal()
{
ll re = ;
int i,j;
for(i = ; i <= m; i++) r[i] = i;
cmp_c = we;
sort(r+, r + + m, cmp_id);
//ec = 0;
for(i = ; i <= m; i++){
j = r[i];
if(unite(fro[j],to[j])){
add_e(fro[j],to[j],we[j]);
add_e(to[j],fro[j],we[j]);
re += we[j];
we[j] = ;
}
}
return re;
} const int LOG = ; int fa[N][LOG], mx[N][LOG];
int dep[N]; void dfs(int u,int f = ,int fw = ,int d = )
{
fa[u][] = f;
mx[u][] = fw;
dep[u] = d;
for(int i = hd[u]; i; i = nx[i]) {
int v = ver[i];
if(v == f) continue;
dfs(v,u,wei[i],d+);
}
} int lg; int queryMx(int u,int v)
{
int re = , i;
if(dep[u] < dep[v]) swap(u,v);
for(i = lg; i >= ; i--) if(dep[u] - (<<i) >= dep[v]){
re = max(re,mx[u][i]);
u = fa[u][i];
}
if(u == v) return re;
for(i = lg; i >= ; i--) if(fa[u][i] != fa[v][i]){
re = max(re,max(mx[u][i],mx[v][i]));
u = fa[u][i];
v = fa[v][i];
}
return max(re,max(mx[u][],mx[v][]));
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
//cout<<log2(N);
scanf("%d%d",&n,&m);
int i,j;
for(i = ; i <= m; i++){
scanf("%d%d%d",fro+i,to+i,we+i);
}
ll mst = kruskal(); dfs();
lg = ceil(log2(n));
for(j = ; j <= lg; j++){
for(i = ; i <= n; i++) if(fa[i][j-]){
fa[i][j] = fa[fa[i][j-]][j-];
mx[i][j] = max(mx[i][j-],mx[fa[i][j-]][j-]);
}
}
for(i = ; i <= m; i++) {
printf("%I64d\n",we[i]?mst + we[i] - queryMx(fro[i],to[i]):mst);
}
return ;
}
codeforces 609E Minimum spanning tree for each edge的更多相关文章
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- codeforces 609E. Minimum spanning tree for each edge 树链剖分
题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...
- Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge
题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2 ...
- cf 609E.Minimum spanning tree for each edge
最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- DataX 安装和使用
阿里云介绍: 1. 下载安装包.作为阿里主要的数据传输工具Datax,阿里已经完全开源到github上面了.下载地址(https://github.com/alibaba/DataX). 2. 安装环 ...
- rsync 问题总结
Rsync服务常见问题汇总讲解:==================================1. rsync服务端开启的iptables防火墙 [客户端的错误] No route to ...
- jacob自己动生成word文档目录
任务目的 1自动生成word文档目录. 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存.调用程序读取目标文档,自动根据标题生成目录到{目录}位置. 效果 关键代码 insert ...
- Column 'orders' in order clause is ambiguous
今天报了这个错误 原因是.当使用sql查询语句,使用了join查表.但是这个orders没指定是哪张表的字段 ,发生在自关联情况
- springboot 之 使用jetty web容器
springboot 中默认的web容器是tomcat. 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器. <dependency> <groupId> ...
- nyoj 1023——还是回文——————【区间dp】
还是回文 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描述 判断回文串很简单,把字符串变成回文串也不难.现在我们增加点难度,给出一串字符(全部是小写字母),添加或删除一 ...
- What's the difference between @Component, @Repository & @Service annotations in Spring?
@Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...
- 日期控件html
日期控件多的是,这里收录的是最简单的一种 <html> <head> <script type="text/javascript"> funct ...
- python搭建本地服务器
python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...
- C#学习笔记5
1.接口的显式实现:显式实现需要在实现接口的类型中,在实现接口的成员中添加接口名称的前缀.且没有必要添加public.virtual这些修饰符,因为显式实现只能通过接口调用,不能用实现类进行调用.为此 ...