Effect:
You know the king likes to be busy, so you want to invite as many people as possible, but as a direct relationship between the two people directly in the party will appear very embarrassed, so can not also please have the relationship between the two people.
The king is the host of the party and the supreme leader of the kingdom, so must be present.
In order to be better prepared for the party, you need to know how many guests can be invited to the banquet.
Analysis:
Tree-shaped DP
Dp[x][0] represents the optimal solution for X and its subtree in the case of the X is not selected
Dp[x][1] represents the optimal solution of X and its subtree in the case of X
Then dp[x][0] = SUM (max (dp[y][0], dp[y][1]))
DP[X][1] = SUM (dp[y][0]) + 1
As the boss must be present, the final result remains in dp[boss][1].
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAXN =1005;7 intN, M;8 9 structNode {Ten intto, next; One}E[MAXN *MAXN]; A intHEAD[MAXN]; - inttot; - voidAddintUintv) { theE[tot].to =v; -E[tot].next =Head[u]; -Head[u] = tot++; - } + intH[MAXN], T[MAXN]; - intboss; + A intdp[maxn][2]; at - voidDfsintu) { - if(H[u] = =0) { -dp[u][0] =0; -dp[u][1] =1; - return ; in } - intSUM1 =0;intsum2 =0; to for(inti = Head[u]; I i =E[i].next) { + intv =e[i].to; - Dfs (v); theSum1 + = dp[v][0]; *sum2 + = max (dp[v][0], dp[v][1]); $ }Panax Notoginsengdp[u][1] = sum1 +1; -dp[u][0] =sum2; the } + A intDP () { theMemset (DP,0,sizeof(DP)); + DFS (boss); - returndp[boss][1]; $ } $ - intMain () { - intu, v; the while(EOF! = scanf ("%d%d", &n, &m)) { -tot =1;WuyiMemset (Head,0,sizeof(head)); thememset (H,0,sizeof(H)); -memset (T,0,sizeof(T)); Wu for(inti =1; I <= m; i++) { -scanf"%d%d", &u, &v); About Add (U, v); $h[u]++; t[v]++; - } - for(inti =1; I <= N; i++) { - if(T[i] = =0) { ABoss =i; + Break; the } - } $printf"%d\n", DP ()); the } the return 0; the}
View Code
HLG1475 King's Banquet "tree-shaped DP"