【hdu 2376】Average distance
【题目链接】: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的更多相关文章
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 一本通1619【例 1】Prime Distance
1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【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 ...
- 【hdu 3863】No Gambling
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...
随机推荐
- Altium Designer如何调整鼠标形状
在 里面有一个
- netty检测系统工具PlatformDependent
1. 检测jdk版本 @SuppressWarnings("LoopStatementThatDoesntLoop") private static int javaVersion ...
- mysql的入门基础操作
1.数据库的简单介绍 1.1 什么是数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle 是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IB ...
- WCF REST 基础教程
概述 Representational State Transfer(REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 因此REST是设计风格而不是标准,R ...
- POJ 2823 Sliding Window 线段树
http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...
- Linux上制作Window启动盘
Linux上制作Window启动盘 注意: U盘在Linux中的标签(依具体情况而定:执行df查看) U盘 ----- /dev/sdb4 格式化U盘 建立U盘的启动分区 安装关键工具 ms-sys ...
- [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 ...
- C# 二分法查找和排序
using System;using System.Collections.Generic;using System.Text; namespace AAA{ public class Dich ...
- 想要搞BGM,没有歌曲链接怎么办?
有对于想要做个个人网站BGM,而非商业用途和非法用途,这只是个小技巧,仅限于个人娱乐使用. 方法一: 首先打开酷狗网页端 搜索想要的音乐名字 进入播放页面 进入开发者模式(右键鼠标->检查或者直 ...
- css hover控制其他元素
<html> <body> <style> #a:hover {color : #FFFF00;} #a:hover > #b:first-child{col ...