[APC001] D Forest
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的更多相关文章
- 基于netty轻量的高性能分布式RPC服务框架forest<下篇>
基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...
- 基于netty轻量的高性能分布式RPC服务框架forest<上篇>
工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...
- [Machine Learning & Algorithm] 随机森林(Random Forest)
1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...
- STL : map函数的运用 --- hdu 4941 : Magical Forest
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 异常检测算法--Isolation Forest
南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- POJ 1873 - The Fortified Forest 凸包 + 搜索 模板
通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...
随机推荐
- Python3实战系列之九(获取印度售后数据项目)
项目现状:已经部署在服务器上并正常运行了. 1.服务器上的部署 2.下载到服务器的文件列表 3.转存在到数据库SQL Server中的数据 项目总结:这次项目采用python来实现,刚开始还是有点担忧 ...
- 重写comparater比较器
TreeSet 能自动对对象进行排序,但是一般需要在类中重写比较方法 比较器介绍 在类中实现Comparable接口,并重写compareTo方法 排序对象类 package typing.overr ...
- thymeleaf学习笔记:总结
Thymeleaf定义:Thymeleaf is a modern server-side Java template engine for both web and standalone envir ...
- python任意进制转换
python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...
- 【Mysql】事务的四种特性和隔离级别
四种特性: 原子性(Atomicity):事务里所有操作视为一个整理,要么全部完成,要么全回滚. 一致性(Consistency):操作前后,数据库内数据逻辑上一致.比如:1w元转账给不同的人,转出去 ...
- centos中单进程监控
[root@k8s6 proc]# ps aux|grep -v PID|sort -rn -k +|head - root ? Ssl : : /usr/bin/dockerd root ? Ssl ...
- HDU 4309 Seikimatsu Occult Tonneru (状压 + 网络流)
题意:输入 n 个城市 m 条边,但是边有三种有向边 a b c d,第一种是 d 是 0,那么就是一条普通的路,可以通过无穷多人,如果 d < 0,那么就是隧道,这个隧道是可以藏 c 个人, ...
- Objective-C优缺点
优点: 1:Category,使用category可以在不改变原来类的同时为类增加新的方法或者重写原来类的方法实现(使用runtime方法还可以在分类中实现方法交换和添加属性操作) 2:运行时 动态识 ...
- 使用FFmpeg如何转发一个RTSP视频流
版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/6930221.html 转发RTSP流,这类需求一般出现于转发一些摄像头采集视频,并在摄像头上做RTSP ...
- jQuery应用实例4:下拉列表
应用场景:左侧是已有商品,右侧是未有商品,选择其中的内容点击箭头即可互换: 点击大箭头则全部内容去另一边,或者双击已有商品的选项也会加入右边: 代码实现: <!DOCTYPE html> ...