HDU-5723 Abandoned country
For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
题目大意:
有n个点,有m条边,让你求它的最小生成树,并且在这个生成树上,求任意两点间的距离的期望。
解题思路:
第一问比较裸,就直接求最小生成树就好,第二问需要一些技巧。
现在有一条边ab,其中b的子树个数为num,那么这条边被经过的次数为(n - num) * num
然后乘一下这条边的权值就可以了
代码:
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 1e5 + 5;typedef long long LL;
typedef struct node{int from, to;int w;bool operator < (node zz){return w < zz.w;}
}Edge;
typedef struct nn{int to;int w;nn(){}nn(LL a, LL b){to = a; w = b;}
}EEE;LL ans;
double res;
int t, n, m;
Edge g[maxn * 10];
vector<EEE> e[maxn];
int vis[maxn], pre[maxn];int findfather(int x){return pre[x] = (pre[x] == x ? x : findfather(pre[x]));
}
void join(int x, int y){pre[findfather(x)] = findfather(y);
}
void kruskal(){int x, y, flag = 0;for(int i = 0; i < m; ++i){x = findfather(g[i].from);y = findfather(g[i].to);if(x == y) continue;join(x, y);++flag;ans += g[i].w;e[g[i].from].push_back(nn(g[i].to, g[i].w));e[g[i].to].push_back(nn(g[i].from, g[i].w));if(flag == n - 1) return;}
}
int dfs(int p){if(vis[p]) return 0;vis[p] = 1;int tt = 1, num;for(int i = 0; i < e[p].size(); ++i){int v = e[p][i].to;num = dfs(v);tt += num;res += (LL)(n - num) * (LL)num * (LL)e[p][i].w;}return tt;
}
int main(){scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);for(int i = 1; i <= n; ++i) {pre[i] = i;vis[i] = 0;e[i].clear();}for(int i = 0; i < m; ++i){scanf("%d%d%d", &g[i].from, &g[i].to, &g[i].w);}sort(g, g + m);ans = 0;kruskal();res = 0;dfs(1);printf("%lld %.2lf\n", ans, res / ((n - 1LL) * n / 2.0));}return 0;
}
转载于:https://www.cnblogs.com/wiklvrain/p/8179452.html
HDU-5723 Abandoned country相关推荐
- hdu-5723 Abandoned country(最小生成树+期望)
题目链接: Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- Abandoned country
A Abandoned country 最小生成树 + 树上任意点对距离之和 树上任意点对距离之和: 计算时考虑每一个边,它的贡献值为它的 左端点以左的点的个数 * 右端点以右的点的个数 * 该边的权 ...
- 2019.9.17最小生成树知识点回顾
POJ 1797 Heavy Transportation(最大生成树-Prim) 最大生成树,方法模仿最小生成树,每次选最大边进行操作,即可. HDU 5723 Abandoned country( ...
- Kuangbin专题三Dancing Links
Kuangbin专题三Dancing Links 没写完所有的,因为要去上课了赶紧先预习一下,这就先发出来吧. 跳舞链这东西以前在hihocoder上翻到过,当时看的模模糊糊的,现在好好学一学. 暂时 ...
- 暑期集训3:几何基础 练习题G: HDU - 1052
2018学校暑期集训第三天--几何基础 练习题G -- HDU - 1052 (昨天加练题) Tian Ji -- The Horse Racing Here is a famous sto ...
- hdu 4857 Little Devil I
http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,边的颜色要么为白色,要么为黑色,初始每条边为白色,有三种操作 1.将u-v链上面的所有 ...
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
最新文章
- opencv轮廓周长原理_OpenCV3 - 轮廓特征
- 6显示wan口未插线_WiFi 6到底6不6——华硕 AX56U热血版 路由器评测
- 语言筛选法选素数怎么解决输出超限_论文深耕 | 什么时候才能把文献看完?——论文的正确筛选、阅读顺序...
- 实现仿简书选取内容生成分享图片效果
- 邮件服务器实用技巧和选购指南
- 软件开发人员,自身素质应该注意的问题!
- CLR寄宿(上) MSCOREE.DLL
- 嵌入式C语言(指针)
- Dos攻击与DDos攻击
- 微信小程序入门12-微信小程序开发设置中服务器域名和业务域名
- 如何看待”日光之下,并无新事“
- Windows自带的【远程桌面连接】操作方法及常见问题
- 部分双机热备软件详细介绍-行云管家
- 计算机组装的硬件配置及报价,2021组装电脑配置清单及价格表说明
- Kunbernetes——二进制单节点部署
- CODING 携手 Thoughtworks 助力老百姓大药房打造”自治、自决、自动”的敏捷文化
- 智慧小区智慧物业管理系统一体化解决方案
- 【ModuleNotFoundError 与 ImportError】之 most likely due to a circular import
- 年薪170W阿里P8相亲要求女方月薪1万,网友:有点高
- android 指定区域内绘制,Android实现扫一扫功能之绘制指定区域透明区域