HDU 2376 Average distance(树上两点间的期望)
来源:互联网 发布:传智播客python怎么样 编辑:程序博客网 时间:2024/06/05 08:10
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=20005;struct node{ int x,y; int v,next;}edge[maxn];int head[maxn],cnt,n;int sz[maxn];double an;int add(int x,int y,int v){ edge[cnt].x=x; edge[cnt].y=y; edge[cnt].v=v; edge[cnt].next=head[x]; head[x]=cnt++;}int dfs(int root,int fa){ sz[root]=1; for(int i=head[root];i!=-1;i=edge[i].next) { int a=edge[i].y; int b=edge[i].v; if(a==fa) continue; dfs(a,root); sz[root]=sz[root]+sz[a]; an=an+(double)(n-sz[a])*sz[a]*b; }}int main(){ int t; scanf("%d",&t); while(t--) { memset(head,-1,sizeof(head)); memset(sz,0,sizeof(sz)); cnt=0; scanf("%d",&n); for(int i=0;i<n-1;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } an=0.0; dfs(0,-1); printf("%.7f\n",an/ (1.0 * n * (n - 1) / 2.0)); }}
Average distance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 943 Accepted Submission(s): 318
Special Judge
Problem Description
Given a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d01 + d02 + d03 + d04 + d12 +d13 +d14 +d23 +d24 +d34)/10 = (6+3+7+9+9+13+15+10+12+2)/10 = 8.6.
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with an integer n (2 <= n <= 10 000): the number of nodes in the tree. The nodes are numbered from 0 to n - 1.
n - 1 lines, each with three integers a (0 <= a < n), b (0 <= b < n) and d (1 <= d <= 1 000). There is an edge between the nodes with numbers a and b of length d. The resulting graph will be a tree.
One line with an integer n (2 <= n <= 10 000): the number of nodes in the tree. The nodes are numbered from 0 to n - 1.
n - 1 lines, each with three integers a (0 <= a < n), b (0 <= b < n) and d (1 <= d <= 1 000). There is an edge between the nodes with numbers a and b of length d. The resulting graph will be a tree.
Output
For each testcase:
One line with the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
One line with the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
Sample Input
150 1 60 2 30 3 73 4 2
Sample Output
8.6
Source
bapc2007
Recommend
lcy | We have carefully selected several similar problems for you: 2378 2379 2377 2380 2381
0 0
- HDU 2376 Average distance(树上两点间的期望)
- hdu 2376 Average distance DFS 求树上任意两点距离和
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- HDU 4035 树上的期望
- hdu5723(16多校第1场,树上两点平均距离的期望)
- POJ 1986 Distance Queries LCA树上两点的距离
- Poj 1986 Distance Queries (LCA 树上两点间最短距离)
- hdu 5723 (最小生成树 + 树上的期望)
- HDU 5723 (最小生成树/树上的期望)
- zoj 2912 hdu 2376 Average distance 树形
- HDU 2376 Average distance (树形dp)
- HDU 2376 Average distance 树形dp
- hdu 2376(求树上任意两点之间距离之和的平均值)
- HDU2376Average distance(树形dp|树上任意两点距离和的平均值)
- Hdu 5866 Lucky E(树上点期望的最大值)
- HDU 5723 Abandoned country (并查集 + DFS+求解树上任意两点间的距离的平均值)
- Tree (树上期望dp)
- HDU 5723 Abandoned country mst+树上期望
- Cordova 开发属于自己的插件
- Greenplum中Heap表膨胀查看和解决方案
- 一些收藏的网址
- Cent os mysql主从同步问题
- 状态模式
- HDU 2376 Average distance(树上两点间的期望)
- Android 编程下的 TraceView 简介及其案例实战
- jQuery EasyUI —Combo(自定义下框)
- Java注解的实际应用案例讲解
- iOS 7适配上存在的各种问题
- 5.5以上版本查询锁信息
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
- 学Unity3D游戏开发到官方合作机构
- 项目笔记