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/ ...
随机推荐
- unity 退到桌面的 OnApplicationPause
void OnApplicationFocus( bool isFocus ) { // Debug.Log("--------OnApplicationPause---" + i ...
- 配置mac自带的Apache服务器
第一步: 484 cd /etc/apache2 备份httpd.conf文件,以防万一 486 sudo cp httpd.conf httpd.conf.bak 如果操作错误,可以通过 491 ...
- Raft详解-启动后运行期间代码
Raft启动后运行期间主要执行两个函数:1.状态监测和转化 func (rf *Raft) Loop() { // Set out as a follower TimeOutConst := 0 fo ...
- windows7所有版本
windows7所有版本迅雷地址下载集合(含32位和64位) Windows7 SP1旗舰版 32位官方原版下载: ed2k://|file|/cn_windows_7_ultimate_with_s ...
- HDU 5795 A Simple Nim
打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- .net core 时间戳转换
/// <summary> /// 时间戳和日期的转化 /// </summary> public class DateTimeUtility { /// <summar ...
- gerrit review 设置
$ git config remote.review.pushurl "ssh://someone@ip:29418/the_project" $ git config remot ...
- HDU2629:Identity Card
Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...
- Mac Git 学习笔记
lapommedeMacBook-Pro:~ lapomme$ cd GitHub lapommedeMacBook-Pro:GitHub lapomme$ cd lapommedeMacBook-P ...
- jetty访问jsp页面出现( PWC6345: There is an error in invoking javac)
org.apache.jasper.JasperException: PWC6345: There is an error in invoking javac. A full JDK (not ju ...