求助大佬,为何50分?
查看原帖
求助大佬,为何50分?
322120
H2O_MERO楼主2023/10/3 16:49

我和第一篇题解对了一下,没区别呀!

#include<iostream>
#include<vector>
#include<string.h>
#include<cstdio>
using namespace std;

const int N = 10e5 + 15 , K = 20 + 2;
int cnt = 0 , k;
vector<int> t[N];
struct edge
{
	int child , next;
}e[N*2];
int head[N];
//memset( head , -1 , sizeof(head) );
long long int d[N][K] , b[N][K];
int v[ N ];

void add( int x , int y )
{
	e[cnt].child = y;
	e[cnt].next = head[x];
	head[x] = cnt;
	cnt++;
}

int read()
{
	int x = 0 , f = 1;
	char c = getchar();
	while( c < '0' || c > '9' ){
		if( c == '-' )
			f = -1;
		c = getchar();
	}
	while( c >= '0' && c <= '9' ){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

void dfsa( int son , int fa )
{
	for( int i = 0 ; i <= k ; i++ )
		d[son][i] = v[son];
	for( int i = head[son] ; i ; i = e[i].next ){
		int grand = e[i].child;
		if( grand != fa ){
			dfsa( grand , son );
			for( int i = 1 ; i <= k ; i++ )
				d[ son ][ i ] += d[ grand ][ i - 1 ];
		}
		
	}
}

void dfsb( int son , int fa )
{
	for( int i = head[son] ; i ; i = e[i].next ){
		int grand = e[i].child;
		if( grand != fa )
		{
			b[grand][1] += d[son][0];
			for( int i = 2 ; i <= k ; i++ )
				b[grand][i] += b[son][i-1] - d[grand][i-2];
			dfsb( grand , son );
		}
	}
}

int main()
{
	int n = read();
	k = read();
	for( int i = 1 ; i < n ; i++ ){
		int x = read() , y = read();
		add( x , y );
		add( y , x );
	}
	for( int i = 1 ; i <= n ; i++ ){
		v[i] = read();
	}
	
	dfsa( 1 , 1 );
	for( int i = 1 ; i <= n ; i++ )
		for( int j = 0 ; j <= k ; j++ )
			b[i][j] = d[i][j];
	dfsb( 1 , 1 );
	for( int i = 1 ; i <= n ; i++ )
		cout << b[i][k] << endl;
	
	return 0;
}
2023/10/3 16:49
加载中...