题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723

题意:求最小生成树,并且求这棵最小生成树上所有边走过次数的期望。

走过次数的期望=Σ边被走过次数*边权/(n*(n-1)/2)

求边被走过的次数,相当于关心这个边的两侧分别有多少点,走的次数就是两边的点数的乘积这个很好理解。可以从边的一侧开始dfs,找到这个边的一侧点数x,由于最小生成树是联通的,那么边的另一侧点数一定是n-x。所以这条边的贡献是(n-x)*x*w。

 #include <bits/stdc++.h>
using namespace std; typedef long long LL;
typedef struct Edge {
int v, next;
LL w;
}Edge; typedef struct E {
int u, v;
LL w;
}E;
const int maxn = ;
const int maxm = ;
int n, m;
E e[maxm<<];
Edge edge[maxm<<];
int head[maxn], ecnt;
int pre[maxn];
LL ret1, ret2; int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} bool unite(int x, int y) {
x = find(x); y = find(y);
if(x != y) {
pre[x] = y;
return ;
}
return ;
} void init() {
memset(head, -, sizeof(head));
ecnt = ; ret1 = ; ret2 = ;
for(int i = ; i <= n; i++) pre[i] = i;
} void adde(int u, int v, LL w) {
edge[ecnt].v = v; edge[ecnt].w = w; edge[ecnt].next = head[u]; head[u] = ecnt++;
edge[ecnt].v = u; edge[ecnt].w = w; edge[ecnt].next = head[v]; head[v] = ecnt++;
} bool cmp(E a, E b) {
return a.w < b.w;
} int dfs(int u, int p) {
int siz = ;
for(int i = head[u]; ~i; i=edge[i].next) {
int v = edge[i].v; LL w = edge[i].w;
if(p == v) continue;
int pre = dfs(v, u);
siz += pre;
ret2 += (LL)pre * (LL)(n - pre) * w;
}
return siz;
} int main() {
//freopen("in", "r", stdin);
int T, u, v;
LL w;
scanf("%d", &T);
while(T--) {
scanf("%d%d",&n,&m);
init();
for(int i = ; i < m; i++) {
scanf("%d%d%lld",&u,&v,&w);
e[i].u = u; e[i].v = v; e[i].w = w;
}
sort(e, e+m, cmp);
for(int i = ; i < m; i++) {
u = e[i].u; v = e[i].v; w = e[i].w;
if(unite(u, v)) {
ret1 += w;
adde(u, v, w);
}
}
dfs(, -);
LL k = n * (n - );
printf("%lld %.2lf\n", ret1, 2.0*ret2/(double)k);
}
return ;
}

[HDOJ5723]Abandoned country(最小生成树,期望)的更多相关文章

  1. hdu 5723 Abandoned country 最小生成树 期望

    Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  2. HDU 5723 Abandoned country 最小生成树+搜索

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. hdu 5723 Abandoned country 最小生成树+子节点统计

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. HDU 5723 Abandoned country (最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

  5. Abandoned country(最小生成树+树形DP)

    #include<bits/stdc++.h> using namespace std; struct node{ int u, v, w, nex; bool gone; node(){ ...

  6. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

  7. HDU 5723:Abandoned country(最小生成树+算期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Problem Description   An abandoned ...

  8. hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

随机推荐

  1. GitHub Desktop for Win 安装不上

    采用了ClickOnce部署方式,网速不给力,安装过程经常断线,要是有离线安装包就好了.

  2. mysql笔记03 查询性能优化

    查询性能优化 1. 为什么查询速度会慢? 1). 如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上要优化其子任务,要么消除其中一些子任务,要么减 ...

  3. scala模拟一个timer

    直接上代码: package com.test.scalaw.test.demo import java.util.Date /** * 模拟一个定时timer */ object Timer { d ...

  4. drupal 自定义表单调用autocomplete主标签实现方法

    代码如下: <php function module_name_form() { $form = array(); $form['city'] = array( '#title' => t ...

  5. 【css】多行文字图片混排容器内垂直居中解决方案

    css:   .box-wrap{display:table;width:200px;height:200px;*position:relative;}/*最外边的容器,需固定宽高*/ .box-ha ...

  6. thinkphp模板中截取中文字符串的方法分享

    前段用thinkphp写了一个系统,感觉thinkphp学起来比较容易,开发起来了比较顺手,其中一个关键的因素就是它的模版引擎相当强大,使用方法跟smarty类似,在模版中还可以用php代码,有模版包 ...

  7. 《深度探索C++对象模型》3

    第六章:执行期语意学 全局对象的初始化: 局部静态对象的初始化: 构建对象数组的vec_new()函数: 针对数组的new操作: placement operator new: 反聚合提高效率: 模板 ...

  8. 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏

    D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...

  9. zoj 3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  10. SQL与C#结合完整修改 删除信息

    --SQl中--建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) primary key,- ...