AcWing 285 Prom without a boss (introductory tree dp)

**
Original title link

Title description

**Ural University has N employees, numbered 1~N.

Their relationship is like a tree rooted by the principal, and the parent node is the direct boss of the child node.

Each employee has a happiness index, which is given by an integer Hi, where 1≤i≤N.

Now there is an anniversary party, but no staff is willing to attend the meeting with their direct supervisors.

Under the premise that this condition is met, the organizer hopes to invite some staff to participate in the conference, so that the sum of the happiness index of all the staff participating in the conference is maximized, and find the maximum value.

Input format
An integer N in the first line.

Next N rows, the i-th row represents the happiness index Hi of employee i.

In the next line N-1, enter a pair of integers L and K in each line, indicating that K is L's direct boss.

Output format
Output the largest happiness index.

Data range
1≤N≤6000,
−128≤Hi≤127

Input example:
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
Output example :
5

Topic analysis

For this topic, the first thing we have to do is to build a map (build a tree), each point has a weight, but when selecting, our child nodes and parent nodes cannot be selected together. What is the maximum weight that can be selected?
For example, we can build such a tree. The

red area is the best choice.
We can set f (i) is the root node is the best choice when i, then found it impossible to transfer the state, because each father node has two states, election or no election, affect the child node is different
1 When choosing the parent node. Child nodes cannot be selected.
2 When the parent node is not selected. The byte point can be selected or not,
so here we add one dimension, f[i][1] and f[i][0] respectively indicate the case of selecting the parent node and not selecting the parent node

State transition
Insert picture description here

For a node i has two child nodes s1 and s2,

1If the parent node i is selected,
f[i][1]=f[s1][0]+f[s2][0]; 2If the parent
node i is not selected,
f[i][0]=max (f[s1][0],f[s1][1])+max(f[s2][0],f[s2][1])

We pay attention to n points in the title. n-1 has one edge, so it must be a tree.
I have adopted a method similar to the chain forward star to save the image
AcCode

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define  gold_m main
#define re register
#define Accept return 0;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf =0x3f3f3f3f;
const int maxn=1e6+5;
const int mod = 1e9+7;
const int N=666;
const long double PI=3.1415926535897932384626433832795;
const long double e=2.71828182845904523536028747135266;
typedef pair<int,int>PII;
inline ll read() {
    
    
	ll  x=0;
	bool f=0;
	char ch=getchar();
	while (ch<'0'||'9'<ch)	f|=ch=='-', ch=getchar();
	while ('0'<=ch && ch<='9')
		x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}
priority_queue<int,vector<int>, greater<int> >heap;
stack<int>st;
int  dp[maxn][2],cnt,root,head[maxn],h[maxn],n,l,k,rt[maxn];
struct wmy {
    
    
	ll u,v,w,next;
} a[maxn];
void add(ll u,ll v) {
    
    
	a[cnt].u=u;
	a[cnt].v=v;
//	a[cnt].w=w;
	a[cnt].next=head[u];
	head[u]=cnt++;
}
void dfs(int  u)
{
    
    
	dp[u][1] =h[u];
	
	for(int i=head[u];i!=-1;i=a[i].next)
	{
    
    
		ll v=a[i].v;
		dfs(v);
		dp[u][1]+=dp[v][0];
		dp[u][0]+=max(dp[v][0],dp[v][1]);
	}
	return ;
}
int   gold_m() {
    
    
	memset(head,-1,sizeof(head));
	n=read();
	for(int i=1 ; i<=n ; i++) h[i]=read();
	for(int i=1 ; i<n; i++) {
    
    
		cin>>l>>k; //有一条由k指向l的线段 
		add(k,l);
		rt[l]++;
	}
	for(int i=1 ; i<=n; i++) {
    
    
		if(rt[i]==0) {
    
    
			root=i;// 如果这个点没有父亲节点,那么这个点一定是根节点,校长节点 
			break;
		}
	}
	dfs(root);
	cout<<max(dp[root][0],dp[root][1]);
	Accept;
}
/*

*/

Guess you like

Origin blog.csdn.net/wmy0536/article/details/104445461