dp——联合权值

原文链接:https://www.luogu.com.cn/problem/P1351

dp——联合权值_第1张图片
dp——联合权值_第2张图片

解析:
dp——联合权值_第3张图片

AC代码:

#include
#include
#include
#include
using namespace std;
int n,maxn=0,ans=0,sum=0;
int mapp[2000005];
int head[2000005],cnt=0;
typedef struct edged{
	int fr,to,next;
}edged;
edged edge[4000005];
void add(int u,int v){
	edge[cnt].fr=u;
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt;
	cnt++;
}
int main(){
	int i,j;
	memset(head,-1,sizeof(head));
	memset(mapp,0,sizeof(mapp));
	cin>>n;
	for(i=0;i<n-1;i++){
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	for(i=1;i<=n;i++){
		cin>>mapp[i];
	}
	for(i=1;i<=n;i++){
		int t1=0,t2=0,max1=0,max2=0;
		for(j=head[i];j!=-1;j=edge[j].next){
			int e=edge[j].to;
			if(mapp[e]>max1){
				max2=max1;max1=mapp[e];
			}
			else if(mapp[e]>max2){
				max2=mapp[e];
			}
			t1=(t1+mapp[e])%10007;
			t2=(t2+mapp[e]*mapp[e])%10007;
		}
		maxn=max(maxn,max1*max2);
		sum=(sum+t1*t1%10007+10007-t2)%10007;
	}
	cout<<maxn<<" "<<sum<<endl;
	return 0;
}

你可能感兴趣的:(dp)