【题目链接】: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. 【Codeforces Round #437 (Div. 2) A】Between the Offices

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...

  2. SoC的Testbench中的简易bus_monitor(加入print函数)

    SoC的Testbench中的简易bus_monitor(加入print函数) 主要思路 向固定地址写信息 使用工具链将C写的print/printf函数编译成hex文件 在testbench中创建b ...

  3. zynq修改ramdisk文件系统

    ⑴ 挂载 Ramdisk新建目录 tmp, 并将 uramdisk.image.gz 拷贝至该目录$ cd <WORKDIR>/Filesystem$ mkdir tmp$ cp uram ...

  4. UVA Bandwidth

    题目例如以下: Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcsin VxV, and a ...

  5. wireshark分析包中关于三次握手和四次终止标识

    转自: http://hi.baidu.com/hepeng597/item/5ba27e0b98bc8de3ff240de0 三次握手Three-way Handshake 一个虚拟连接的建立是通过 ...

  6. redis 的惊群处理和分布式锁的应用例子

    在并发量比较高的情况下redis有很多应用场景,提升查询效率,缓解底层DBio ,下面列举两个平时开发中应用过的两个例子,欢迎各位一起讨论改进. 1 . redis 惊群处理 1.1 方案的由来 Re ...

  7. [Angular2 Form] Validation message for Reactive form

    <div class="form-field"> <label>Confirm Password: </label> <input typ ...

  8. okhttp的简介(二)之简单封装

    前一篇文章简单的介绍了okhttp的简单使用.okhttp的简介(一):http://blog.csdn.net/wuyinlei/article/details/50579564 相信使用还是非常好 ...

  9. Helloworld之Spring依赖注入/控制反转(DI/IoC)版

    Helloworld之Spring依赖注入/控制反转(DI/IoC)版 作者:雨水, 日期:2014-10-29 摘要:本文主要用于培训刚開始学习的人理解Spring中的依赖注入的基本概念. 先介绍依 ...

  10. TF-IDF计算方法和基于图迭代的TextRank

    文本处理方法概述 说明:本篇以实践为主,理论部分会尽量给出参考链接 摘要: 1.分词 2.关键词提取 3.主题模型(LDA/TWE) 4.词的两种表现形式(词袋模型和分布式词向量) 5.关于文本的特征 ...