原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376

  经典问题,求的是树上任意两点和的平均值。

  这里我们不能枚举点,这样n^2的复杂度。我们可以枚举每一条边,设这条边的端点分别为A、B,则通过这条边的路径总数为与A点相连的端点数乘以与B点相连的端点数,再乘以这条边的权值,将所有的和相加,最后除以n*(n-1)/2就可以了(除以2是因为这里每条边重复计算了两次)。

  这里统计求和的时候,一遍深搜就可以了,需要注意的是,假设某个点子树所包含点的个数为k,则这条边另一个端点所包含的点的个数的为n-k。

 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node{
int v;//终点
int w;//权值
};
vector<node> tree[maxn];
long long dp[maxn];
int sum[maxn];//统计每个点子树所包含点的个数
int n;
void dfs(int cur,int father){
sum[cur] = ;
for(int i = ;i<tree[cur].size();i++){
int son = tree[cur][i].v;
long long len = tree[cur][i].w;
if(father == son)
continue;
dfs(son,cur);
sum[cur] += sum[son];
dp[cur] += dp[son]+sum[son]*(n-sum[son])*len;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ;i<n;i++)
tree[i].clear();
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
int u,v,w;
for(int i = ;i<n-;i++){
scanf("%d%d%d",&u,&v,&w);
node t1,t2;
t1.v = v;
t1.w = w;
t2.v = u;
t2.w = w;
tree[u].push_back(t1);
tree[v].push_back(t2);
}
//以任意一点为根进行搜索
dfs(,-);
printf("%lf\n",dp[]*2.0/n/(n-));
}
return ;
}

HDU 2376 树形dp|树上任意两点距离和的平均值的更多相关文章

  1. HDU2376Average distance(树形dp|树上任意两点距离和的平均值)

    思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...

  2. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  3. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

  4. 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】

    学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...

  5. caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST

    caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...

  6. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  7. HDU_5723_最小生成树+任意两点距离的期望

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  9. hdu 4607 (树形DP)

    当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...

随机推荐

  1. jmeter(七)定时器

    知识来源有点复杂,其他测试工作者的博客,百度百科,搜集的电子文档,个人理解等等,限于水平和理解能力,可能有些内容有错误的地方... jmeter提供了很多元件,帮助我们更好的完成各种场景的性能测试,其 ...

  2. thinkphp怎么实现图片验证码

    1.控制器 function verify() { ob_clean();//丢弃输出缓冲区中的内容 $config = array( 'fontSize' => 20, // 字体大小 'le ...

  3. Leetcode:378. Kth Smallest Element in a Sorted Matrix

    题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

  4. vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错

    包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...

  5. dockerRegistry搭建

    docker registry安装: 官方仓库下载registry     pull镜像: fu@ubuntu:~$ sudo docker pull registry    运行镜像 : sudo ...

  6. Android BLE 蓝牙编程(二)

    大家中秋快乐啊--哈哈,今天继续工程项目吧! 上篇我们已经实现了蓝牙设备的扫描,本篇我们来通过list展示扫描到的设备并 实现点击连接. 先贴出上篇的完整的MainActivity的方法: packa ...

  7. 解析 XML

    解析 XML 文档: 下面的代码片段把 XML 文档解析到 XML DOM 对象中: if (window.XMLHttpRequest){// code for IE7+, Firefox, Chr ...

  8. Linux下压缩音频文件

    安装工具 sudo apt-get install lame 具体用法可以查看帮助  lame --help 通过更改音频文件的帧数 可以让文件变小  但是音质也会随之下降 现在比较多的mp3文件是1 ...

  9. Debian8搭建php环境

    安装apache 新装的系统发现 apt-get install apach<tab> 没有自动补全 请查看 这里 apt-get install apache2 安装mysql apt- ...

  10. Leetcode 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...