洛谷—P1351权值联合(LCA、图论)

在这里插入图片描述
解题思路:
题中隐含了一个重要的信息,这是一个无环图,所以与同一个节点相连的两个节点都满足条件
另外有一个点需要减复杂度
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y;
};
int wit[200020],n,m,s;
bool visit[200020];
vector<int> arr[200020];
vector<node> v;
int main()
{
	cin>>n;
	for(int i=0;i<n-1;++i)
	{
		int x,y;
		cin>>x>>y;
		arr[x].push_back(y);
		arr[y].push_back(x);
	}
	for(int i=1;i<=n;++i)
	cin>>wit[i];
	for(int i=1;i<=n;++i)
	{
		int temp1=0,temp2=0,max1=0,max2=0;
		for(auto c:arr[i])
		{
			if(wit[c]>max1){
				max2=max1;
				max1=wit[c];
			}
			else if(wit[c]>max2) max2=wit[c];
			temp1=(temp1+wit[c])%10007;
			temp2=(temp2+wit[c]*wit[c])%10007;
		}
		temp1=temp1*temp1%10007;
		s=(s+temp1-temp2+10007)%10007;
		if(m<max1*max2) m=max1*max2;
	}
	cout<<m<<' '<<s<<endl;
	return 0; 
} 
发布了165 篇原创文章 · 获赞 11 · 访问量 4885

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/104701454
今日推荐