hdu 5723 (最小生成树 + 树上的期望)
来源:互联网 发布:2选1数据选择器原理图 编辑:程序博客网 时间:2024/06/14 02:28
多校简单题
- 第一个问,最小生成树。
- 第二个问,求出最小生成树后建图。
枚举每一条边,然后期望是边的期望和,边的期望就是两边节点数的乘,乘以花费。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <cmath>using namespace std;#define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl;using namespace std;const int maxn = 1000005;int n,m;int fa[maxn];double ans2;vector<pair<int,int> >E[maxn];int fi(int u){ return u != fa[u] ? fa[u] = fi( fa[u] ) : u;}void uni(int x,int y){ int p=fa[x],q=fa[y]; if(p==q)return; fa[p]=q;}struct node{ int x,y,z;}p[maxn];bool cmp(node a,node b){ return a.z<b.z;}void init(){ ans2=0; for(int i=0;i<maxn;i++)fa[i]=i; memset(p,0,sizeof(p)); for(int i=0;i<maxn;i++)E[i].clear();}int dfs(int x,int f){ int sz = 0; for(int i=0; i<(int)E[x].size(); i++){ int v = E[x][i].first; if(v==f)continue; int tmp = dfs(v,x); sz+=tmp; ans2 = ans2 + 1.0 * tmp * (n-tmp) * E[x][i].second; } return sz+1;}int main(){ int t; scanf("%d",&t); while(t--){ init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); } sort(p+1,p+1+m,cmp); long long ans = 0; for(int i=1;i<=m;i++){ int p1=fi(p[i].x),q1=fi(p[i].y); if(p1==q1)continue; uni(p1,q1); ans+=p[i].z; E[p[i].x].push_back(make_pair(p[i].y,p[i].z)); E[p[i].y].push_back(make_pair(p[i].x,p[i].z)); } dfs(1,0); ans2 = ans2 * 1.0 * 2.0 * 1.0 / (1.0* n) / (n - 1.0); printf("%I64d %.2lf\n",ans,ans2); }}
0 0
- hdu 5723 (最小生成树 + 树上的期望)
- HDU 5723 (最小生成树/树上的期望)
- HDU 5723 Abandoned country 最小生成树,树上期望
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- 最小生成树+树上期望_____Abandoned country(hdu 5723 2016多校第一场)
- hdu 5723 最小生成树+期望
- HDU 5723 Abandoned country (最小生成树+期望)
- HDU 4035 树上的期望
- hdu 5723 最小生成树+数学期望 难度:一般
- HDU 2376 Average distance(树上两点间的期望)
- HDU 5723 Abandoned country mst+树上期望
- Hdu 5866 Lucky E(树上点期望的最大值)
- hdu5723 最小生成树+DFS(距离和,期望)
- HDU 5723(最小生成树+dfs)
- hdu5723 Abandoned country (最小生成树+期望)
- HDU5723 Abandoned country (最小生成树+期望)
- hdu 5723(最小生成树)
- Tree (树上期望dp)
- 聚类 - 1 - 聚类介绍
- Java内存区域与内存溢出
- Win7 64Bit + VS2012 + openBLAS0.2.18
- Oracle学习记录整理笔记1-数据备份
- 关于对数组和指针的测试与分析OC
- hdu 5723 (最小生成树 + 树上的期望)
- 使用JMeter3.0实战之分布式并发测试以及web API接口测试
- OPENCV 3.0.0 win10 vs2013 配置
- Winform--文件上传到服务器
- 制定你的创业计划
- ubuntu安装systemtap
- 聚类 - 2 - K-means算法,K中值聚类
- ANDROID应用性能调优的技术点
- VisualStudio2013如何完全卸载