做完之后感觉 这个题应该是需要一些数学思想
题解看这个博客:链接
代码如下
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
#define in = read();
typedef long long ll;
typedef unsigned int ui;
const ll size = 1000000 + 10000;
struct point{ ll next,to,dis;}edge[size];
ui n;
ui a[size];
ui site,ans,maxans;
ui head[size];
inline ll read(){
ll num = 0 , f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num*10 + ch - '0';
ch = getchar();
}
return num*f;
}
inline void add(ll x,ll y){
edge[++site].next = head[x];
edge[site].to = y;
head[x] = site;
}
int main(){
n in;
for(int i=1;i<n;i++){
ui x,y; x in; y in;
add(x,y); add(y,x);
}
for(int i=1;i<=n;i++){
a[i] in; a[i] %= 10007;
}
for(register int i=1;i<=n;i++){
ll sum = 0; ui max1= 0 , max2 = 0;
for(register int j=head[i];j;j=edge[j].next){
if(a[edge[j].to] > max1){
max2 = max1;
max1 = a[edge[j].to];
}
else if(a[edge[j].to] > max2)
max2 = a[edge[j].to];
ans = (ans + sum*a[edge[j].to])%10007;
sum = (sum + a[edge[j].to])%10007;
maxans = max(maxans , max1*max2);
}
}
ans = (ans*2)%10007;
printf("%d %d",maxans,ans);
}
//COYG