bzoj2060[usaco2010 nov]visiting Cows Visit cows *

Source: Internet
Author: User

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 *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.