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 ...
随机推荐
- CSS开发规范
虽然很久之前整理过一份简单的CSS规范,但是当时写的也不是很全面,有些细节也没有照顾到.记录一份较详细的版本,以备不时之需. 命名规范 [强制] class一律使用小写字母+下划线格式命名 例: cl ...
- linux下搜索指定内容
echo " ls *.$2 | while read file;do out=$( (grep -n $1 $file) ) [ -n \"$out\" ] & ...
- python的range()函数
range函数的三种用法:>>> range(1,5) # 代表从1到5(不包含5) [1, 2, 3, 4] >>> range(1,5,2) # 代表从1到5, ...
- Python爬取视频(其实是一篇福利)
窗外下着小雨,作为单身程序员的我逛着逛着发现一篇好东西,来自知乎 你都用 Python 来做什么?的第一个高亮答案. 到上面去看了看,地址都是明文的,得,赶紧开始吧. 下载流式文件,requests库 ...
- 【转载】CSRF攻击及其应对之道
在我最开始接触JavaEE时,我工作的第一个内容就是解决项目中存在的CSRF漏洞,当时的解决方法是在Referer添加token的方法.我对CSRF攻击的主要认知和解决的大部分思路都来自于这篇文章. ...
- #多个关联的python程序在linux后台运行
由于在shell脚本中直接使用&符号和python程序有冲突,不能正常的进入后台执行python(多番尝试python xxx.py &,后面的&总是不能正常识别.系统cent ...
- JDK1.8中HashMap实现
JDK1.8中的HashMap实现跟JDK1.7中的实现有很大差别.下面分析JDK1.8中的实现,主要看put和get方法. 构造方法的时候并没有初始化,而是在第一次put的时候初始化 putVal方 ...
- Google mobile test
1. 现已更新至3.0+版本: 2. 应对版本频繁的迭代更新,进行[版本监控.持续更新.反馈,开发的单元测试] 1. 多关注金字塔的底层: 2. [集成测试, Espresso, EarlGrey] ...
- commons - lang(1) StringUtils
分享几个关于StrngUtils的几个实用的方法(以下方法中省略了参数) 1.isBlank() 这个方法用来判空,包括null和空字符串,之前自己写的时候都是str != null &&am ...
- 【线段树】BZOJ2752: [HAOI2012]高速公路(road)
2752: [HAOI2012]高速公路(road) Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1621 Solved: 627[Submit] ...