Codeforces 609E Minimum spanning tree for each edge【MST + LCA倍增】

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。



思路:先求MST,在求的过程中建树,记录代价为ans。对每条边<u, v>权值记录为c,把它加进MST里面的代价就需要去掉u->lca(u, v)->v路径上权值最大的边记录权值为Max。那么结果就是ans - Max + c。


AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
int N, M;
struct LCA
{
    struct Edge{
        int from, to, val, next;
    };
    Edge edge[MAXM];
    int head[MAXN], edgenum;
    int depth[MAXN], fa[MAXN][20], Mx[MAXN][20];
    void init(){
        edgenum = 0;
        CLR(head, -1);
    }
    void addEdge(int u, int v, int w)
    {
        Edge E = {u, v, w, head[u]};
        edge[edgenum] = E;
        head[u] = edgenum++;
    }
    void DFS(int u, int f, int d)
    {
        depth[u] = d;  fa[u][0] = f;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(v == f)
            {
                Mx[u][0] = edge[i].val;
                continue;
            }
            DFS(v, u, d+1);
        }
    }
    void LCA_init()
    {
        DFS(1, -1, 0);
        for(int i = 1; i < 20; i++)
        {
            for(int j = 1; j <= N; j++)
            {
                fa[j][i] = fa[fa[j][i-1]][i-1];
                Mx[j][i] = max(Mx[j][i-1], Mx[fa[j][i-1]][i-1]);
            }
        }
    }
    int Get_path_max(int u, int v)
    {
        int ans = 0;
        while(depth[u] != depth[v])
        {
            if(depth[u] < depth[v])
                swap(u, v);
            int d = depth[u] - depth[v];
            for(int i = 0; i < 20; i++)
            {
                if(d >> i & 1)
                {
                    ans = max(ans, Mx[u][i]);
                    u = fa[u][i];
                }
            }
        }
        if(u == v)
            return ans;
        for(int i = 19; i >= 0; i--)
        {
            if(fa[u][i] != fa[v][i])
            {
                ans = max(ans, Mx[u][i]);
                ans = max(ans, Mx[v][i]);
                u = fa[u][i];
                v = fa[v][i];
            }
        }
        return max(ans, max(Mx[u][0], Mx[v][0]));
    }
};
LCA L;
struct Union
{
    int pre[MAXN];
    void init()
    {
        for(int i = 1; i <= N; i++)
            pre[i] = i;
    }
    int Find(int p)
    {
        int child = p;
        while(p != pre[p])
            p = pre[p];
        while(child != p)
        {
            int t = pre[child];
            pre[child] = p;
            child = t;
        }
        return p;
    }
};
Union U;
struct Node{
    int u, v, w;
};
Node num[MAXM];
bool cmp(Node a, Node b){
    return a.w < b.w;
}
int a[MAXN], b[MAXN], c[MAXN];
int main()
{
    Ri(N); Ri(M);
    U.init(); L.init();
    int top = 0;
    for(int i = 1; i <= M; i++)
    {
        Ri(a[i]); Ri(b[i]); Ri(c[i]);
        num[top].u = a[i]; num[top].v = b[i]; num[top++].w = c[i];
    }
    sort(num, num+top, cmp);
    LL ans = 0;
    for(int i = 0; i < top; i++)
    {
        int u = U.Find(num[i].u);
        int v = U.Find(num[i].v);
        if(u != v)
        {
            L.addEdge(num[i].u, num[i].v, num[i].w);
            L.addEdge(num[i].v, num[i].u, num[i].w);
            ans += num[i].w;
            U.pre[u] = v;
        }
    }
    L.LCA_init();
    for(int i = 1; i <= M; i++)
    {
        int Max = L.Get_path_max(a[i], b[i]);
        Pl(ans - Max + c[i]);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值