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

【题意】



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

【题解】



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

这条边的边权为w;

答案累加上w*num1*num2;

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



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e4+100; int n;
LL sum[N];
double ans;
vector <int> G[N];
vector <LL> w[N]; void in()
{
rei(n);
rep1(i, 1, n)
G[i].clear(), w[i].clear();
rep1(i, 1, n - 1)
{
int x, y; LL z;
rei(x), rei(y), rel(z);
x++, y++;
G[x].push_back(y),G[y].push_back(x);
w[x].push_back(z), w[y].push_back(z);
}
} void dfs(int x, int fa)
{
sum[x] = 1;
int len = G[x].size();
rep1(i,0,len-1)
{
int y = G[x][i];
if (y == fa) continue;
dfs(y, x);
sum[x] += sum[y];
ans += 1LL*(n - sum[y])*sum[y] * w[x][i];
}
} void o()
{
double tt = n*(n - 1) / 2;
ans /= tt;
printf("%.8f\n", ans);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int t;
rei(t);
while (t--)
{
ans = 0;
in();
dfs(1, 0);
o();
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【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. 阿里云部署Docker(3)----指令学习

    通过上两节的学习http://blog.csdn.net/minimicall/article/details/40119177 和http://blog.csdn.net/minimicall/ar ...

  2. STL_算法_删除(unique、unique_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) 全部容器适用 unique(b,e) unique(b,e,p) unique_copy(b1,e1,b2) unique ...

  3. 很吊炸天的Xcode插件,你想要的这都有

    整理自BigPolarBear的博客 .杂技杂记  以及CC此前整理. 古人云"工欲善其事必先利其器",打造一个强大的开发环境.是马上提升自身战斗力的绝佳途径!下面是搜集的一些有力 ...

  4. web.config访问走代理的配置

    <system.net>    <defaultProxy>      <proxy bypassonlocal="False" usesystemd ...

  5. Php无限层级并显示层级数

    今天在处理递归无限层级菜单时,遇到一个稍微烧脑的问题,如何显示当前节点所在的层级数.废话不多说,我们先看个直观的无限层级: <?php // 这里的arr是直接从数据库取出的,仅作为测试数据 $ ...

  6. SVN—怎样安装SVNclient软件

            一.怎样安装TortoiseSVN-1.7.12.24070-win32-svn-1.7.9版本号的SVNclient软件:        a.下载TortoiseSVN-1.7.12 ...

  7. Openstack nova(二)——架构(一)

    架构源自需求 需求分析 软件架构大部分都来自于需求.能够说.有什么样的需求,就会有什么样的架构, 尽管不同一时候期,不同的人来实现,可能不全然一样.可是整体来说, 架构不会相差太远. 如今假设假设须要 ...

  8. docker安装及问题处理

    1.在Ubuntu的命令行中输入 sudo apt-get install docker.io 2.如果切换到了root用户下 apt-get install docker.io 3.对于新安装的Ub ...

  9. Asp.NETCore让FromServices回来

    起因 这两天,我忽然有点怀念 Asp.NET MVC 5 之前的时代,原因是我看到项目里面有这么一段代码(其实不止一段,几乎每个 Controller 都是) [Route("home&qu ...

  10. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...