题意:求最小生成树,和任意两个点之间距离的期望

官方题解:

最后求两遍点的积的时候,还是要判断父子关系。

注意 long long

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int maxn = +;
int father[maxn]; int Find_Set(int x) {
if(x!=father[x])
father[x] = Find_Set(father[x]);
return father[x];
} struct Edge {
int u,v,d;
bool operator < (const Edge & rhs) const {
return d < rhs.d;
}
}edges[+]; struct o {
int v,d;
}; vector <o> G[maxn];
int sum[maxn];
bool vis[maxn];
int ff[maxn]; int x;
void dfs(int r,int f) {
if(vis[r]) return ;
sum[r] = ;
vis[r] = true;
for(int i=;i<G[r].size();i++)
{
int v = G[r][i].v;
if(v!=f) {
ff[v] = r;
dfs(v,r);
sum[r] +=sum[v];
}
}
} vector<Edge> minans; int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--) {
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
father[i] = i; for(int i=;i<=n;i++) {
G[i].clear();
} minans.clear();
memset(sum,,sizeof(sum));
memset(vis,,sizeof(vis));
x = ; for(int i=;i<m;i++)
scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].d); sort(edges,edges+m); LL ans = ;
for(int i=;i<m;i++) {
int u = edges[i].u;
int v = edges[i].v;
int d = edges[i].d;
int fx = Find_Set(u);
int fy = Find_Set(v);
if(fx!=fy)
{
G[u].push_back((o){v,edges[i].d});
G[v].push_back((o){u,edges[i].d});
minans.push_back((Edge){u,v,d});
father[fy] = fx;
ans +=edges[i].d;
}
} dfs(,-); LL x = ;
for(int i=;i<minans.size();i++) {
int u,v,d;
u = minans[i].u;
v = minans[i].v;
d = minans[i].d; if(ff[u]==v) {
x +=(LL)sum[u]*(LL)((LL)n-sum[u])*d;
}
else if(ff[v]==u) {
x +=(LL)sum[v]*(LL)((LL)n-sum[v])*d;
} } LL mm = (LL)n*(LL)(n-)/;
printf("%I64d %.2lf\n",ans,x*1.0/mm); } return ;
}

HDU 5723 最小生成树上的期望的更多相关文章

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

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

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

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

  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(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  5. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 1233(最小生成树)

    HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...

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

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

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

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

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

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

随机推荐

  1. log4net sqlite

    <?xml version="1.0" encoding="utf-8" ?><log4net> <appender name=& ...

  2. switch case 注意事项

    switch case常见的注意事项: 1.case后面常量值的顺序可以任意,一般按顺序编写 2.default顺序也可以编写在switch中的任意位置 当所有case都不满足时则执行default ...

  3. Permanent data region free space insufficient to allocate 64792 bytes of memory

    TT0802: Database permanent space exhaustedTT6220: Permanent data region free space insufficient to a ...

  4. 8086中断系统——《x86汇编语言:从实模式到保护模式》读书笔记04

    80X86中断系统 能够处理256个中断 用中断向量号0-255区别 可屏蔽中断还需要借助专用中断控制器Intel 8259A实现优先权管理 1.中断的分类 中断可以分为内部中断和外部中断. (1)内 ...

  5. HDU 1257——最少拦截系统——————【LIS变型题】

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. java基础--常用函数总结

    java基础--常用函数总结 2019-3-16-23:28:01-----云林原创 1.split()字符串分割函数 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.Math.flo ...

  7. mysql的引擎和锁

  8. 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)

    多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...

  9. 获取css样式,style、getComputedStyle及currentStyle的区别

    样式表有三种: 内嵌样式:<div id="box" style="color:red">box</div>,style写在html中的 ...

  10. 中值滤波C语言优化

    中值滤波C语言优化 图像平滑是图像预处理的基本操作,本文首先用不同的方法对一张图片做预处理比较它们效果的不同,然后针对中值滤波,实现了一种快速实现.(其实是copy的opencv实现,呵呵).因为op ...