CF# Educational Codeforces Round 3 E. 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
题意:给出一个图,问每一条边如果要在一个生成树当中,那这个生成树最小是多少。
分析:先找出一个最小生成树。
想像一下,加入一条边,会对这个生成树造成什么影响。
形成了一个环,然后最优情况,肯定要拿掉除他之外最大的一条边。
问题就变成了,在最小生成树上查询两点之间的边的最大值。
- /**
- Create By yzx - stupidboy
- */
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <iostream>
- #include <algorithm>
- #include <map>
- #include <set>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- typedef long long LL;
- typedef double DB;
- #define MIT (2147483647)
- #define INF (1000000001)
- #define MLL (1000000000000000001LL)
- #define sz(x) ((int) (x).size())
- #define clr(x, y) memset(x, y, sizeof(x))
- #define puf push_front
- #define pub push_back
- #define pof pop_front
- #define pob pop_back
- #define mk make_pair
- inline int Getint()
- {
- int Ret = ;
- char Ch = ' ';
- bool Flag = ;
- while(!(Ch >= '' && Ch <= ''))
- {
- if(Ch == '-') Flag ^= ;
- Ch = getchar();
- }
- while(Ch >= '' && Ch <= '')
- {
- Ret = Ret * + Ch - '';
- Ch = getchar();
- }
- return Flag ? -Ret : Ret;
- }
- const int N = , M = ;
- int n, m;
- struct EdgeType
- {
- int u, v, value, index;
- LL ans;
- inline bool operator <(const EdgeType &t) const
- {
- return value < t.value;
- }
- inline void Read()
- {
- u = Getint();
- v = Getint();
- value = Getint();
- }
- } edge[N];
- int fa[N], favalue[N];
- int first[N], to[N * ], value[N * ], next[N * ], tot;
- int up[N][M], depth[N], maxcnt[N][M];
- LL ans;
- inline void Input()
- {
- n = Getint();
- m = Getint();
- for(int i = ; i <= m; i++)
- {
- edge[i].Read();
- edge[i].index = i;
- }
- }
- inline int Find(int x)
- {
- static int path[N], len;
- for(len = ; x != fa[x]; x = fa[x])
- path[++len] = x;
- for(int i = ; i <= len; i++) fa[path[i]] = x;
- return x;
- }
- inline void Insert(int u, int v, int val)
- {
- tot++;
- to[tot] = v, value[tot] = val, next[tot] = first[u];
- first[u] = tot;
- }
- inline void Bfs()
- {
- static int que[N], head, tail;
- for(int i = ; i <= n; i++) fa[i] = -;
- que[] = , head = tail = , fa[] = , depth[] = ;
- while(head <= tail)
- {
- int u = que[head++];
- for(int tab = first[u], v; tab; tab = next[tab])
- if(fa[v = to[tab]] == -)
- {
- fa[v] = u, favalue[v] = value[tab], depth[v] = depth[u] + ;
- que[++tail] = v;
- }
- }
- }
- inline int GetMax(int u, int v)
- {
- int ret = , level = M;
- while(depth[u] != depth[v])
- {
- if(depth[u] < depth[v]) swap(u, v);
- while(level && ( << level) > depth[u] - depth[v]) level--;
- ret = max(ret, maxcnt[u][level]);
- u = up[u][level];
- }
- level = M;
- while(level && u != v)
- {
- while(level && ( << level) > depth[u]) level--;
- while(level && up[u][level] == up[v][level]) level--;
- ret = max(ret, maxcnt[u][level]);
- ret = max(ret, maxcnt[v][level]);
- u = up[u][level], v = up[v][level];
- }
- while(u != v)
- {
- ret = max(ret, favalue[u]);
- ret = max(ret, favalue[v]);
- u = fa[u], v = fa[v];
- }
- return ret;
- }
- inline bool CompareByIndex(const EdgeType &a, const EdgeType &b)
- {
- return a.index < b.index;
- }
- inline void Solve()
- {
- sort(edge + , edge + + m);
- for(int i = ; i <= n; i++) fa[i] = i;
- for(int i = ; i <= m; i++)
- {
- int u = Find(edge[i].u), v = Find(edge[i].v);
- if(u != v)
- {
- Insert(edge[i].u, edge[i].v, edge[i].value);
- Insert(edge[i].v, edge[i].u, edge[i].value);
- ans += edge[i].value;
- fa[u] = v;
- }
- }
- Bfs();
- for(int i = ; i < M; i++)
- {
- if(( << i) > n) break;
- for(int j = ; j <= n; j++)
- if(i == )
- {
- up[j][i] = fa[j];
- maxcnt[j][i] = favalue[j];
- }
- else
- {
- up[j][i] = up[up[j][i - ]][i - ];
- maxcnt[j][i] = max(maxcnt[j][i - ], maxcnt[up[j][i - ]][i - ]);
- }
- }
- for(int i = ; i <= m; i++)
- {
- int u = edge[i].u, v = edge[i].v;
- int ret = GetMax(u, v);
- edge[i].ans = ans - ret + edge[i].value;
- }
- sort(edge + , edge + + m, CompareByIndex);
- for(int i = ; i <= m; i++) printf("%I64d\n", edge[i].ans);
- }
- int main()
- {
- freopen("a.in", "r", stdin);
- Input();
- Solve();
- return ;
- }
CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge的更多相关文章
- CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种
题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...
- 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 ...
- 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 (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- [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
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Edu3 E. Minimum spanning tree for each edge
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- CSS3与页面布局学习总结——Box Model、边距折叠、内联与块标签、CSSReset
目录 一.盒子模型(Box Model) 1.1.宽度测试 1.2.溢出测试 1.3.box-sizing属性 1.4.利用CSS画图 二.边距折叠 2.1.概要 2.2.垂直方向外边距合并计算 三. ...
- Android RadioButton selector背景
RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...
- android中点击空白处隐藏软键盘
InputMethodManager manager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERV ...
- EF – 7.一对多关联
5.6.8 <一对多关联(上)> 5.6.9 <一对多关联(下)> 一对多的关联,可以说是整个数据库应用程序中最常见的一种关联类型了,因此,必须高度重视这种关联类型CRUD的实 ...
- [webkit移动开发笔记]之如何去除android上a标签产生的边框(转)
转载地址:http://www.cnblogs.com/PeunZhang/archive/2013/02/28/2907708.html 去年年底,做完最后一个项目就可以开开心心回家,可是在测试阶段 ...
- spring 集成 log4j 配置
在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> < ...
- js 动态时钟
js 动态时钟 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- WPF QuickStart系列之样式和模板(Style and Template)
在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术.简单来说,如果我们需要简单的给一个Button设置宽,高,Marg ...
- PMP 第十一章 项目风险管理
1规划风险管理 2识别风险 3 风险定性分析 4风险定量分析 5规划风险应对 6监控风险 1.项目风险是什么?已知未知风险.未知未知风险对应应急储备和管理储备的关系.风险承受力和风险偏好是什么? 2. ...
- php获取当前毫秒时间戳
最近在做一个智能家居项目的后台,需要实时上传用户对智能设备的配置信息到服务器,以便实现同步,因此对于时间的精确度要求比较高,最开始直接是用php的time()函数来获取时间戳,获取的时间精确到秒级别, ...