D - The Child and Zoo

思路:

并查集+贪心

每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序

然后用并查集从大的边开始合并,因为你要合并的这两个联通块之间的点肯定要经过这条边,而这条要合并的边是所有已经合并中的最小的,所以两个联通块之间的所有点之间的f就是这条边(而且是所有情况最大的,因为是从最大的边开始贪心的)。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define mem(a,b) memset(a,b,sizeof(a))
  6.  
  7. const int N=1e5+;
  8. struct edge{
  9. int u,v,w;
  10. bool operator < (edge t){
  11. return w>t.w;
  12. }
  13. }edge[N];
  14. int cnt[N];
  15. int rnk[N];
  16. int par[N];
  17. int a[N];
  18. void init(int n){
  19. for(int i=;i<=n;i++)par[i]=i,cnt[i]=;
  20. }
  21. int find(int x){
  22. if(x==par[x])return x;
  23. else return par[x]=find(par[x]);
  24. }
  25. void unite(int x,int y){
  26. int px=find(x);
  27. int py=find(y);
  28. if(px!=py){
  29. if(rnk[px]<rnk[py]){
  30. par[px]=py;
  31. cnt[py]+=cnt[px];
  32. }
  33. else{
  34. if(rnk[px]==rnk[py]){
  35. rnk[px]++;
  36. }
  37. par[py]=px;
  38. cnt[px]+=cnt[py];
  39. }
  40. }
  41. }
  42. int main(){
  43. ios::sync_with_stdio(false);
  44. cin.tie();
  45. int n,m,u,v;
  46. cin>>n>>m;
  47. for(int i=;i<=n;i++)cin>>a[i];
  48. int c=;
  49. while(m--){
  50. cin>>u>>v;
  51. edge[c].u=u;
  52. edge[c].v=v;
  53. edge[c++].w=min(a[u],a[v]);
  54. }
  55. sort(edge,edge+c);
  56. init(n);
  57. ll ans=;
  58. for(int i=;i<c;i++){
  59. int pu=find(edge[i].u);
  60. int pv=find(edge[i].v);
  61. if(pu!=pv){
  62. ans+=(ll)edge[i].w*cnt[pu]*cnt[pv];
  63. unite(edge[i].u,edge[i].v);
  64. }
  65. }
  66. cout<<fixed<<setprecision()<<ans*2.0/(1.0*(n-)*n)<<endl;
  67. return ;
  68. }

Codeforces D - The Child and Zoo的更多相关文章

  1. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

  2. Codeforces 437D The Child and Zoo(并查集)

    Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...

  3. Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树

    Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...

  4. codeforces 437D The Child and Zoo

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集

    B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  6. Codeforces 437 D. The Child and Zoo 并查集

    题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...

  7. Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces 437B The Child and Set

    题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...

  9. cf437D The Child and Zoo

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. tfs项目解绑及svn上传

    1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹

  2. python解决matplotlib中文坐标值乱码的问题

    加上这句话即可 plt.rcParams['font.sans-serif']=['SimHei'] 效果:

  3. 使用基于 PHP 的开源软件 YOURLS 搭建短链接地址服务

    使用基于 PHP 的开源软件 YOURLS搭建 系统配置 php7.1+mysql5.7+nginx 下载源代码 git clone https://github.com/YOURLS/YOURLS. ...

  4. [Java]接受拖拽文件的窗口

    至于这个问题,Java的awt.dnd包下提供了许多完成这一功能的类 例如DropTarget.DropTargetListener等 先来讲一下DropTarget类,这个类完成和拖拽.复制文件等操 ...

  5. git分支 远程协作

    创建文件mkdir ### cd ### git init 初始化 git config global user.name “username” git config global user.emia ...

  6. Python3基础 函数 无参数无返回值 调用会输出hello world的函数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Django框架(四) Django之视图层

    视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . ...

  8. 权限管理,pymysql模块

    权限管理 权限管理重点 MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL ...

  9. sql逻辑查询语句的执行顺序

    SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN ...

  10. thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?

    为什么会产生这样的 路径 常量等 的 困扰? 是由于 在tp中, 使用了多种形式的常量导致的, 比如, 有php语言本身的 "魔术常量", 有 php函数, 比如dirname定义 ...