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 ...
随机推荐
- collections --Counter
collections 模块--Counter 目的是用来跟踪值出现的次数.是一个无序的容器类型,以字典的键值对形式存储,其中元素为 key,其计数作为 value.计数值可以是任意的 Integer ...
- grep命令及基本正则表达式
grep命令是Linux系统中一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功 ...
- SQLServer:无法生成 SSPI 上下文(Cannot generate SSPI context)
服务器版本:windows Server 2012 R2 数据库版本: SQLServer 2016 +sp1 SQL2016AlwaysOn群集: 由于重启过域控,造成后续的部分服务器也解析不到DN ...
- SQLSERVER 死锁标志
最开始做DBA的时候,整天死锁到头痛1222,至今都能回想到这个错误窗口: 死锁定义:死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所站用不会释放的资源而处于的一种永久等待 ...
- node.js 之 Hello,World in Node !
创建一个js文件,把下面的内容粘贴进去,命名为helloworld.js. //加载 http 模块 var http = require("http"); //创建 http 服 ...
- linux下的磁盘挂载
将新的磁盘安装在服务器上后,怎么挂载到现在的服务器上呢? 1.查询是否已经分配磁盘 fdisk -l 这里因为测试,只是挂载了10G的硬盘 2.发现有磁盘/dev/sdb.然后使用fdisk命令建立分 ...
- 设计一个有getMin功能的栈(1)
题目: 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作. 要求: 1.pop.push.getMin操作的时间复杂度都是O(1) 2.设计的栈类型可以输用现成的栈结构 解答 ...
- easyHOOK socket send recv
代码比较简单,就不做注释了. 包含一个sockethookinject.DLL 和sockethook.exe 有一点不清楚, SetExclusiveACL可以添加当前线程的hook, 但是eas ...
- socket模型处理多个客户端
最近学完了简单的socket编程,发现其实socket的网络编程其实并没有什么难度,只是简单的函数调用,记住客户端与服务端的步骤,写起来基本没有什么问题. 在服务器程序的设计中,一个服务器不可能只相应 ...
- iOS 应用全部添加滑动返回
if ([self class] == [HomeViewController class]||[self class] == [ComprehensivefinanceViewControlle ...