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. Struts2-综合项目

    综合项目:视频后台管理系统 开发环境:Tomcat6(服务器)+jdk6(windows操作系统) 使用技术:struts2(后台)+jsp(前台显示)+ajax(信息传递)+json(服务器响应前台 ...

  2. angular前端框架

    总所周知,在前端开发中,大家用的比较多的框架就是angular,vue,react等,今天就为大家讲一下angular大家框架的原理及运用 1.本次所举的例子是以依赖require.js的, < ...

  3. UVM中factory机制的使用

    UVM中的factory机制一般用在sequence的重载,尤其是virtual sequence.当Test_case变化时,通过virtual sequence的重载,可以很容易构建新的测试. 因 ...

  4. FRM-40212: Invalid value for field %s.

    Cause:        Caused by one of the following: 1.  The value is not of the proper data type. 字段类型不对 2 ...

  5. 20155334 2016-2017-2 《Java程序设计》第八周学习总结

    20155334 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章:NIO与NIO2 NIO的定义: InputStream.OutputStream ...

  6. jquery-easyui combobox combogrid 级联不可编辑实例

    jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...

  7. python的os模块和sys模块

    os模块 os.getcwd()  获取当前的工作目录 os.chdir('绝对路径/相对于当前工作目录的路径')  改变工作目录,相当于shell的cd命令,例如Windows平台下os.chdir ...

  8. scrapy运行方式

    1,在cmd 命令行下执行 scrapy crawl  demo (爬虫主逻辑的 name= 'demo '的名字) 2, 也可以在spider目录下添加一个py文件,加入以下代码 from scra ...

  9. python监控端口脚本[jkport2.0.py]

    #!/usr/bin/env python #!coding=utf-8 import os import time import sys import smtplib from email.mime ...

  10. 20145335郝昊《网络对抗》Exp 8 Web基础

    20145335郝昊<网络对抗>Exp 8 Web基础 实验内容 本实践的具体要求有: Web前端HTML:能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法, ...