hdu_5723_Abandoned country(最小生成树)
题目链接:hdu_5723_Abandoned country
题意:
让你求最小生成树的花费,然后求任给两点的期望路程
题解:
最小生成树大家都会求,Kruskal这里要改改,因为后面要求任意两点的期望路程,我们这里Kruskal 记录下最小生成树的边,然后通过DFS把树建出来,最后找每条边经过了多少次,然后乘上对应的组合,比如样列,第一条边权为1的边,这条边左边只有一个节点,右边有3个节点,那么这条边就会经过3次,所以这条边的贡献就是1(左边点个数)*3(右边点个数)*1(边权),依次类推,然后最后总和除以(n*(n-1)/2),因为任意点对有这么多种组合的方式,这里的数据居然没有设计成最后除会爆double的,所以不用边除边加,当然保险的是边除边加
#include<cstdio>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int N=1e5+,M=1e6+;
int n,m,g[N],nxt[N*],v[N*],ed,f[N],a[N],sz[N]; inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} struct edge{
int u,v,w;
bool operator<(const edge &b)const{return w<b.w;}
}e[M]; inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);} void Kruskal(int tot=){
sort(e+,e++m);
F(i,,n)f[i]=i;
F(i,,m){
int fu=find(e[i].u),fv=find(e[i].v);
if(fu!=fv)a[++tot]=i,f[fu]=fv;
if(tot==n-)break;
}
} void dfs(int x,int pre){
sz[x]=;
for(int i=g[x];i;i=nxt[i])
if(v[i]!=pre)dfs(v[i],x),sz[x]+=sz[v[i]];
} int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
F(i,,m)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
Kruskal();
F(i,,n)g[i]=;ed=;
F(i,,n-){
adg(e[a[i]].u,e[a[i]].v);
adg(e[a[i]].v,e[a[i]].u);
}
dfs(,);
double ans=,div=(1.0*n*(n-)/);
long long an=;
F(i,,n-){
int u=e[a[i]].u,v=e[a[i]].v;
if(sz[u]<sz[v])u^=v,v^=u,u^=v;
an+=e[a[i]].w;
ans+=(double)(sz[v])*(n-sz[v])*e[a[i]].w;
}
printf("%lld %.2lf\n",an,ans/div);
}
return ;
}
hdu_5723_Abandoned country(最小生成树)的更多相关文章
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- Abandoned country(最小生成树+树形DP)
#include<bits/stdc++.h> using namespace std; struct node{ int u, v, w, nex; bool gone; node(){ ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU5723 Abandoned country (最小生成树+深搜回溯法)
Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
随机推荐
- angular中重要指令介绍($eval,$parse和$compile)
在angular的服务中,有一些服务你不得不去了解,因为他可以说是ng的核心,而今天,我要介绍的就是ng的两个核心服务,$parse和$compile.其实这两个服务讲的人已经很多了,但是100个读者 ...
- android ndk调用OpenGL 实现纹理贴图Texture
android ndk调用OpenGL 实现纹理贴图Texture 时间 2014-06-25 05:24:39 CSDN博客 原文 http://blog.csdn.net/chrisfxs/a ...
- java URL编程
一.URL编程技术 URL是统一资源定位器的缩写,它是指向互联网“资源”的指针.URL表示了Internet上某个资源的地址.URL支持http,file,ftp等 多种协议.通过URL标识,可以直接 ...
- IP子网掩码划分及设置
IP子网掩码划分及设置 定长子网掩码: 一.子网掩码的计算 TCP/IP网间网技术产生于大型主流机环境中,它能发展到今天的规模是当初的设计者们始料未及的.网间网规模的迅速扩展对IP地址模式的威胁并不是 ...
- Chapter 2 Open Book——9
When I was finished with that, I took my book bag upstairs. Before starting my homework, I changed i ...
- Git合并分支出现的冲突解决
人生不如意之事十有八九,合并分支往往也不是一帆风顺的. 我们准备新的分支newbranch. LV@LV-PC MINGW32 /c/gitskill (master)$ git checkout - ...
- gridview列绑定HyperLink
<asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Navig ...
- button的action属性如果有参数,必须加“:”
比如: [bt addTarget:self action:@selector(shareButtonClickHandler:) …… 后面未写完 一开始我没加,就报错.
- springboot 打包
springboot 打包 先clean 然后 maven package 通过命令java -jar target/GoshenWepPro-0.1.0.jar运行程序
- fdisk添加磁盘
1. 通过Fdisk查看系统分区详细信息: Fdisk –l 详解: [root@jetsenLin ~]# fdisk -l Disk /dev/sda: 10.7 GB, ...