题目链接

solution

板子题,从最大的下手找

#include<bits/stdc++.h>
using namespace std;
const int N = 16500;
int s[N],n;
struct node{
	int next,to;
}edge[N<<1];
int head[N],cnt;
inline void add(int from,int to) {
	edge[++cnt].to=to;edge[cnt].next=head[from];
	head[from]=cnt;
}
void dfs(int u,int f) {
	for(int i=head[u];i;i=edge[i].next) {
		int v=edge[i].to;
		if(v==f) continue;
		dfs(v,u);
		s[u]+=max(s[v],0);
	}
}
int main() {
	cin>>n;
	int root=0;s[root]=-1;
	for(int i=1;i<=n;i++) {cin>>s[i];if(s[i]>s[root]) root=i;}
	for(int i=1;i<n;i++) {
		int a,b;cin>>a>>b;
		add(a,b),add(b,a);
	}
	dfs(root,root);
	cout<<s[root];
	return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2021-12-09
  • 2021-12-25
  • 2022-01-10
  • 2021-11-27
猜你喜欢
  • 2022-01-06
  • 2022-03-08
  • 2021-06-01
  • 2021-07-22
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案