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 ...
随机推荐
- tfs项目解绑及svn上传
1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹
- python解决matplotlib中文坐标值乱码的问题
加上这句话即可 plt.rcParams['font.sans-serif']=['SimHei'] 效果:
- 使用基于 PHP 的开源软件 YOURLS 搭建短链接地址服务
使用基于 PHP 的开源软件 YOURLS搭建 系统配置 php7.1+mysql5.7+nginx 下载源代码 git clone https://github.com/YOURLS/YOURLS. ...
- [Java]接受拖拽文件的窗口
至于这个问题,Java的awt.dnd包下提供了许多完成这一功能的类 例如DropTarget.DropTargetListener等 先来讲一下DropTarget类,这个类完成和拖拽.复制文件等操 ...
- git分支 远程协作
创建文件mkdir ### cd ### git init 初始化 git config global user.name “username” git config global user.emia ...
- Python3基础 函数 无参数无返回值 调用会输出hello world的函数
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Django框架(四) Django之视图层
视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . ...
- 权限管理,pymysql模块
权限管理 权限管理重点 MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL ...
- sql逻辑查询语句的执行顺序
SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN ...
- thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?
为什么会产生这样的 路径 常量等 的 困扰? 是由于 在tp中, 使用了多种形式的常量导致的, 比如, 有php语言本身的 "魔术常量", 有 php函数, 比如dirname定义 ...