题目链接:点击打开链接
Problem Description
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.
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
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
代码:
#include <iostream>
#include<cstdio>
#include<vector>
#include<cstring>
const int maxn=1e4+5;
using namespace std;
vector<pair<int ,int> >m[maxn];
int t,n,k;
int a[maxn];//i子树(包括自己)节点的个数
int vis[maxn];//标记是否访问过次节点
double sum=0;
int dfs(int x,int y)
{
vis[x]=1;
if(m[x].size()==1&&x!=0)
{
sum+=1.0*(n-1)*y;
return a[x];
}
for(int i=0;i<m[x].size();i++)
{
int v=m[x][i].first;
int w=m[x][i].second;
if(!vis[v])
{
a[x]+=dfs(v,w);
}
}
sum+=a[x]*1.0*(n-a[x])*y;
}
int main()
{
cin>>t;
while(t--)
{
sum=0;
memset(vis,0,sizeof(vis));
cin>>n;
for(int i=0;i<=n;i++)
{
a[i]=1;
m[i].clear();
}
for(int i=0;i<n-1;i++)
{
int x,y,v;
cin>>x>>y>>v;
m[x].push_back(make_pair(y,v));
m[y].push_back(make_pair(x,v));
}
dfs(0,0); a[0]=0;
double num=1.0*n*(n-1)/2;
printf("%.7lf\n",sum/num);//一注意要精确度
}
return 0;
}