zoukankan      html  css  js  c++  java
  • 【CH6201】走廊泼水节

    题目大意:给定一棵树,要求增加若干条边,将其转化为完全图,且该完全图以该树为唯一的最小生成树,求增加的边权最小是多少。

    题解:完全图的问题一般要考虑组合计数。重新跑一遍克鲁斯卡尔算法,每次并查集在合并时进行计数,因为要求最小生成树唯一,必须保证每条边都比当前连接两个联通块的边要至少大 1,因此每次合并对答案的贡献为 ((w+1)*(size[x]*size[y]-1))

    代码如下

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=6010;
    
    struct node{
    	int from,to,w;
    	bool operator<(const node& y)const{
    		return this->w<y.w;
    	}
    }e[maxn];
    int n,f[maxn],size[maxn];
    long long ans;
    
    int find(int x){
    	return x==f[x]?x:f[x]=find(f[x]);
    }
    
    
    void read_and_parse(){
    	scanf("%d",&n);
    	for(int i=1;i<n;i++)scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w);
    	for(int i=1;i<=n;i++)f[i]=i,size[i]=1;
    }
    
    void solve(){
    	sort(e+1,e+n);
    	for(int i=1;i<n;i++){
    		int x=find(e[i].from),y=find(e[i].to),z=e[i].w;
    		ans+=(long long)(size[x]*size[y]-1)*(z+1);
    		f[x]=y,size[y]+=size[x];
    	}
    	printf("%lld
    ",ans);
    }
    
    int main(){
    	int T;scanf("%d",&T);
    	while(T--){
    		ans=0;
    		read_and_parse();
    		solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    整型数字转utf8
    cmake构建时指定编译器架构(x86 or x64)
    tcp echo server libuv
    VS2015编译boost1.62
    android rom开发
    游戏昵称
    乐观锁和悲观锁
    数据库锁机制
    MySQL事务实现原理
    MySQL事务
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10022792.html
Copyright © 2011-2022 走看看