P1351 联合权值
https://www.luogu.org/problem/show?pid=1351
就是一道数学题
#include<iostream> #include<queue> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=200009; const int P=10007; int n; int h[N],nex[N*2],to[N*2],cnt; int w[N]; long long ans,tot,maxn,sum,max1,max2; int main() { // freopen("link.in","r",stdin); // freopen("link.out","w",stdout); scanf("%d",&n); for(int i=1,x,y;i<n;i++) { scanf("%d%d",&x,&y); to[++cnt]=y,nex[cnt]=h[x],h[x]=cnt; to[++cnt]=x,nex[cnt]=h[y],h[y]=cnt; } for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int x=1;x<=n;x++) { tot=0;sum=0; max1=0;max2=0; for(int i=h[x];i;i=nex[i]) { tot+=w[to[i]]; if(1LL*w[to[i]]>max1) { max2=max1; max1=w[to[i]]; }else if(1LL*w[to[i]]>max2) max2=w[to[i]]; sum+=1LL*w[to[i]]*w[to[i]]; } maxn=max(maxn,max1*max2); tot=(tot*tot-sum)%P; ans=( ans+tot ); } while(ans<0) ans=(ans+P)%P; ans=ans%P; printf("%lld %lld\n",maxn,ans); return 0; }