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 ...
随机推荐
- SQL 中单引号 和一些特殊字符的处理
为了防止程序SQL语句错误以及SQL注入,单引号必须经过处理.有2种办法: 1.使用参数,比如SELECT * FROM yourTable WHERE name = @name; 在Java中就是用 ...
- c#null值加法运算
加号都是一个含义啊,操作数不同,加号重载的方法就不一样,当加号的左边或右边含有字符串的时候,总是返回一个不为空的字符串.当加号左右两边都是数值的时候,就会对其进行数学运算,null+任何数都为null ...
- python练习题-简单方法判断三个数能否组成三角形
python简单方法判断三个数能否组成三角形 #encoding=utf-8 import math while True: str=raw_input("please input thre ...
- python webdriver 从无到有搭建数据驱动自动化测试框架的步骤和总结
一步一步搭建数据驱动测试框架的过程和总结 跟吴老学了搭建自动化数据驱动的框架后,我在自己练习的时候,尝试从简单的程序进行一点一点的扩展和优化,到实现这个数据驱动的框架. 先说一下搭建自动化测试框架的目 ...
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- 在线js调试工具JSbin、jsFiddle
在线js调试工具JSbin.jsFiddle JS Bin - Collaborative JavaScript Debugginghttp://jsbin.com/?html,output这个在线j ...
- 用CSS让字体在一行内显示不换行
青枫 , 2012/07/13 18:08 , css样式设计 , 评论(0) , 阅读(2189) , Via 本站原创 大 | 中 | 小 当一行文字超过DIV或者Table的宽度的时候,浏览器 ...
- selinux配置错误实例介绍
错误原因 配置关闭SELinux,结果误操作 应修改配置文件/etc/selinux/config中的“SELINUX”参数的值, # SELINUX=enforcing 原始配置 SELINUX= ...
- django session 使用案例
django session 使用案例 HTTP被设计为”无状态”,每次请求都处于相同的空间中. 在一次请求和下一次请求之间没有任何状态保持,我们无法根据请求的任何方面(IP地址,用户代理等)来识别来 ...
- 【打分策略】Elasticsearch打分策略详解与explain手把手计算
一.目的 一个搜索引擎使用的时候必定需要排序这个模块,一般情况下在不选择按照某一字段排序的情况下,都是按照打分的高低进行一个默认排序的,所以如果正式使用的话,必须对默认排序的打分策略有一个详细的了解才 ...