Codeforces 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的更多相关文章
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- Codeforces 437D The Child and Zoo(并查集)
Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...
- 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 ...
- codeforces 437D The Child and Zoo
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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/ ...
- Codeforces 437 D. The Child and Zoo 并查集
题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...
- 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 ...
- Codeforces 437B The Child and Set
题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...
- cf437D The Child and Zoo
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- python直接赋值、浅拷贝和深拷贝
# 解: # import copy # names1=['Amir','Barry','Cgakes','Dao',[11,22,33]] # names2=names1#直接赋值,指向同一个对象 ...
- Javascript-逻辑或(||)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQL查询遍历数据方法一 [ 临时表 + While循环]
以下以SQL Server 2000中的NorthWind数据库中的Customers表为例, 用 临时表 + While循环 的方法, 对Customers表中的CompanyName列进行遍历 c ...
- mysql套接字文件
- Git—使用方法
1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...
- 网页中自适应的显示PDF
PDF格式呢,是一个高大的新式,如何在不同的浏览器中自适应显示,是一个值得研究的问题. 这里说明重点部分:获取浏览器宽高. IE中: document.body.clientWidth ==> ...
- java tar.gz文件生成
/** * 压缩文件成Gzip格式,Linux上可使用 * 压缩文件夹生成后缀名为".gz"的文件并下载 * @param folderPath,要压缩的文件夹的路径 * @par ...
- Python: 合并多个字典
现在有多个字典或者映射,想将它们从逻辑上合并为一个单一的映射后执行某些操作,比如查找值或者检查某些键是否存在. eg1: a = {'x': 1, 'z': 3 }b = {'y': 2, 'z': ...
- 数据仓库基础(十一)Informatica小技巧(2)
本文转载自:http://www.cnblogs.com/evencao/p/3152384.html 1.User shortcuts:shortcuts能实现快捷方式的复用.快捷方式可以根据源的变 ...
- python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名
python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名,如果在类中定义的就是类的私有成员. >>> dir(__builtin ...