HDU 5723 最小生成树上的期望
题意:求最小生成树,和任意两个点之间距离的期望
官方题解:

最后求两遍点的积的时候,还是要判断父子关系。
注意 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 最小生成树上的期望的更多相关文章
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- HDU 5723:Abandoned country(最小生成树+算期望)
http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Problem Description An abandoned ...
- hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- win 引用win32无效问题
我使用pycharm 安装: python window 10 下安装win32 module 问题:安装后引用win32api等依赖,无效,但是目录下确实已经安装: 解决方案: ${python_h ...
- 读取日志文件,搜索关键字,打印关键字前5行。yield、deque实例
from collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen=h ...
- Linux Nginx环境安装配置redmine3.1
作者博文地址:https://www.cnblogs.com/liu-shuai/ 环境: CentOS-6.5+Nginx-1.8.0+Redmine-3.1.1+Ruby-2.0 1.配置环境 1 ...
- (转) sync命令
sync sync命令 sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的 ...
- TOJ 3248 Flip Game
Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...
- Flask 编写http接口api及接口自动化测试
片言 此文中代码都是笔者工作中源码,所以不会很完整,主要摘常见场景的api片段用以举例说明 另:此文主要针对自动化测试人员,尤其有python基础阅读更佳. 笔者使用 python3.6 + post ...
- IE678910不兼容H5的placeholder属性,需要JS解决
两种方法的思路 一.使用input的value作为显示文本 二.不使用value,添加一个额外的span标签,绝对定位覆盖到input上面. 两种方式各有优缺点,方法一占用了input的value属性 ...
- react native 完美解决启动白屏
先讲下我的RN版本0.58.5 首先安装react-native-splash-screen(目前使用的版本是3.2.0) 项目地址https://github.com/crazycodeboy/re ...
- 远程SQL Server连接不上
运行 cmd -> 输入 netsh winsock reset重启后 应该可以连接sql了
- 面试基础(二)-mem函数
常考的函数有下面三个,memset,memcpy,memmove,一定要记住三个函数的函数原型,熟记返回值类型和参数类型,当然也不能忘记参数检查 memset #include<iostre ...