D - The Child and Zoo

思路:

并查集+贪心

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

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

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
struct edge{
int u,v,w;
bool operator < (edge t){
return w>t.w;
}
}edge[N];
int cnt[N];
int rnk[N];
int par[N];
int a[N];
void init(int n){
for(int i=;i<=n;i++)par[i]=i,cnt[i]=;
}
int find(int x){
if(x==par[x])return x;
else return par[x]=find(par[x]);
}
void unite(int x,int y){
int px=find(x);
int py=find(y);
if(px!=py){
if(rnk[px]<rnk[py]){
par[px]=py;
cnt[py]+=cnt[px];
}
else{
if(rnk[px]==rnk[py]){
rnk[px]++;
}
par[py]=px;
cnt[px]+=cnt[py];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,m,u,v;
cin>>n>>m;
for(int i=;i<=n;i++)cin>>a[i];
int c=;
while(m--){
cin>>u>>v;
edge[c].u=u;
edge[c].v=v;
edge[c++].w=min(a[u],a[v]);
}
sort(edge,edge+c);
init(n);
ll ans=;
for(int i=;i<c;i++){
int pu=find(edge[i].u);
int pv=find(edge[i].v);
if(pu!=pv){
ans+=(ll)edge[i].w*cnt[pu]*cnt[pv];
unite(edge[i].u,edge[i].v);
}
}
cout<<fixed<<setprecision()<<ans*2.0/(1.0*(n-)*n)<<endl;
return ;
}

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. mysql中delete和truncate区别

    delete和truncate区别如下: 一.灵活性:delete可以条件删除数据,而truncate只能删除表的所有数据: delete from table_test where ... trun ...

  2. tensorflow显存管理

    在运行上面的blog的Tensorflow小程序的时候程序我们会遇到一个问题,当然这个问题不影响我们实际的结果计算,但是会给同样使用这台计算机的人带来麻烦,程序会自动调用所有能调用到的资源,并且全占满 ...

  3. Postman: Pre-request Script,设置body 变量

    1)Postman Pre-request Script 设置变量名 2)把变量放在body里 3)Send后查看变量是否被替换

  4. Linux基础命令---cat

    cat 连接文本文件或者标准输入,将结果输出到标准输出设备. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法       c ...

  5. MySQL数据库----完整性约束

    一.介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  6. 各种语言中的可变参数(java、python、c++、javascript)

    索引: java python c++ js 1.Java public class Animal { // 接受可变参数的方法 void eat(String... Objects) { for ( ...

  7. python的re正则表达式模块

    元字符  .   *   +   ?   ^   $   { }     [ ]     -     \ .  匹配除了/n之外的任意一个字符 * 匹配*前面的单个字符任意次,即[0,+∞] + 匹配 ...

  8. label语句、break语句和continue语句

    label语句 可以在代码中添加标签,以便使用.以下是label语句的语法: label:statement 示例: start: for (var i = 0; i < count; i++) ...

  9. Java的各种加密算法

    Java的各种加密算法 JAVA中为我们提供了丰富的加密技术,可以基本的分为单向加密和非对称加密 1.单向加密算法 单向加密算法主要用来验证数据传输的过程中,是否被篡改过. BASE64 严格地说,属 ...

  10. Python3基础 os listdir 列举指定的所有文件及文件夹的名字

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