滑稽树下你和我&&Average distance(树形dp求任意两点距离之和)

滑稽树下你和我.

链接:https://ac.nowcoder.com/acm/contest/992/J
来源:牛客网

题目描述
红红和蓝蓝是随机降生在苹果树上的苹果仙灵,现在红线仙想估测他们的CP系数,并决定是否使他们成为一对CP。
给出n个结点n-1条边的树,节点编号为1到n,定义distance(i,j)为i与j的树上距离。
CP系数是指所有红红和蓝蓝在不同位置i,j的distance(i,j)之和。
即 \sum_{i=1}{n-1}{\sum_{j=i+1}{n}{distance(i,j)}}∑
i=1
n−1


j=i+1
n

distance(i,j)。
求红红和蓝蓝的CP系数,对109+7取模。
输入描述:
第一行一个整数n( 1 < n <= 105 ),表示树的结点个数。
随后n-1行,每行三个整数a,b,c ( 1 <= a,b <= n ),( 0 <= c <= 109 ),表示结点a,b之间有一条权值为c的边,( a \ne


= b )。
输出描述:
一行一个整数,表示CP系数对109+7取模的结果。
示例1
输入
复制

4
1 2 1
2 3 1
2 4 1

输出
9
一条边一条边的加肯定会超时,把树中边的两边节点个数相乘就是经过此节点的次数(s[x]*(n-s[x])),dfs可以求出了,再乘权重。

#include<stdio.h>
#include<string.h>
typedef long long ll;
const int maxn=1e5+5;
ll ans=0,t=1e9+7;
int w[maxn],v[maxn],next[maxn],n,head[maxn],c=0,s[maxn]={0};
void add(ll x,ll y,ll z){
	v[c]=y;
	w[c]=z;
	next[c]=head[x];
	head[x]=c++;
}
void dfs(int x,int y){
	s[x]=1;
	for(int i=head[x];i!=-1;i=next[i]){
		if(v[i]!=y){
			dfs(v[i],x);
			s[x]+=s[v[i]];
			ans=(ans+1ll*s[v[i]]*(n-s[v[i]])%t*w[i]%t)%t;
		}
	}
}
int main()
{
    long long int len,i,z,j,k,x,y,c;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(i=1;i<n;i++){
    	scanf("%lld %lld %lld",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);  	  	
	}
	dfs(1,0);
	printf("%lld\n",ans);
    return 0;
}

Average distance

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 (d01 + d02 + d03 + d04 + d12 +d13 +d14 +d23 +d24 +d34)/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.

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

Sample Input
1
5
0 1 6
0 2 3
0 3 7
3 4 2

Sample Output
8.6

dfs里面求和没有加long long卡了半天-_-||

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
typedef pair<int,int> pir;
const int maxn=20005;
vector< pir >v[maxn];
int s[maxn],n;
long long ans,cont;
void dfs(int x,int y){
	for(int i=0;i<v[x].size();i++){
		pir u=v[x][i];
		int to=u.first;
		int w=u.second;
		if(to!=y){
			dfs(to,x);
			s[x]+=s[to];
			ans+=(long long )s[to]*(n-s[to])*w;			
		}
	}
}
int main()
{
	int i,j,t,x,y,z;
	scanf("%d",&t);
	while(t--){		
		scanf("%d",&n);
		for(i=0;i<n;i++){
			v[i].clear();
			s[i]=1;
		}			
		for(i=1;i<n;i++){
			scanf("%d %d %d",&x,&y,&z);
			v[x].push_back(make_pair(y,z));
			v[y].push_back(make_pair(x,z));
		}
		ans=0;
		dfs(0,-1);		
		double p=(n-1)*n/2;
		printf("%.6lf\n",(double)ans/p);
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值