Codeforces 437 D. The Child and Zoo 并查集
题目链接:D. The Child and Zoo
题意:
题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边。每条边的权值为该条路连接的两个区中权值较小的一个。如果两个区没有直接连接,那么f值即为从一个区走到另一个区中所经过的路中权值最小的值做为权值。如果有多条路的话,要取最大的值作为路径的长度。问,平均两个区之间移动的权值为多少。
题解:
每条边的长度已经知道了,因为路径的权值取这条路中最小的且多条路的话则取较大的,那么我们其实可以从比较大的边开始取(先把边排序),用并查集开始合并边。这样前面的操作就不会影响后面的操作。每合并两个区间就将答案加上两个点的数量的乘积与当前这条边的权值的乘积。
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+;
struct node
{
long long f,t,v;
};
node edge[MAX_N];
bool cmp(const node a,const node b)
{
return a.v > b.v;
}
long long ans = ;
int fat[MAX_N];
long long val[MAX_N];
long long num[MAX_N];
vector<int> vec[MAX_N];
int Find(int x)
{
if(fat[x] == x)
{
return x;
}
int y = Find(fat[x]);
return fat[x] = y;
}
void mix(int x,int y,int v)
{
int fx = Find(x);
int fy = Find(y);
if(fx == fy) return;
fat[fx] = fy;
ans += num[fx]*num[fy]*v;
num[fy] += num[fx];
//cout<<num[fx]<<"....."<<num[fy]<<"...."<<ans<<endl;
}
int main()
{
int N,M,T;
while(cin>>N>>M)
{
ans = ;
for(int i=;i<N;i++) vec[i].clear();
for(int i=;i<=N;i++)
{
scanf("%lld",&val[i]);
fat[i] = i;
num[i] = ;
}
for(int i=;i<M;i++)
{
scanf("%lld%lld",&edge[i].f,&edge[i].t);
edge[i].v = min(val[edge[i].f],val[edge[i].t]);
}
sort(edge,edge+M,cmp);
for(int i=;i<M;i++)
{
mix(edge[i].f,edge[i].t,edge[i].v);
}
printf("%.6lf\n",2.0*ans/(N*1.0*(N-)));
}
return ;
}
Codeforces 437 D. The Child and Zoo 并查集的更多相关文章
- 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 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 ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Beta Round #5 E. Bindian Signalizing 并查集
E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
随机推荐
- 房上的猫:switch选择结构,与选择结构总结
switch选择结构: 一.定义: switch选择结构,可以方便地解决等值判断问题二.语法: switch(表达式){ case 常量1: //代码块1; break; c ...
- HY.Mail:C#简单、易用的邮件工具库
一.开发HY.Mail的初衷 Nuget或者github上有很多成熟且优秀的邮件库可以使用, 但是目前找到的使用都不够简洁或者不适合我的使用场景 我的场景是开发应用场景(例如系统通知.运维通知),而非 ...
- nvm 装 nodejs 重启终端失效的解决方法
(1) 安装 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash (2) ...
- Netty入门之客户端与服务端通信(二)
Netty入门之客户端与服务端通信(二) 一.简介 在上一篇博文中笔者写了关于Netty入门级的Hello World程序.书接上回,本博文是关于客户端与服务端的通信,感觉也没什么好说的了,直接上代码 ...
- css布局--水平垂直居中
1. 使用text-align 和 vertical-align 和 inline-block实现水平垂直居中 html <div class="parent"> &l ...
- Java学习笔记5(类的入门以及ArrayList)
1.类的概念:将现实生活中的事物抽象成了代码(类),我们可以使用自定义的数组类型(类)来描述现实生活中的事物. 2.分析:用一部手机来分析,手机可以打电话,上网,听音乐,这些就是方法,手机有型号,颜色 ...
- CSS属性:定位属性(图文详解)
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. CSS的定位属性有三种,分别是绝对定位.相对定位.固定定位. posit ...
- python与 Ajax跨域请求
同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...
- Java-反射初级知识掌握
PS:本文就Java基础需要掌握的反射知识做下汇总和分析. Java-reflect:java反射,对应我们研究框架.底层框架起到基本的知识储备. Class类Java的世界类,万事皆对象,所有的类都 ...
- 算法实现之python篇
Python source code: gradient_boosting_regression.py from sklearn import ensemblefrom sklearn.metrics ...