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

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

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

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. typedef struct Edge {
  6. int v, next;
  7. LL w;
  8. }Edge;
  9.  
  10. typedef struct E {
  11. int u, v;
  12. LL w;
  13. }E;
  14. const int maxn = ;
  15. const int maxm = ;
  16. int n, m;
  17. E e[maxm<<];
  18. Edge edge[maxm<<];
  19. int head[maxn], ecnt;
  20. int pre[maxn];
  21. LL ret1, ret2;
  22.  
  23. int find(int x) {
  24. return x == pre[x] ? x : pre[x] = find(pre[x]);
  25. }
  26.  
  27. bool unite(int x, int y) {
  28. x = find(x); y = find(y);
  29. if(x != y) {
  30. pre[x] = y;
  31. return ;
  32. }
  33. return ;
  34. }
  35.  
  36. void init() {
  37. memset(head, -, sizeof(head));
  38. ecnt = ; ret1 = ; ret2 = ;
  39. for(int i = ; i <= n; i++) pre[i] = i;
  40. }
  41.  
  42. void adde(int u, int v, LL w) {
  43. edge[ecnt].v = v; edge[ecnt].w = w; edge[ecnt].next = head[u]; head[u] = ecnt++;
  44. edge[ecnt].v = u; edge[ecnt].w = w; edge[ecnt].next = head[v]; head[v] = ecnt++;
  45. }
  46.  
  47. bool cmp(E a, E b) {
  48. return a.w < b.w;
  49. }
  50.  
  51. int dfs(int u, int p) {
  52. int siz = ;
  53. for(int i = head[u]; ~i; i=edge[i].next) {
  54. int v = edge[i].v; LL w = edge[i].w;
  55. if(p == v) continue;
  56. int pre = dfs(v, u);
  57. siz += pre;
  58. ret2 += (LL)pre * (LL)(n - pre) * w;
  59. }
  60. return siz;
  61. }
  62.  
  63. int main() {
  64. //freopen("in", "r", stdin);
  65. int T, u, v;
  66. LL w;
  67. scanf("%d", &T);
  68. while(T--) {
  69. scanf("%d%d",&n,&m);
  70. init();
  71. for(int i = ; i < m; i++) {
  72. scanf("%d%d%lld",&u,&v,&w);
  73. e[i].u = u; e[i].v = v; e[i].w = w;
  74. }
  75. sort(e, e+m, cmp);
  76. for(int i = ; i < m; i++) {
  77. u = e[i].u; v = e[i].v; w = e[i].w;
  78. if(unite(u, v)) {
  79. ret1 += w;
  80. adde(u, v, w);
  81. }
  82. }
  83. dfs(, -);
  84. LL k = n * (n - );
  85. printf("%lld %.2lf\n", ret1, 2.0*ret2/(double)k);
  86. }
  87. return ;
  88. }

[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. Sql Server 2012 的新分页方法分析(offset and fetch) - 转载

    最近在分析 Sql Server 2012 中 offset and fetch 的新特性,发现 offset and fetch 无论语法的简洁还是功能的强大,都是相当相当不错的 其中 offset ...

  2. delphi注册/反注册OCX

    uses ShellAPI; function ExecAndWait(const ExecuteFile, ParamString : string): boolean; var SEInfo: T ...

  3. 【MFC三天一个游戏】之 局域网黑白棋

    欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611 花了三天上班时间,妈的上班写就是不能静下心来,擦,要防BOSS巡山.... 以前也写过 ...

  4. android 项目学习随笔十一(ListView下拉刷新提示)

    1. 设置mHeaderView.setPadding TOPPADING为负值,隐藏刷新提示头布局 在onTouchEvent事件中进行头布局显示隐藏切换 import java.text.Simp ...

  5. docker-gerrit

    Docker Hub https://yeasy.gitbooks.io/docker_practice/content/repository/dockerhub.html 但如何直接下载成压缩文件呢 ...

  6. PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能

    PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时 ...

  7. Java多线程中的进程,线程,并行,并发

    2:什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出现进程. 进程:就是正在运行的程序. 进程是系统进行资源分配和调用的独立单位.每一个进程都有它自己的内 ...

  8. Install MongoDB on Red Hat Enterprise, CentOS, Fedora, or Amazon Linux

    Install MongoDB on Red Hat Enterprise, CentOS, Fedora, or Amazon Linux¶ Overview Use this tutorial t ...

  9. ectouch第二讲之 文件结构

    相信大家在ectouch官网都注意到了,ectouch采用的MVC框架,之前一直以为它用的和ecshop一样都是smarty,本鸟默默按照smarty的文件结构研究了好几天,结果是各种文件对不上号.无 ...

  10. 结对2.0--复利计算WEB升级版

    结对2.0--复利计算WEB升级版 复利计算再升级------------------------------------------------------------ 客户在大家的引导下,有了更多 ...