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 ...
随机推荐
- UBUNTU16.04 使用APT-GET如何设置代理
sudo apt-get install software-name -o Acquire::http::proxy="http://用户名:密码@代理服务器IP:代理服务器端口&quo ...
- cmd 笔记(随时补充)
被一篇破解WIFI的标题文骗到了,所以学习一下CMD的命令 1 查看已经连接的wifi和密码 netsh wlan show profiles 回车 netsh wlan show profiles ...
- webstorm使用心得
收藏夹功能:当工程目录很庞大时,有些子目录很经常打开,但层级又很深,这时候可以把目录添加到收藏夹里面,添加成功后,左侧有个“Favorites”菜单 面包屑导航:除了左侧的工程页面,可以选择目录之外, ...
- MySQL从删库到跑路_高级(四)——存储过程
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.存储过程简介 1.存储过程简介 存储过程是一组具有特定功能的SQl语句集组成的可编程的函数,经编译创建并保存在数 ...
- java接口对接——调用别人接口推送数据
实际开发中经常会遇到要和其他平台或系统对接的情况,实际操作就是互相调用别人的接口获取或者推送数据, 当我们调用别人接口推送数据时,需要对方给一个接口地址以及接口的规范文档,规范中要包括接口的明确入参及 ...
- WordPress存在DoS拒绝服务漏洞,推荐删除根目录下的xmlrpc.php
WordPress存在DoS拒绝服务漏洞,推荐删除根目录下的xmlrpc.php受影响的版本:WordPress 3.9.x-3.9.1WordPress 3.8.x-3.8.3WordPress 3 ...
- javaScript的内置对象以及一些常用的方法
前几天,我们学习了JavaScript的入门课程,但是要想做网站,仅仅学会入门是不够的,今后的几天,我将带领大家精通JavaScript,希望大家好好学习! JS内置对象 String对象:字符串对象 ...
- java笔试题三(javaWeb)
1.讲一下Servlet的执行过程,doGet和doPost的区别. 执行过程: 比如注解配置版本,先继承httpServlet,一旦发送get请求 调用,再执行post方法. doGet和doPos ...
- Python Web学习笔记之socket编程
Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...
- YAML配置文件
最近,研究jeeweb这个框架,发现新版本中的配置文件都是用的.yml为后缀的文件,打开一看,和以前的xml和properties语法有很大区别,因此仔细研究一下. 简介: YAML是(YAML Ai ...