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

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 test(s)
input
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

题意:给你一个n个点,m条边的无向图。对于每一条边,求包括该边的最小生成树
我们首先想到的是,求一次整图的MST后,对于每一条边(u,v),如果该边在整图的最小生成树上,答案就是MST,否则,加入的边(u,v)就会使原来的最小生成树成环,可以通过LCA确定该环,那么我们只要求出点u到LCA(u,v)路径上的最大边权和v到LCA(u,v)路径上的最大边权中的最大值mx,MST - mx + w[u,v]就是答案了
其中gx[u][i]表示节点u到其第2^i个祖先之间路径上的最大边权
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int DEG = 20;
typedef long long ll;
struct edge {
    int v, w, next;
    edge() {}
    edge(int v, int w, int next) : v(v), w(w), next(next){}
}e[N << 1];

int head[N], tot;
int fa[N][DEG], deg[N];
int gx[N][DEG];
void init() {
    memset(head, -1, sizeof head);
    tot = 0;
}
void addedge(int u, int v, int w) {
    e[tot] = edge(v, w, head[u]);
    head[u] = tot++;
}
void BFS(int root) {
    queue<int> que;
    deg[root] = 0;
    fa[root][0] = root;
    gx[root][0] = 0;
    que.push(root);
    while(!que.empty()) {
        int tmp = que.front();
        que.pop();
        for(int i = 1; i < DEG; ++i) {
            fa[tmp][i] = fa[ fa[tmp][i - 1] ][i - 1];
            gx[tmp][i] = max(gx[tmp][i - 1], gx[ fa[tmp][i - 1] ][i - 1]);
          // printf("[%d %d] ", tmp, gx[tmp][i]);
        }
       // puts("");
        for(int i = head[tmp]; ~i; i = e[i].next) {
            int v = e[i].v;
            int w = e[i].w;
            if(v == fa[tmp][0]) continue;
            deg[v] = deg[tmp] + 1;
            fa[v][0] = tmp;
            gx[v][0] = w;
            que.push(v);
        }
    }
}
int Mu, Mv;
ll LCA(int u, int v) {
    Mu = Mv = -1;
    if(deg[u] > deg[v]) swap(u, v);
    int hu = deg[u], hv = deg[v];
    int tu = u, tv = v;
    for(int det = hv - hu, i = 0; det; det >>= 1, ++i)
        if(det & 1) { Mv = max(Mv, gx[tv][i]); tv = fa[tv][i];  }
    if(tu == tv) return Mv;
    for(int i = DEG - 1; i >= 0; --i) {
        if(fa[tu][i] == fa[tv][i]) continue;
        Mu = max(Mu, gx[tu][i]);
        Mv = max(Mv, gx[tv][i]);
        tu = fa[tu][i];
        tv = fa[tv][i];


    }
    return max(max(Mu, gx[tu][0]), max(Mv, gx[tv][0]));
}

int U[N], V[N], w[N], r[N], f[N];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool cmp(int a, int b) { return w[a] < w[b]; }
ll MST;
int n, m;
void mst() {

    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &U[i], &V[i], &w[i]);
        r[i] = i;
        f[i] = i;
    }
    sort(r + 1, r + m + 1, cmp);
    MST = 0;
    for(int i = 1; i <= m; ++i)
    {
        int id = r[i];
        int fu = find(U[id]);
        int fv = find(V[id]);
        if(fu != fv) {
            MST += w[id];
            f[ fu ] = fv;
            addedge(U[id], V[id], w[id]);
            addedge(V[id], U[id], w[id]);
        }
    }
}
int main() {
    init();
    mst();
    BFS(1);

    for(int i = 1; i <= m; ++i) {
        printf("%I64d\n", MST - LCA(U[i], V[i]) + w[i]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/orchidzjl/p/5062445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值