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

  1. #include<set>
  2. #include<map>
  3. #include<cmath>
  4. #include<queue>
  5. #include<cctype>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<cstring>
  9. #include<iostream>
  10. #include<algorithm>
  11. using std::min;
  12. using std::max;
  13. using std::swap;
  14. using std::vector;
  15. const int N=1e5+5;
  16. typedef double db;
  17. typedef long long ll;
  18. #define pb(A) push_back(A)
  19. #define pii std::pair<ll,int>
  20. #define mp(A,B) std::make_pair(A,B)
  21. pii val[N];
  22. int n,m,used[N];
  23. int cnt,head[N],vis[N];
  24. struct Edge{
  25. int to,nxt;
  26. }edge[N<<1];
  27. void add(int x,int y){
  28. edge[++cnt].to=y;
  29. edge[cnt].nxt=head[x];
  30. head[x]=cnt;
  31. }
  32. ll getint(){
  33. ll X=0,w=0;char ch=0;
  34. while(!isdigit(ch))w|=ch=='-',ch=getchar();
  35. while( isdigit(ch))X=X*10+ch-48,ch=getchar();
  36. if(w) return -X;return X;
  37. }
  38. std::priority_queue< pii > pq[N];
  39. void dfs(int now,int tot){
  40. vis[now]=tot;
  41. pq[tot].push(mp(-val[now].first,now));
  42. for(int i=head[now];i;i=edge[i].nxt){
  43. int to=edge[i].to;
  44. if(vis[to]) continue;
  45. dfs(to,tot);
  46. }
  47. }
  48. signed main(){
  49. n=getint(),m=getint();
  50. for(int i=1;i<=n;i++) val[i].first=getint(),val[i].second=i;
  51. for(int i=1;i<=m;i++){
  52. int x=getint()+1,y=getint()+1;
  53. add(x,y);add(y,x);
  54. } int tot=0;
  55. for(int i=1;i<=n;i++) if(!vis[i]) dfs(i,++tot);
  56. if(tot==1) return printf("0"),0;
  57. if(2*tot-2>n) return printf("Impossible"),0;
  58. ll ans=0;
  59. for(int i=1;i<=tot;i++){
  60. ans+=-pq[i].top().first;
  61. used[pq[i].top().second]=1;
  62. } std::sort(val+1,val+1+n);
  63. int cnts=0;
  64. for(int i=1;i<=n;i++){
  65. if(cnts==tot-2) break;
  66. while(i<=n and used[val[i].second]) i++;
  67. ans+=val[i].first;cnts++;
  68. } printf("%lld\n",ans);
  69. return 0;
  70. }

[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. django by example 第四章 dashboard处html无法渲染问题

    描述: 实现django by example 代码时,第四章 dashboard处html无法渲染问题. 此时报错,NoReverseMatch at /account/login/, Error ...

  2. java多线程系列12 ConcurrentHashMap CopyOnWriteArrayList 简介

    我们知道 ,hashmap 和 arraylist 是线程不安全的 在多线程环境下有数据安全问题, 当然 我们可以通过Collections的一些方法把他们变成线程安全的, Collections.s ...

  3. AX_SysExcel

    void KTL_CPeng_ImportCustStamp()  {      str                 file;      FileNameFilter      filter = ...

  4. JS 的execCommand 方法 做的一个简单富文本

    execCommand 当一个 HTML 文档切换到设计模式(designMode)时,文档对象暴露 execCommand 方法,该方法允许运行命令来操纵可编辑区域的内容.大多数命令影响文档的选择( ...

  5. $q的基本用法

    angularjs的http是异步的没有同步,一般都会遇到一个场景,会把异步请求的参数作为条件执行下一个函数,之前一直在看其他人的博客理论太多看了很久才看懂 http({ method:'post', ...

  6. MySQL 基础--字符类型

    ##=====================================================================================## MySQL支持的字符 ...

  7. HTML中的文本标签

    <span></span> 请使用 <span> 来组合行内元素,以便通过样式来格式化它们. 注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上 ...

  8. 纯CSS下拉菜单(希望对有需要的小伙伴有所帮助)

    效果截图(颜色有点丑,请无视): <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  9. SQL-1--语句分类

  10. 解决SpringBoot jar包太大的问题

    转载 2017年09月18日 09:21:53 577 SpringBoot的web应用一般都添加了spring-boot-maven-plugin插件. Maven xml代码   <buil ...