POJ 2342 && HDU 1520 Tree DP

Source: Internet
Author: User

Test instructions: There are n people, the next n line is the value of N, and then the next n line gives L,k said that L is the Boss is K, here note l and K can not appear at the same time

Link: Point Me

DP[I][1] + = dp[j][0],

DP I [0] + = max{dp[j][0],dp[j][1]}, where j is the child node of I.

The second time, it feels like water.

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; AtypedefLong Longll; - #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); the Const intmaxn=6010; - intN,m,tt; - intAA[MAXN]; - inttot,head[maxn],dp[maxn][2],inch[MAXN]; + structEdge - { +     intTo,next; A }EDGE[MAXN]; at voidAddedge (intUintv) - { -edge[tot].to=v; -edge[tot].next=Head[u]; -head[u]=tot++; - } in intDfsintu) - { to      for(inti=head[u];i!=-1; i=edge[i].next) +     { -         intv=edge[i].to; the Dfs (v); *dp[u][1]+=dp[v][0]; $dp[u][0]+=max (dp[v][0],dp[v][1]);Panax Notoginseng     } -     returnMax (dp[u][0],dp[u][1]); the } + voidInit () A { thetot=0; +memset (head,-1,sizeof(head)); - CL (DP); $Clinch); $ } - intMain () - { the     inti,j,k; - #ifndef Online_judgeWuyiFreopen ("1.in","R", stdin); the     #endif -      while(SCANF ("%d", &n)! =EOF) Wu     { - init (); About          for(i=1; i<=n;i++) $scanf"%d", &dp[i][1]); -         intb; -          while(SCANF ("%d%d",&a,&b)) -         { A             if(a==0&&b==0) Break; + Addedge (b,a); the             inch[a]++; -         } $         intans=0; the          for(i=1; i<=n;i++)//The title means a tree the         { the             if(!inch[i]) ans=DFS (i); the         } -printf"%d\n", ans); in     } the}

POJ 2342 && HDU 1520 Tree DP

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.