大佬麻烦看看为什么过不了
查看原帖
大佬麻烦看看为什么过不了
215872
yycQAQ楼主2023/11/26 21:24
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int v,nxt;
}E[10001];
int t,n;
int w[10001],h[10001],tot=0,f[10001][1];
void add(int u,int v){
	E[++tot].nxt=h[u];
	E[tot].v=v;
	
	h[u]=tot;
}
void dfs(int u){
	f[u][0]=0;
	f[u][1]=w[u];
	for(int i=h[u];i;i=E[i].nxt){
		int v=E[i].v;
		
		dfs(v);
		
		f[u][0]+=max(f[v][1],f[v][0]);
		f[u][1]+=f[v][0];
		
	}
	
}
int vis[10001];
int main(){
	cin>>n;
	memset(h,0,sizeof(h));
	for(int i=1;i<=n;i++){
		cin>>w[i];
	}
	for(int i=1;i<=n-1;i++){
		int u1,v1;
		cin>>v1>>u1;
		add(u1,v1);
		vis[v1]=1;
	}
	int root;
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			root=i;
			break;
		}
	}
	
	
	dfs(root);

	cout<<max(f[root][1],f[root][0])<<endl;
	return 0;
}
2023/11/26 21:24
加载中...