悬棺求调,Wa on#3#4#7#8 48pts
查看原帖
悬棺求调,Wa on#3#4#7#8 48pts
1085779
duanhongwen楼主2024/11/22 19:23
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7;
struct Edge
{
	int nxt,to;
}edge[N<<1];
struct Point
{
	int head,value;
}point[N];
int n,maxx=LONG_LONG_MIN,cnt,dp[N][2];
void addedge(int a,int b)
{
	edge[++cnt].nxt=point[a].head;
	point[a].head=cnt;
	edge[cnt].to=b;
}
void dfs(int nownode,int fa)
{
	for(int i=point[nownode].head;i;i=edge[i].nxt)
	{
		int y=edge[i].to;
		if(y!=fa)
		{
			dfs(y,nownode);
			dp[nownode][0]=max(dp[y][0],max((long long)0,dp[y][1]));
			dp[nownode][1]+=max((long long)0,dp[y][1]);
		}
	}
}
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		dp[i][0]=LONG_LONG_MIN;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>point[i].value;
		dp[i][1]=point[i].value;
		maxx=max(point[i].value,maxx);
	}
	for(int i=1;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		addedge(a,b);
		addedge(b,a);
	}
	dfs(1,0);
	if(maxx<=0)
	{
		cout<<"0";
		return 0;
	}
	cout<<max(dp[1][0],max(dp[1][1],point[1].value));
	return 0;
}
2024/11/22 19:23
加载中...