Hdu 2376
题意:给出一颗含有n个结点的树,树上每条边都有一个长度,求树上所有路径的平均长度。
考虑树上每条边对所有路径长度和的贡献,对于每条偶 就是边的两个短点u和v,只需要记录以u为根的子树的结点的个数,
那么不在结点u为根的子树上的结点个数为n-count[u], 所有该边对总长度的贡献就是count[u] * (n - count[u]) * cost.
最后用总长度除以总的边数(n * (n - 1) / 2)., 有点坑的是:eps我调到1e-11才过。。。
/*************************************************************************
> File Name: 2376.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年10月17日 星期五 23时10分56秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ typedef pair<int, int> pii;
typedef long long LL;
vector<pii> G[];
LL res;
//int tot[10050];
int n; int dfs(int u, int fa) {
int sz = G[u].size();
//tot[u] = 1;
// if (sz == 1) return 1;
int tot = ;
for (int i = ; i < sz; i++) {
int v = G[u][i].first, w = G[u][i].second;
if (v != fa) {
int cnt = dfs(v, u);
tot += cnt;
res += (LL)cnt * (n - cnt) * w;
}
}
return tot;
} int main(void) {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = ; i < n; i++) G[i].clear();
for (int i = ; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back(pii(v, w));
G[v].push_back(pii(u, w));
}
res = ;
dfs(, -);
cout << fixed << setprecision() << (res + 0.0) / (n * (n - ) / ) << endl;
}
return ;
}
Hdu 2376的更多相关文章
- HDU 2376 树形dp|树上任意两点距离和的平均值
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...
- 【hdu 2376】Average distance
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2376 [题意] 让你计算树上任意两点之间的距离的和. [题解] 算出每条边的两端有多少个节点设为n ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- golang中time包的使用
一.代码 package main; import ( "time" "fmt" ) func main() { //time.Time代表一个纳秒精度的时间点 ...
- IDEA被删除的模块在编译时会再次出现
工程根目录下.idea文件->compiler.xml,删除多余的model,workspace.xml->删除带有无用的target标签
- 总结加密、机密jar中的class
1.加密和解密部署到jboss中间件中的的单个class文件,原理:使用“java源程序加密解决方案(基于Classloader解密) (2014-07-13 11:31)”blog即可实现: imp ...
- linux下phpstudy安装
linux下phpstudy安装 一.总结 一句话总结: 就是下载然后一步步用指令安装即可 二.linux下phpstudy安装 参考:linux下phpstudy安装https://www.cnbl ...
- day3:python运算符及数据类型(str)(int)
运算符 算数运算 :a = 10 * 10赋值运算:a = a + 1 a+=1 比较运算:a = 1 > 5 逻辑运算: a = 1>6 or 1==1 a = 1 and b = ...
- 石子合并问题 /// 区间DP oj2025
Description 在一个圆形操场的四周摆放着n堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的两堆石子合并成新的一堆,并将新得的这堆石子数记为该次合并的得分. 试设计一个算法,计算出 ...
- java_增强for循环
增强for循环(foreach): 底层使用了迭代器,简化了迭代器的书写 JDK1.5新特性 所有的单列集合都可以使用增强for循环 for(集合/数组 的数据类型 变量名 : 集合名/数组名) pu ...
- MQTT入门介绍
一简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级&q ...
- UVA - 374
https://vjudge.net/problem/19685/origin 费马小定理优化快速幂 因为加了费马小定理优化,小心2 2 2这种情况,会出现0 0 2,也就是0的0次方,实际答案为0 ...
- DRF初识
目录 Web API接口 什么是Web API接口 接口四大特征 接口文档的编写测试 restful接口规范 url链接设计 五大请求方式 响应结果 DRF框架安装 基于原生Django实现十大接口 ...