题干:

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 (d 01 + d 02 + d 03 + d 04 + d 12 +d 13 +d 14 +d 23 +d 24 +d 34)/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

题目大意:

给你一棵带权值的树(共有n*(n-1)/2条路径),现在问你这些路径的平均值是多少。

解题报告:

计算每条边的入选的次数(也就是算这条边对答案的贡献),也就是它所连的两个点的点的乘积。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
struct Edge {int to;ll w;Edge(){}Edge(int to,ll w):to(to),w(w){}
};
ll dp[MAX];
int n;//从0到n-1
vector <Edge> vv[MAX];
ll ans;
void dfs(int cur,int root) {int up = vv[cur].size();ll res = 0;for(int i = 0; i<up; i++) {Edge v = vv[cur][i];if(v.to == root) continue;
//      if(v.to == root) {
//          res += v.w;
//          continue;
//      }
//      ll tmp = dfs(v.to,cur);
//      ans += tmp;
//      res += tmp;dfs(v.to,cur);dp[cur] += dp[v.to];ans += dp[v.to] * (n-dp[v.to]) * v.w;}
//  return res;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);ans=0;for(int i = 0; i<n; i++) vv[i].clear(),dp[i] = 1;for(int i = 1; i<=n-1; i++) {int a,b;ll w;scanf("%d%d%lld",&a,&b,&w);vv[a].pb(Edge(b,w));vv[b].pb(Edge(a,w));}//ans += dfs(0,-1);dfs(0,-1);printf("%.6f\n",ans*1.0/(n*(n-1)/2));}return 0 ;
}

总结:

代码中注释掉的是写的一个longlong返回类型的一个dfs函数的思路,,,是不对的。。。那样写的话思路不是很明确,,应该得搞半天或许能搞出来、、、

【HDU - 2376】Average distance (树,平均距离,算贡献)相关推荐

  1. HDU 2376 Average distance

    HDU_2376 对于任意一棵子树来讲,以根节点为深度最浅的点的路径一共有两类,一类是以根节点为端点的路径,另一类是过根节点但端点分别在两棵子树中的路径.然后将无根树转化为有根树后dfs时计算出上面两 ...

  2. 滑稽树下你和我Average distance(树形dp求任意两点距离之和)

    滑稽树下你和我. 链接:https://ac.nowcoder.com/acm/contest/992/J 来源:牛客网 题目描述 红红和蓝蓝是随机降生在苹果树上的苹果仙灵,现在红线仙想估测他们的CP ...

  3. 【HDU-2376】Average distance

    [HDU-2376]Average distance 题意: 给你一个树,求树上任意两个点之间的距离的平均值 题解: 就是求出任意两点之间的距离和然后除以边数 "任意两点之间的距离" ...

  4. HDU 1036 Average is not Fast Enough!:题目解答源码

    2019独角兽企业重金招聘Python工程师标准>>> HDU 1036 Average is not Fast Enough!:题目解答源码 A relay is a race f ...

  5. 【HDU 6973】Bookshop 树剖+平衡树

    [HDU 6973]Bookshop 树剖+平衡树 [引言] ​ 平衡树的题做得比较少,难得补一次神题,记录一下供以后学习. [题意] ​ 给出一棵 nnn 个结点的树,每个结点有一个价值为 pip_ ...

  6. HDU 2795 Billboard (线段树+贪心)

    HDU 2795 Billboard (线段树+贪心) 手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/articl ...

  7. 【51Nod - 1215 】数组的宽度 (单调栈 或 分治 或 单调队列,算贡献,需去重)

    题干: N个整数组成的数组,定义子数组aii..ajj的宽度为:max(ai..aj) - min(ai..aj),求所有子数组的宽度和. Input 第1行:1个数N,表示数组的长度.(1 < ...

  8. C. Serval and Toxel‘s Arrays(容斥+按值算贡献

    Problem - C - Codeforces 题意:一个 n 长的值成对不同数组,有 m 次变化,每次执行一次 p 位置,改为 v 值[保证每次改完的数组内的值成对不同],计算所有 m + 1 的 ...

  9. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

最新文章

  1. 如何进行网站主题模型优化_如何进行网站关键词优化
  2. GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败
  3. sqlservice对于时间的操作
  4. 基于单片机的调光控制器设计
  5. Qt图形界面编程入门(基本窗口及控件)
  6. 154 万 AI 开发者用数据告诉你,中国 AI 如何才能弯道超车?| 中国 AI 应用开发者报告
  7. .net中Task.WaitAll和WaitAny同步的等待Task完成
  8. 2021年Java开发者应该学习的技术
  9. poj2406 Power Strings 模式匹配算法KMP变形
  10. 《寄生虫》横扫奥斯卡,Python告诉你这部电影到底好在哪里?
  11. Android校园二手交易平台
  12. 亲爱的,别把上帝缩小了 ---- 读书笔记3
  13. struts2与常用表格ajax操作的json传值问题
  14. 计算机网络局域网之无线局域网
  15. 一对同居男女同一天的日记对比
  16. matlab中circle函数_MATLAB如何用自带函数画圆
  17. 智能运维 | 我们不一样!告诉你百度云如何做智能流量异常检测
  18. stm8l051 halt之后外部中断唤醒问题
  19. 电脑无法识别启动盘 不显示启动盘 wintogo WTG 移动硬盘盒+m.2等等
  20. ILSpy中baml转化为xaml的改进(四)

热门文章

  1. codeUp 2031 To fill or not to fill 复杂贪心
  2. mysql创建表格1warning_MySQL:创建、修改和删除表
  3. micropython stm32f407 以太网_[MicroPython]STM32F407开发板DIY声光控开关
  4. t检验的p值对照表_论文数据分析实战 | 如何对汇总数据进行t检验
  5. Linux以oracle用户登录,Linux_oracle 10G for linux常用命令,首先以Oracle用户登录1、 - phpStudy...
  6. 《C语言深度解剖》中的.c/.h 程序模板及函数注释风格
  7. ios view添加上边框_iOS开发之如何给View添加指定位置的边框线详解
  8. python 可视化监控平台_python可视化篇之流式数据监控的实现
  9. 左右伸缩_冬季装修为啥要留伸缩缝?等到天热地板开裂就晚了!合肥人注意下...
  10. 打开pjsip2.1版本的视频支持