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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- STM32硬件错误HardFault_Handler的处理方法
https://blog.csdn.net/electrocrazy/article/details/78173558
- python相关软件安装流程图解——Windows下安装Redis以及可视化工具——Redis-x64-3.2.100——redis-desktop-manager-0.9.3.817
https://www.2cto.com/database/201708/666191.html https://github.com/MicrosoftArchive/redis/releases ...
- ASP.NET的底层体系2
文章引导 1.ASP.NET的底层体系1 2.ASP.NET的底层体系2 引言 接着上一篇ASP.NET的底层体系1我们继续往下走 一.System.Web.HttpRuntime.ProcessRe ...
- python编码(31-01)
以什么方式编码,就以什么方式解码! 第一种编码与解码方式: encode()编码 decode()解码 type()查看数据类型 repr()查看数据内容 s = '你好'print(type(s)) ...
- shell脚本练习02--求字符串的长度
######################################################################### # File Name: -.sh # Author ...
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- 保持SSH连接的linux服务器不断线
目录 1. secureCRT和putty连接设置 2. 命令行设置 3. 服务器配置修改 4. 参考 使用ssh连接远程服务器的时候,如果长时间没有操作,远程连接就有可能中断.正在执行的程序和vim ...
- PCA降维的前世今生
PCA降维的数学原理 PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分 ...
- opencv-图像遍历
#include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...
- linux 解压 WinRAR 压缩文件
1.Download rar for linux wget http://www.rarlab.com/rar/rarlinux-x64-5.5.b1.tar.gz 2.Configure rar t ...