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 ...
随机推荐
- JavaScript 计算指定月份有多少天
用 js 画工作日历的时候,需要用 js 计算指定月份一共有多少天 在网上找了些方法,都比较繁琐,后来灵机一动,想到一个偷懒的办法,分享一下 一.原理分析 要想得到某月有多少天,只需要获取到当月最后一 ...
- web打印总结
一.打印样式 区别显示和打印的样式 使用media="print"控制打印时的样式,如下: 打印时不显示打印按钮,设置页面宽度 <style media="prin ...
- Nginx集群之SSL证书的WebApi微服务
目录 1 大概思路... 1 2 Nginx集群之SSL证书的WebApi微服务... 1 3 HTTP与HTTPS(SSL协议)... 1 4 Ope ...
- jQuery 遍历函数(八)
函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个元素的所有子元素. .closes ...
- jQuery 属性操作方法(五)
方法 描述 addClass() 向匹配的元素添加指定的类名. attr() 设置或返回匹配元素的属性和值. hasClass() 检查匹配的元素是否拥有指定的类. html() 设置或返回匹配的元素 ...
- 基于div表单模拟右对齐
基于div表单模拟右对齐 --------------------------------------------------------- ----------------------------- ...
- 机器学习小记——KNN(K近邻) ^_^ (一)
为了让绝大多数人都可以看懂,所以我就用简单的话语来讲解机器学习每一个算法 第一次写ML的博文,所以可能会有些地方出错,欢迎各位大佬提出意见或错误 祝大家开心进步每一天- 博文代码全部为python 简 ...
- 【转】sublime text 3 显示空格和Tab
因为sublime text3确实太好用了所以也用它写代码了,可是在Python3第一步把preferences.sublime-setting-Default里面的"draw_white_ ...
- 【矩阵快速幂】bzoj1297 [SCOI2009]迷路
1297: [SCOI2009]迷路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1407 Solved: 1007[Submit][Status ...
- BaseServer的介绍
服务器类型 5种类型:BaseServer,TCPServer,UnixStreamServer,UDPServer,UnixDatagramServer.注意:BaseServer不直接对外服务. ...