码迷,mamicode.com
首页 > 其他好文 > 详细

Minimum spanning tree for each edge

时间:2018-04-08 00:25:36      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:lower   node   order   scan   set   union   ops   记录   space   

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
Copy
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
题解:建最小支撑树,往里面加边u-v,那么容易想到的是ans=minTree-max{u-....-v这条链上的最大值}+va[u-v]。这里和LCA联系上了,记录从两端点向上跳2^k次得到的最大值,直到跳到u和v的公共祖先。
  1 #pragma warning(disable:4996)
  2 #include<bitset>
  3 #include<string>
  4 #include<cstdio>
  5 #include<cstring>
  6 #include<iostream>
  7 #include<algorithm>
  8 using namespace std;
  9 typedef long long ll;
 10 
 11 const int INF = 2e9 + 9;
 12 const int maxn = 200005;
 13 
 14 int n, m, tot, root;
 15 int head[maxn], pa[maxn][20], dp[maxn], Fa[maxn], as[maxn][20];
 16 
 17 struct node { int u, v, w; } a[maxn], b[maxn];
 18 struct mode { int to, va, next; } e[2*maxn];
 19 
 20 bool cmp(node& x, node& y) { return x.w < y.w; }
 21 
 22 void Inite() {
 23     tot = 0;
 24     memset(head, -1, sizeof(head));
 25 
 26     memset(as, 0, sizeof(as));
 27     memset(dp, 0, sizeof(dp));
 28     memset(pa, -1, sizeof(pa));
 29     for (int i = 1; i <= n; i++) Fa[i] = i;
 30 }
 31 
 32 void addedge(int u, int v, int w) {
 33     e[tot].to = v;
 34     e[tot].va = w;
 35     e[tot].next = head[u];
 36     head[u] = tot++;
 37 }
 38 
 39 int Find(int a) {
 40     if (a == Fa[a]) return a;
 41     return Fa[a] = Find(Fa[a]);
 42 }
 43 
 44 bool Union(int a, int b) {
 45     int x = Find(a), y = Find(b);
 46     if (x == y) return false;
 47     else {
 48         Fa[x] = y;
 49         return true;
 50     }
 51 }
 52 
 53 ll minTree() {
 54     sort(a + 1, a + m + 1, cmp);
 55     ll ans = 0;
 56     for (int i = 1; i <= m; i++) {
 57         if (Union(a[i].u, a[i].v)) {
 58             ans += a[i].w;
 59             root = a[i].u;
 60             addedge(a[i].u, a[i].v, a[i].w);
 61             addedge(a[i].v, a[i].u, a[i].w);
 62         }
 63     }
 64     return ans;
 65 }
 66 
 67 void DFS(int u, int p, int deep) {
 68     pa[u][0] = p;
 69     dp[u] = deep;
 70     for (int i = head[u]; i != -1; i = e[i].next) {
 71         int v = e[i].to;
 72         if (v == p) continue;
 73         as[v][0] = e[i].va;
 74         DFS(v, u, deep + 1);
 75     }
 76 }
 77 
 78 void getPa() {
 79     DFS(root, -1, 0);
 80     for (int i = 1; (1 << i) < n; i++) {
 81         for (int j = 1; j <= n; j++) {
 82             if (pa[j][i - 1] != -1) {
 83                 pa[j][i] = pa[pa[j][i - 1]][i - 1];
 84                 as[j][i] = max(as[j][i - 1], as[pa[j][i - 1]][i - 1]);
 85             }
 86         }
 87     }
 88 }
 89 
 90 int Lca(int u, int v) {
 91     if (dp[u] > dp[v]) swap(u, v);
 92     int ans = 0;
 93     for (int i = 0; i <= 19; i++) if ((dp[v] - dp[u]) >> i & 1) { ans = max(ans, as[v][i]); v = pa[v][i]; }
 94     if (u == v) return ans;
 95     for (int i = 19; i >= 0; i--) {
 96         if (pa[u][i] != pa[v][i]) {
 97             int tmp = max(as[u][i], as[v][i]);
 98             ans = max(ans, tmp);
 99             u = pa[u][i];
100             v = pa[v][i];
101         }
102     }
103     int tmp = max(as[u][0], as[v][0]);
104     ans = max(ans, tmp);
105     return ans;
106 }
107 
108 int main()
109 {
110     while (scanf("%d%d", &n, &m) != EOF) {
111         Inite();
112         for (int i = 1; i <= m; i++) scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
113         for (int i = 1; i <= m; i++) b[i].u = a[i].u, b[i].v = a[i].v, b[i].w = a[i].w;
114 
115         ll ans1 = minTree();
116         //cout << "root" << " " << root << endl;
117 
118         getPa();
119         for (int i = 1; i <= m; i++) {
120             int ans2 = Lca(b[i].u, b[i].v);
121             printf("%I64d\n", ans1 + b[i].w - ans2);
122         }
123     }
124     return 0;
125 }

 

Minimum spanning tree for each edge

标签:lower   node   order   scan   set   union   ops   记录   space   

原文地址:https://www.cnblogs.com/zgglj-com/p/8735347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!