#include<cstdio>
#define MAXN 200005
#define mod 10007
using namespace std;
inline int read(){
int num=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')num=num*10+ch-48,ch=getchar();
return num*f;
}
struct size{
int v;
int next;
}G[MAXN<<1];
struct{
int dp1,dp2;//第一大、第二大子节点
long long sum;//所有子节点权值的和
int w;//权值w
int fa;//父亲
}Node[MAXN];
int head[MAXN];
long long max = 0;
long long ans = 0;
int tot = 0;
int N;
inline void add(int u,int v){
G[++tot].v=v;G[tot].next=head[u];head[u]=tot;
}
inline void find_father_and_get_dp(int rt,int fa){
//当前访问节点和此节点的父节点
Node[rt].fa=fa;
for(register int i=head[rt];i;i=G[i].next){
int v = G[i].v;
if(v==fa)continue;
Node[rt].sum+=Node[v].w;
if(Node[v].w>Node[rt].dp1){//如果大于dp1
Node[rt].dp2 = Node[rt].dp1;//更新dp2
Node[rt].dp1 = Node[v].w;//更新dp1
}
else if(Node[v].w>Node[rt].dp2){//如果不大于dp1又大于dp2
Node[rt].dp2 = Node[v].w;//更新dp2
}
find_father_and_get_dp(v,rt);//递归
}
}
inline void dfs(int rt,int fa){
if(fa>1){
long long w = ((long long)Node[Node[fa].fa].w*Node[rt].w);
if(w>max)max=w;
ans = (ans + (w<<1)) % mod;
}
long long dp = (long long)Node[rt].dp1*Node[rt].dp2;
if(dp>max)max=dp;
for(register int i=head[rt];i;i=G[i].next){
int v = G[i].v;if(v==fa)continue;
long long sum = Node[rt].sum - Node[v].w;
ans = (ans + (long long)Node[v].w*sum) % mod;
dfs(v,rt);
}
}
int main(){
N=read();
int u,v;
for(register int i=1;i<N;++i){
u=read();v=read();add(u,v);add(v,u);
}
for(register int i=1;i<=N;++i){
Node[i].w=read();
Node[i].dp1=0;Node[i].dp2=0;
Node[i].sum=0;
}
find_father_and_get_dp(1,0);
dfs(1,0);
printf("%lld %lld",max,ans);
return 0;
}
转载于:https://www.cnblogs.com/Neworld2002/p/8486057.html