E. Minimum spanning tree for each edge
 

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
 
  1. 5 7
    1 2 3
    1 3 1
    1 4 5
    2 3 2
    2 5 3
    3 4 2
    4 5 4
output
 
  1. 9
    8
    11
    8
    8
    8
    9

题意:

  给你n个点,m条边的带权无向图

  问你分别包含第i条边的 MST 权值 是多少

题解:

  先求一遍MST,这样知道了 总权值, 和某些 构成 MST 的 边

  假设 第 i 条边 在 当前求的 MST 的 边中

    那么 答案就是 这个 weight吧

  假如 第 i 条边 不在 当前 求的 MST 中,那么我们就加入在这个 MST的图中,会发现 无论 如何都构成 环

  那么 答案 就是 ,在这个环上 删除一条 除了加入的边 的权 值 最大的 那一条边

  问题就变成了 去MST 中 任意两点 的 边权 最大值

  这个问题的解法有很多

    我写的是LCA ,复杂度是logn * m

    还可以 直接 暴力 一点的写法

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #pragma comment(linker, "/STACK:102400000,102400000")
  5. #define ls i<<1
  6. #define rs ls | 1
  7. #define mid ((ll+rr)>>1)
  8. #define pii pair<int,int>
  9. #define MP make_pair
  10.  
  11. typedef long long LL;
  12. const long long INF = 1e18;
  13. const double Pi = acos(-1.0);
  14. const int N = 2e5+, M = 5e5+, inf = 2e9, mod = 1e9+;
  15.  
  16. int fa[N],pa[N][],mx[N][],deep[N],vis[N],n,m;
  17. LL weight;
  18. LL ans[N];
  19. vector<pii> G[N];
  20. struct edge{
  21. int u,v,w,id;
  22. bool operator < (const edge &b) const {
  23. return w<b.w;
  24. }
  25. }e[M];
  26. int finds(int x) {return x == fa[x] ? x:fa[x]=finds(fa[x]);}
  27.  
  28. void dfs(int u,int f,int c) {
  29. pa[u][]=f, mx[u][]=c;
  30. for(int i = ; i < G[u].size(); ++i) {
  31. int to = G[u][i].first;
  32. if(to == f) continue;
  33. deep[to] = deep[u] + ;
  34. dfs(to,u,G[u][i].second);
  35. }
  36. }
  37. void solve() {
  38. for(int i = ; i <= n; ++i) fa[i] = i;
  39. sort(e+,e+m+);
  40. for(int i = ; i <= m; ++i) {
  41. int fx = finds(e[i].u);
  42. int fy = finds(e[i].v);
  43. if(fx == fy) continue;
  44. G[e[i].u].push_back(MP(e[i].v,e[i].w));
  45. G[e[i].v].push_back(MP(e[i].u,e[i].w));
  46. fa[fx] = fy;
  47. vis[i] = ;
  48. weight += e[i].w;
  49. }
  50. dfs(,-,-);
  51. for(int k = ; k < ; ++k) {
  52. for(int i = ; i <= n; ++i) {
  53. if(pa[i][k-] == -)
  54. pa[i][k] = -, mx[i][k] = -;
  55. else
  56. pa[i][k] = pa[pa[i][k-]][k-],
  57. mx[i][k] = max(mx[i][k-],mx[pa[i][k-]][k-]);
  58. }
  59. }
  60. }
  61. int Lca(int u,int v) {
  62. if(deep[u] > deep[v]) swap(u,v);
  63. int ret = ;
  64. for(int k = ; k < ; ++k)
  65. if((deep[v] - deep[u])>>k & )
  66. ret = max(ret, mx[v][k]), v = pa[v][k];
  67. if(u == v) return ret;
  68. for(int k = ; k >= ; --k)
  69. if(pa[u][k] != pa[v][k]) {
  70. ret = max(ret, mx[u][k]), u = pa[u][k];
  71. ret = max(ret, mx[v][k]), v = pa[v][k];
  72. }
  73. return max(ret, max(ret, max(mx[u][],mx[v][])));
  74. }
  75. int main() {
  76. scanf("%d%d",&n,&m);
  77. for(int i = ; i <= m; ++i) {
  78. scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
  79. e[i].id = i;
  80. }
  81. solve();
  82. for(int i = ; i <= m; ++i) {
  83. if(vis[i]) ans[e[i].id] = weight;
  84. else {
  85. ans[e[i].id] = weight - Lca(e[i].u,e[i].v) + e[i].w;
  86. }
  87. }
  88. for(int i = ; i <= m; ++i) printf("%I64d\n",ans[i]);
  89. }

好暴力的写法 %

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define PB push_back
  5. #define MP make_pair
  6. #define SZ(v) ((int)(v).size())
  7. #define FOR(i,a,b) for(int i=(a);i<(b);++i)
  8. #define REP(i,n) FOR(i,0,n)
  9. #define FORE(i,a,b) for(int i=(a);i<=(b);++i)
  10. #define REPE(i,n) FORE(i,0,n)
  11. #define FORSZ(i,a,v) FOR(i,a,SZ(v))
  12. #define REPSZ(i,v) REP(i,SZ(v))
  13. typedef long long ll;
  14. typedef unsigned long long ull;
  15. ll gcd(ll a,ll b) { return b==?a:gcd(b,a%b); }
  16.  
  17. const int MAXN=;
  18. const int MAXM=;
  19. typedef struct E { int a,b,c,idx; } E;
  20. bool operator<(const E &p,const E &q) { return p.c<q.c; }
  21.  
  22. int n,m;
  23. E e[MAXM];
  24.  
  25. int par[MAXN],sz[MAXN],val[MAXN];
  26.  
  27. ll ret;
  28. int extra[MAXM];
  29. int process(int a,int b,int c) {
  30. int mx=;
  31. while((par[a]!=a||par[b]!=b)&&a!=b) { if(par[b]==b||par[a]!=a&&sz[a]<=sz[b]) mx=max(mx,val[a]),a=par[a]; else mx=max(mx,val[b]),b=par[b]; }
  32. if(a==b) return c-mx;
  33. if(sz[a]<sz[b]) swap(a,b);
  34. sz[a]+=sz[b]; par[b]=a; val[b]=c; ret+=c;
  35. return ;
  36. }
  37.  
  38. void run() {
  39. scanf("%d%d",&n,&m);
  40. REP(i,m) scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c),--e[i].a,--e[i].b,e[i].idx=i;
  41. sort(e,e+m);
  42.  
  43. ret=; REP(i,n) par[i]=i,sz[i]=;
  44. REP(i,m) extra[e[i].idx]=process(e[i].a,e[i].b,e[i].c);
  45. REP(i,m) printf("%I64d\n",ret+extra[i]);
  46. }
  47.  
  48. int main() {
  49. run();
  50. return ;
  51. }

Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  9. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

随机推荐

  1. Qt qss 使用

    1.在资源文件建立一个qss文件.如blue.qss 2. 调用 #include "mainwindow.h" #include <QApplication> #in ...

  2. 问题 A: 【动态规划】采药_二维数组_一维数组

    问题 A: [动态规划]采药 时间限制: 1 Sec  内存限制: 64 MB提交: 35  解决: 15[提交][状态][讨论版] 题目描述 山洞里有一些不同的草药,采每一株都需要一些时间,每一株也 ...

  3. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  4. 51 NOD 1384 全排列(STL 搜索)

    1384 全排列       基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题        收藏        关注   给出一个字符串S(可能又重复的字符),按照字典序 ...

  5. asp.net mvc5 伪静态 WebForm

    Mvc4和5通用 1.背景:老项目WebForm开发 需要 融合到新项目Mvc5开发 2.需求:Url地址TruckDetail.aspx?id=455 达到效果 truck/455.html 3.不 ...

  6. Android仿快递 物流时间轴 的代码实现

    首先,这篇参考了别人的代码.根据自己的项目需求简单改造了一下,效果图如下 xml:代码 <?xml version="1.0" encoding="utf-8&qu ...

  7. android ExpandableListView详解

    ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组 ...

  8. 【linux】学习3

    鸟哥 书的第7章 从 /home/dtest1   跳入 /home/dtest2 目录: cd  ../dtest2   注意 cd后有空格 ..后无空格 特殊目录: .    代表此层目录 .. ...

  9. C/S love自编程序

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  10. Server.Transfer方式(或称HttpContext方式)传值实例

    public class QueryPage : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtStaDate ...