传送门

Average distance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 682    Accepted Submission(s): 244
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.

 
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

 
Sample Input
1
5
0 1 6
0 2 3
0 3 7
3 4 2
 
Sample Output
8.6
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2378 2379 2377 2380 2381 
 
 

哎,思路想复杂了,其实蛮简单的:

转一发题解:

引:如果暴力枚举两点再求距离是显然会超时的。转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的贡献就是(A*B*此边长度)。我们把所有边的贡献求总和,再除以总路径数N*(N-1)/2,即为最 后所求。

每条边两端的点数的计算,实际上是可以用一次dfs解决的。任取一点为根,在dfs的过程中,对每个点k记录其子树包含的点数(包括其自身),设点数为a[k],则k的父亲一侧的点数即为N-a[k]。这个统计可以和遍历同时进行。故时间复杂度为O(n)。

16447376 2016-03-06 09:40:59 Accepted 2376 312MS 4540K 2093 B C++ czy
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
#include <cmath>
#include <map>
#include <queue> #define ll long long
#define N 10005
#define eps 1e-8 using namespace std; int T;
double tot[N];
double cou[N];
double num;
int n; struct PP
{
int to;
double val;
}; vector<PP>Edge[N]; PP te; void add_adge(int from,int to,double val)
{
te.to = to;te.val = val;
Edge[from].push_back(te);
te.to = from;te.val = val;
Edge[to].push_back(te);
} void dfs(int now,int fa)
{
unsigned int i;
PP nt;
cou[now] = ;
for(i = ;i < Edge[now].size();i++){
nt.to = Edge[now][i].to;
nt.val = Edge[now][i].val;
if(nt.to == fa){
continue;
}
dfs(nt.to,now);
tot[now] = tot[now] + tot[nt.to] + (n-cou[nt.to]) * cou[nt.to] * nt.val;
cou[now] = cou[now] + cou[nt.to];
//printf(" now = %d i=%d to=%d tot=%.6f cou=%.6lf\n",now,i,nt.to,tot[now],cou[now]);
}
//printf(" i=%d tot=%.6f cou=%.6f\n",now,tot[now],cou[now]);
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&T);
int i;
int from,to;
double val;
for(int ccnt=;ccnt<=T;ccnt++){
//while(scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])!=EOF){
scanf("%d",&n);
memset(tot,,sizeof(tot));
memset(cou,,sizeof(cou));
num = 1.0 *n*(n-)/;
for(i=;i<=n;i++){
Edge[i].clear();
}
for(i=;i<=n-;i++){
scanf("%d%d%lf",&from,&to,&val);
add_adge(from,to,val);
}
dfs(,-);
//for(i=0;i<n;i++){
// for(int j=0;j<Edge[i].size();j++){
// printf(" i=%d to=%d val=%.6lf\n",i,Edge[i][j].to,Edge[i][j].val);
// }
// }
for(i=;i<n;i++){
//printf(" i=%d tot=%.6f cou=%.6f\n",i,tot[i],cou[i]);
}
printf("%lf\n",tot[]/num);
}
return ;
}

hdu 2736 Average distance的更多相关文章

  1. 【hdu 2376】Average distance

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2376 [题意] 让你计算树上任意两点之间的距离的和. [题解] 算出每条边的两端有多少个节点设为n ...

  2. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  3. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  4. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...

  5. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. HDU 472 Hamming Distance (随机数)

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...

  7. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

  8. HDU 4217 Hamming Distance 随机化水过去

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. HDU ACM 2895-Edit distance

    Edit distance Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

随机推荐

  1. 关于<meta NAME="keywords" CONTENT="">

    昨天终于以实习身份入职一家小创业公司,今天让我多看看别人的网页怎么写的,发现了一个以前都没关注过的东西. <meta name="keywords" content=&quo ...

  2. 盘点那些年,被Oracle收购的公司

    微博上看到一图,很清晰.盘点那些年,被Oracle收购的公司,Oracle日益强大,都收购了哪些公司呢?别再以为只有Sun啦...看看你都知道哪些? ps:Strategic Acquisitions ...

  3. 【HEVC帧间预测论文】P1.3 Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information

    基于图形信息的HEVC帧间预测快速算法/Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information <H ...

  4. JavaScript 的垃圾回收与内存泄露

    JavaScript采用垃圾自动回收机制,运行时环境会自动清理不再使用的内存,因此javascript无需像C++等语言一样手动释放无用内存. 在这之前先说一下垃圾回收的两种方式:引用计数与标记清除. ...

  5. Python3 OpenCV3 图像处理基础

    开发环境搭建 本人使用的是Ubuntu 16.04LTS. 1.安装Python3 ## 其实 Ubuntu 16.04 系统自带了 Python 3.5.2,因此不需要再安装了?但是需要安装一些开发 ...

  6. windows安装tensorflow的一个教训

    今天没什么课,然后就准备安装tensorflow. 看了一下教程,就去做了. 然后就犯了错误.网上的教程还是有一些差异的,而我又比较大意,没有很注意到CUDA,cudnn的版本要求,也过于高估自己cp ...

  7. js 时间戳 随机数 new Date().getTime()

    一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new ...

  8. CPP-STL:随机数发生器random_shuffle

    //--------------------------------------------------------------------------- #include <string.h& ...

  9. Hibernate-01 入门

    学习任务 Hibernate开发环境的搭建 使用Hibernate对单表进行增删改操作 使用Hibernate按照数据表主键查询 关于Hibernate 简介 Hibernate的创始人Gavin K ...

  10. mysql的sql语句练习的2个网址

    sql语句练习: https://blog.csdn.net/mrbcy/article/details/68965271 完成. https://blog.csdn.net/flycat296/ar ...