Description

给定\(n\)个点\(m\)条边组成的森林,每个点有权值\(a_i\)。现在需要将森林连成一棵树,选择两个点\(i,j\)连边的代价是\(a_i+a_j\),每个点最多被选择连边一次。问最小代价。

Solution

首先dfs找出图里联通块个数记为\(x\)。

因为要连成一棵树,所以要连\(x-1\)条边,所以要选择\(2x-2\)个点。

我们不是很需要关心最后连接成的树的形态。贪心的选出代价最小的\(2x-2\)个点就好。

现在每个联通块内选出\(1\)个权值最小的点,这样保证了每个联通块都有点和其他联通块相连。

然后剩下的\(x-2\)个点随便选,选择没有被选过且\(a\)最小的点就好了。

Code

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using std::min;
using std::max;
using std::swap;
using std::vector;
const int N=1e5+5;
typedef double db;
typedef long long ll;
#define pb(A) push_back(A)
#define pii std::pair<ll,int>
#define mp(A,B) std::make_pair(A,B) pii val[N];
int n,m,used[N];
int cnt,head[N],vis[N]; struct Edge{
int to,nxt;
}edge[N<<1]; void add(int x,int y){
edge[++cnt].to=y;
edge[cnt].nxt=head[x];
head[x]=cnt;
} ll getint(){
ll X=0,w=0;char ch=0;
while(!isdigit(ch))w|=ch=='-',ch=getchar();
while( isdigit(ch))X=X*10+ch-48,ch=getchar();
if(w) return -X;return X;
}
std::priority_queue< pii > pq[N];
void dfs(int now,int tot){
vis[now]=tot;
pq[tot].push(mp(-val[now].first,now));
for(int i=head[now];i;i=edge[i].nxt){
int to=edge[i].to;
if(vis[to]) continue;
dfs(to,tot);
}
} signed main(){
n=getint(),m=getint();
for(int i=1;i<=n;i++) val[i].first=getint(),val[i].second=i;
for(int i=1;i<=m;i++){
int x=getint()+1,y=getint()+1;
add(x,y);add(y,x);
} int tot=0;
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i,++tot);
if(tot==1) return printf("0"),0;
if(2*tot-2>n) return printf("Impossible"),0;
ll ans=0;
for(int i=1;i<=tot;i++){
ans+=-pq[i].top().first;
used[pq[i].top().second]=1;
} std::sort(val+1,val+1+n);
int cnts=0;
for(int i=1;i<=n;i++){
if(cnts==tot-2) break;
while(i<=n and used[val[i].second]) i++;
ans+=val[i].first;cnts++;
} printf("%lld\n",ans);
return 0;
}

[APC001] D Forest的更多相关文章

  1. 基于netty轻量的高性能分布式RPC服务框架forest<下篇>

    基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...

  2. 基于netty轻量的高性能分布式RPC服务框架forest<上篇>

    工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...

  3. [Machine Learning & Algorithm] 随机森林(Random Forest)

    1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...

  4. STL : map函数的运用 --- hdu 4941 : Magical Forest

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. 异常检测算法--Isolation Forest

    南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...

  6. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  7. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  8. POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

    通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...

  9. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest

    题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...

随机推荐

  1. ORA-12514: TNS:监听程序当前无法识别连接描述符中请

    若Oracle出现“监听程序当前无法识别连接描述符中请求的服务”这个错误可以按照以下方法解决: 可以通过这个路径找到一个文本文件: oracle\product\10.2.0\db_1\NETWORK ...

  2. nodeJS实现一个在线填表应用

    1.构建一个web服务器 以前玩php和jsp时用过Apache.汤姆猫服务器,nodejs则有不同,他是需要自己createServer. //server.jsvar http = require ...

  3. BeanUtil拷贝

    拷贝vo对象 一些查询到的数据很多是不需要的,可以创建vo对象,对需要的对象属性进行拷贝 maven依赖 <dependency> <groupId>org.projectlo ...

  4. windows 函数

    数组清零 ::ZeroMemory(首地址,大小);

  5. python 复数类型

    1 Python中的算数虚数不能单独存在,它们总是和一个值为0.0的实数部分一起构成一个复数表示虚数的语法:real+imagj实数部分和虚数部分都是浮点数虚数部分必须有j或J下面是些得数:64.23 ...

  6. 1.2万事开头hello world+交互+getpass、sys模块初识

    1.python的hello world: ①运行cmd-输入python-输入print (“hello world!”) ②创造.py的文本helloworld.py(后缀是为了告诉其他人)-输入 ...

  7. post网络请求坑

    微信小程序开发中网络请求必不可少.GET.POST请求是最常用的.GET请求 POST请求的时候有好几个坑.我已经为大家填好了.

  8. kali安装配置ftp

    参考:https://zhidao.baidu.com/question/1511146077646448900.html 一)安装 1.用sudo apt-get install 下载安装包

  9. docker知识点杂记

    最近重新梳理了一下docker的一些基本知识,简单做了下总结.以后会逐步补充. 1.查看系统内核版本,需要3.10以上 $ uname -r 2.下载docker 说明:centos安装docker比 ...

  10. 使用tinymce富文本

    1.tinymce入门参考 https://www.tiny.cloud/docs/general-configuration-guide/basic-setup/ 2.tinymce安装选项 htt ...