题目链接: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 并查集的更多相关文章

  1. 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/ ...

  2. 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 ...

  3. [CF#250 Div.2 D]The Child and Zoo(并查集)

    题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...

  4. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  5. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  6. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  8. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

随机推荐

  1. 3D轮播切换特效 源码

    这个3D轮播切换特效是我2017年2月份写的 当初我 刚接触HTML不久,现在把源码分享给大家 源码的注释超级清楚 . <!-- 声明文档类型:html 作用:符合w3c统一标准规范 每个浏览器 ...

  2. LeetCode题目总结(三)

    我的代码在github上,https://github.com/WINTERFELLS/LeetCode-Answers 这里只提供个人的解题思路,不一定是最好的. 41-60: 给定一个排好序的数组 ...

  3. 安卓电量优化之AlarmManager使用全部解析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.AlarmManager概述 AlarmManager是安卓系统中一种系统级别的提示服务,可以在我们设定时间或者周期性的执行一个intent,这 ...

  4. shell中的循环语句while

    循环语句的结构: ------------| while 条件        | do | 需要执行的命令   | done  | -----------| 例如: 1.while一直循环 2.whi ...

  5. Nexus私服忘记用户名密码解决4步走

    1  停止nexus服务 [root@node001 bin]# /usr/local/nexus/nexus-2.14.5-02/bin/nexus stop******************** ...

  6. Twisted使用和scrapy源码剖析

    1.Twisted是用Python实现的基于事件驱动的网络引擎框架. 事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的 ...

  7. [转]Python 资源大全中文版

    摘自:https://github.com/jobbole/awesome-python-cn 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesom ...

  8. 单张滑动tab 组件

    /* CSS重置 * */ body, ul, ol { margin: 0px; padding: 0px; } .flash { width: 300px; height: 420px; posi ...

  9. 【Java数据结构学习笔记之一】线性表的存储结构及其代码实现

    应用程序后在那个的数据大致有四种基本的逻辑结构: 集合:数据元素之间只有"同属于一个集合"的关系 线性结构:数据元素之间存在一个对一个的关系 树形结构:数据元素之间存在一个对多个关 ...

  10. 51 nod 1628 非波那契树

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1628 花了一个早上+半个下午终于把这题切掉了…… (膜出题人) ...