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

官方题解:

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

注意 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. mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)

    背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...

  2. (转)同步异步,阻塞非阻塞 和nginx的IO模型

    同步异步,阻塞非阻塞 和nginx的IO模型  原文:https://www.cnblogs.com/wxl-dede/p/5134636.html 同步与异步 同步和异步关注的是消息通信机制 (sy ...

  3. Linux下远程连接工具SSHSecureShellClient的使用

    实际开发中,Linux 服务器都在其他的地方,我们要通过远程的方式去连接 Linux 并操作它,Linux 远程的操作工具有很多,企业中常用的有 Puttty.secureCRT.SSH Secure ...

  4. Java实现Ip代理池

    设置Ip代理很多时候都会有用到,尤其是在写爬虫相关项目的时候.虽然自己目前没有接触这种需求,但由于最近比较闲,就写着当作练习吧 爬取代理IP 爬取 关于爬取代理IP,国内首先想到的网站当然是 西刺代理 ...

  5. ife task0003学习笔记(一):JavaScript作用域

    在学习JavaScript作用域概念之前,首先要明白几个概念:执行环境.变量对象.作用域链. 一.JavaScript执行环境(execution context): 在<Professiona ...

  6. SpringBoot 之 打war包

    1.修改打包方式为 war <packaging>war</packaging> 2. 修改tomcat 依赖 <dependency> <groupId&g ...

  7. [shell基础]——echo命令

    echo命令:在shell中主要用于输出 1. -n     不换行的显示结果(默认是换行的) 2. -e " "  支持双引号中使用一些特殊字符 常用的特殊字符有 \a 发出警告 ...

  8. 经典算法详解(1)斐波那契数列的n项

    斐波那契数列是一个常识性的知识,它指的是这样的一个数列,它的第一项是1,第二项是1,后面每一项都是它前面两项的和,如:1,1,2,3,5,8,13,21,34,55,89,144,233…… 说明:由 ...

  9. [转](.NET Core C#) AES Encryption

    本文转自:https://www.example-code.com/dotnet-core/crypt2_aes.asp Chilkat.Crypt2 crypt = new Chilkat.Cryp ...

  10. c#-FrameWork02泛型

    泛型 l  泛型(generic)编程是一种编程范式,它利用”参数化类型”将类型抽象化,从而可以实现更为灵活的复用.把数据类型参数化 sh泛型集合 泛型集合与集合的对比 泛型集合类 非泛型集合类 Li ...