【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=2376

【题意】



让你计算树上任意两点之间的距离的和.

【题解】



算出每条边的两端有多少个节点设为num1和num2;

这条边的边权为w;

答案累加上w*num1*num2;

然后总的答案除n*(n-1)/2;



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%lld",&x)
  14. #define ref(x) scanf("%lf",&x)
  15. typedef pair<int, int> pii;
  16. typedef pair<LL, LL> pll;
  17. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  18. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  19. const double pi = acos(-1.0);
  20. const int N = 1e4+100;
  21. int n;
  22. LL sum[N];
  23. double ans;
  24. vector <int> G[N];
  25. vector <LL> w[N];
  26. void in()
  27. {
  28. rei(n);
  29. rep1(i, 1, n)
  30. G[i].clear(), w[i].clear();
  31. rep1(i, 1, n - 1)
  32. {
  33. int x, y; LL z;
  34. rei(x), rei(y), rel(z);
  35. x++, y++;
  36. G[x].push_back(y),G[y].push_back(x);
  37. w[x].push_back(z), w[y].push_back(z);
  38. }
  39. }
  40. void dfs(int x, int fa)
  41. {
  42. sum[x] = 1;
  43. int len = G[x].size();
  44. rep1(i,0,len-1)
  45. {
  46. int y = G[x][i];
  47. if (y == fa) continue;
  48. dfs(y, x);
  49. sum[x] += sum[y];
  50. ans += 1LL*(n - sum[y])*sum[y] * w[x][i];
  51. }
  52. }
  53. void o()
  54. {
  55. double tt = n*(n - 1) / 2;
  56. ans /= tt;
  57. printf("%.8f\n", ans);
  58. }
  59. int main()
  60. {
  61. //freopen("F:\\rush.txt", "r", stdin);
  62. int t;
  63. rei(t);
  64. while (t--)
  65. {
  66. ans = 0;
  67. in();
  68. dfs(1, 0);
  69. o();
  70. }
  71. //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
  72. return 0;
  73. }

【hdu 2376】Average distance的更多相关文章

  1. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  2. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  3. 一本通1619【例 1】Prime Distance

    1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【22.95%】【hdu 5992】Finding Hotels

    Problem Description There are N hotels all over the world. Each hotel has a location and a price. M ...

  9. 【hdu 3863】No Gambling

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...

随机推荐

  1. Altium Designer如何调整鼠标形状

    在 里面有一个

  2. netty检测系统工具PlatformDependent

    1. 检测jdk版本 @SuppressWarnings("LoopStatementThatDoesntLoop") private static int javaVersion ...

  3. mysql的入门基础操作

    1.数据库的简单介绍 1.1 什么是数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle  是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IB ...

  4. WCF REST 基础教程

    概述 Representational State Transfer(REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 因此REST是设计风格而不是标准,R ...

  5. POJ 2823 Sliding Window 线段树

    http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...

  6. Linux上制作Window启动盘

    Linux上制作Window启动盘 注意: U盘在Linux中的标签(依具体情况而定:执行df查看) U盘 ----- /dev/sdb4 格式化U盘 建立U盘的启动分区 安装关键工具 ms-sys ...

  7. [Now] Deploy a Node project with Zeit’s Now

    Use Zeit’s now to deploy a node application from your local machine to a remote cloud service in mom ...

  8. C# 二分法查找和排序

    using System;using System.Collections.Generic;using System.Text; namespace AAA{    public class Dich ...

  9. 想要搞BGM,没有歌曲链接怎么办?

    有对于想要做个个人网站BGM,而非商业用途和非法用途,这只是个小技巧,仅限于个人娱乐使用. 方法一: 首先打开酷狗网页端 搜索想要的音乐名字 进入播放页面 进入开发者模式(右键鼠标->检查或者直 ...

  10. css hover控制其他元素

    <html> <body> <style> #a:hover {color : #FFFF00;} #a:hover > #b:first-child{col ...