比赛 近期练习题回顾 评测结果 AAAAAAAAAA
题目名称 联合权值 最终得分 100
用户昵称 梦那边的美好ET 运行时间 0.554 s
代码语言 C++ 内存使用 24.34 MiB
提交时间 2018-10-31 07:42:15
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
vector<long long>b[200010];
long long n,w[200010],sum=0,ans,f[200010],ma1[200010],ma2[200010],a1,a2;
int main(){
	freopen("linkb.in","r",stdin);
	freopen("linkb.out","w",stdout);
	scanf("%lld",&n);
	for(int i=1;i<=n-1;i++){
	    scanf("%lld%lld",&a1,&a2);
		b[a1].push_back(a2);
		b[a2].push_back(a1);
	}
	for(int i=1;i<=n;i++)scanf("%lld",&w[i]);
	for(int i=1;i<=n;i++){
	    for(int j=0;j<b[i].size();j++){
		    f[i]+=w[b[i][j]];
			if(ma1[i]==0){ma1[i]=w[b[i][j]];continue;}
			if(ma2[i]==0){ma2[i]=w[b[i][j]];continue;}
			if(ma1[i]<=w[b[i][j]]){ma2[i]=ma1[i];ma1[i]=w[b[i][j]];continue;}
			if(ma2[i]<w[b[i][j]])ma2[i]=w[b[i][j]];
		}
		for(int j=0;j<b[i].size();j++)sum+=(f[i]-w[b[i][j]])*w[b[i][j]];
		ans=max(ans,ma1[i]*ma2[i]);
	}
	printf("%d ",ans);printf("%d",sum%10007);
    return 0;
}