hdu 2736 Average distance
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

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 the average distance between two vertices. This value should have either an absolute or a relative error of at most 10-6
5
0 1 6
0 2 3
0 3 7
3 4 2
哎,思路想复杂了,其实蛮简单的:
引:如果暴力枚举两点再求距离是显然会超时的。转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为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的更多相关文章
- 【hdu 2376】Average distance
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2376 [题意] 让你计算树上任意两点之间的距离的和. [题解] 算出每条边的两端有多少个节点设为n ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...
- hdu 4712 Hamming Distance ( 随机算法混过了 )
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 472 Hamming Distance (随机数)
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...
- HDU 4712 Hamming Distance(随机算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...
- HDU 4217 Hamming Distance 随机化水过去
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU ACM 2895-Edit distance
Edit distance Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
随机推荐
- re正则表达式公式讲解5
1.refullmatch() 完全匹配字符串则返回object,否则返回None import re s = "max@123uyt146" print(re.fullmatch ...
- 架包Error inflating class错误
当引用架包后,出现Error inflating class错误时通常要检测架包是否正确引用: 1.首先将你所需要的架包拷贝到工程目录下: 2.右击工程,选择Build Path-->confi ...
- 手写MVVM框架 之vue双向数据绑定原理剖析
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 洛谷 P1434 滑雪
题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...
- SAP CRM调查问卷的评分和图表显示功能介绍
SAP CRM里我们使用事务码CRM_SURVEY_SUITE创建一个调查问卷(Survey): 其中调查问卷的问题和答案均可分配权值(Rate),最后该问卷总的分数等于每个问题的权值乘以客户选择答案 ...
- sqlserver 数据库主外键关联错误
话题引入: 在建立主外键关系时,系统提示表"table2"中的列与现有的主键或UNIQUE约束不匹配 原因: 数据库表中只有一个主键,这个主键可以是多个列共同组成.所以table2 ...
- 样式化复选框(Styling Checkbox)
原理:https://www.tuicool.com/articles/y67jee 表现:http://www.freejs.net/demo/381/index.html https://www. ...
- thinkphp5 自定义验证码使用
控制器[https://blog.csdn.net/John_rush/article/details/80169702] public function verify(){ $captcha = n ...
- 10. InnoDB表空间加密
10. InnoDB表空间加密 InnoDB支持存储在单独表空间中的表的数据加密 .此功能为物理表空间数据文件提供静态加密. 详细信息见官方文档
- svn上传项目
1.桌面右键单击 2.进行项目导入 3.选择项目所在目录 4.