传送门

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. 【学习笔记】深入理解js原型和闭包(17)——补this

    本文对<深入理解js原型和闭包(10)——this>一篇进行补充,原文链接:https://www.cnblogs.com/lauzhishuai/p/10078307.html 原文中, ...

  2. 数据库SQL server 删除一张表中的重复记录

    --建立一张表 create table cat( catId int, catName varchar(40) ) --将下边的插入语句,多执行几次. insert into catvalues(1 ...

  3. SQL——将两列合并成一列

    将两列合并连接成一列,需要注意的是列的格式必须是NVARCHAR或者VARCHAR类型 ), call_uuid, ) +','+agent_code '   PerDate 1 ,980408102 ...

  4. javaee 第14周

    1.web server Web Server中文名称叫网页服务器或web服务器.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务.Web服务器可以解析( ...

  5. HashMap详解 基于jdk1.7

    转载自:http://zhangshixi.iteye.com/blog/672697 1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操 ...

  6. app自动化配置信息

    caps={    "platformName":"Android",#平台名称    "platformVersion":"6. ...

  7. clone对象或数组

    function clone(obj) { var o; if (typeof obj == "object") { if (obj === null) { o = null; } ...

  8. vs2008控件查看器

    使用 OLE/COM 对象查看器 通过读取控件的类型库,OLE/COM 对象查看器使您得以查看控件的接口. 使用 OLE/COM 对象查看器 单击“工具”菜单上的“OLE/COM 对象查看器”或在命令 ...

  9. MySQL-01 MySQL数据库安装指南

    学习要点 MySQL数据库的安装和设置 下载mysql mysql官网:https://www.mysql.com/downloads/ 主要版本: Oracle MySQL Cloud Servic ...

  10. 【传智播客】Libevent学习笔记(三):事件循环

    目录 00. 目录 01. event_base_loop函数 02. event_base_dispatch函数 03. event_base_loopexit函数 04. event_base_l ...