CodeForces 609E :Minimum spanning tree for each edge 最小生成树 + 树链剖分

传送门

题意

给你 n n n个点, m m m条边,如果对于一个最小生成树中要求必须包括第 i ( 1 < = i < = m ) i(1<=i<=m) i(1<=i<=m)条边,那么最小生成树的权值总和最小是多少。

分析

首先构造最小生成树,然后将树边标记建图,树链剖分,维护边权的最大值
如果第 i i i条边在最小生成树中,直接输出最小生成树的值
如果第 i i i条边不在最小生成树中,那么考虑将第 i i i条边直接加入最小生成树中,那么就形成一个环,然后找到环内最大的边权减去即可,等价于求 e d g e [ i ] . u edge[i].u edge[i].u e d g e [ i ] . v edge[i].v edge[i].v树上简单路径上的最大值,树链剖分就可以解决

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Edge{
    int u,v,w;
    int id;
    bool st;
}edge[M];
int q[N],n,m,cnt;
int h[N],ne[M],e[M],w[M],idx;
int id[N], son[N], sz[N], fa[N], dep[N], top[N], son_w[N];
int nw[N];
ll ans[N];

struct Node{
    int l,r;
    int res;
}tr[N * 4];

void add(int x,int y,int z){
    ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}

bool cmp(Edge A,Edge B){
    return A.w < B.w;
}

int find(int x){
    if(x != q[x]) q[x] = find(q[x]);
    return q[x];
}

void dfs(int u, int f, int d) {
    sz[u] = 1, son[u] = 0, fa[u] = f, dep[u] = d;
    for (int i  = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == f) continue;
        dfs(j, u, d + 1);
        sz[u] += sz[j];
        if (sz[j] > sz[son[u]]) {
            son[u] = j;
            son_w[u] = w[i];
        }

    }
}

void dfs_(int u, int t, int res) {
    // cout << u << ' ' << res << endl;
    id[u] = ++cnt, nw[cnt] = res, top[u] = t;
    if (!son[u]) return;
    dfs_(son[u], t, son_w[u]);
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa[u]) continue;
        if (j == son[u]) continue;
        else dfs_(j, j, w[i]);
    }
}

void build(int u,int l,int r){
    tr[u].l = l,tr[u].r = r;
    if(l == r){
        // cout << nw[l] << endl;
        tr[u].res = nw[l];
        return;
    }
    int mid = l + r >> 1;
    build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
    tr[u].res = max(tr[u << 1].res,tr[u << 1 | 1].res);
}

int query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].res;
    int mid = (tr[u].l + tr[u].r) >> 1;
    int res = -0x3f3f3f3f;
    if (l <= mid) res = query(u << 1, l, r);
    if (r > mid) res = max(res, query(u << 1 | 1, l, r));
    return res;
}

int query_path(int l, int r) {
    int res = -0x3f3f3f3f;
    while (top[l] != top[r]) {
        if (dep[top[l]] < dep[top[r]]) swap(l, r);
        res = max(query(1, id[top[l]], id[l]), res);
        l = fa[top[l]];
    }
    if (dep[l] > dep[r]) swap(l, r);
    res = max(res, query(1, id[son[l]], id[r]));
    return res;
}

int main() {
    read(n),read(m);
    for(int i = 1;i <= n;i++) h[i] = -1,q[i] = i;
    for(int i = 1;i <= m;i++) read(edge[i].u),read(edge[i].v),read(edge[i].w),edge[i].st = false,edge[i].id = i;
    sort(edge + 1,edge + 1 + m,cmp);  
    ll res = 0;
    for(int i = 1;i <= m;i++){
        int x = find(edge[i].u),y = find(edge[i].v);
        if(x != y){
            q[x] = y;
            edge[i].st = true;
            add(edge[i].u,edge[i].v,edge[i].w);
            add(edge[i].v,edge[i].u,edge[i].w);
            
            res = res + 1ll * edge[i].w;
        }
    }
    dfs(1, -1, 1);
    dfs_(1, 1, 0);
    build(1, 1, n);  
    for(int i = 1;i <= m;i++){
        if(edge[i].st){
            ans[edge[i].id] = res;
            continue;
        }
        int x = query_path(edge[i].u,edge[i].v);
        ans[edge[i].id] = res + 1ll * edge[i].w - 1ll * x;
        // cout << edge[i].u << ' ' << edge[i].v << ' ' << ans[edge[i].id] << ' ' << edge[i].w << endl;
    }
    // puts("");
    for(int i = 1;i <= m;i++) dl(ans[i]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值