bzoj2060[usaco2010 nov]visiting Cows Visit cows
Test instructions
To a tree, asking if a node can not be taken adjacent to its node, ask the most desirable number of nodes. The size of the tree is ≤50000.
Exercises
Tree-shaped DP. Make f[i][0] Do not take the I node, f[i][1] for the i node, the equation is F[i][0]=sum (max (f[j][0],f[j][1]+1)), F[i][1]=sum (F[j][0]), J is the child node of I. The final answer is Max (f[1][0],f[1][1]+1). Be careful not to miss that "+1".
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 500106 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} One while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); A returnf*x; - } - structe{intT,n;} es[maxn*2];intg[maxn],ess,n,dp[maxn][2]; the voidPeintFintT) {es[++ess]= (e) {t,g[f]}; g[f]=ess; es[++ess]= (e) {f,g[t]}; g[t]=ess;} - voidDfsintXintFA) { -dp[x][0]=dp[x][1]=0; - for(intI=G[X];I;I=ES[I].N)if(es[i].t!=FA) { +DFS (ES[I].T,X); dp[x][0]+=max (dp[es[i].t][1]+1, dp[es[i].t][0]); dp[x][1]+=dp[es[i].t][0]; - } + } A intMain () { atN=read (); Inc (I,1, N-1){intA=read (), B=read (); PE (A, b);} Dfs1,0); -printf"%d", Max (dp[1][0],dp[1][1]+1));return 0; -}
20160907
bzoj2060[usaco2010 nov]visiting Cows Visit cows *