标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
思路:两个状态;
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define esp 0.00000000001 const int N=2e5+10,M=1e6+10,inf=1e9; int a[N]; int du[N]; vector<int>v[N]; int dp[N][3]; void dfs(int x,int step) { for(int i=0;i<v[x].size();i++) { dfs(v[x][i],step+1); dp[x][0]+=max(dp[v[x][i]][0],dp[v[x][i]][1]); dp[x][1]+=dp[v[x][i]][0]; } } int main() { int x,y,z,i,t; while(~scanf("%d",&x)) { memset(dp,0,sizeof(dp)); memset(du,0,sizeof(du)); for(i=0;i<=x;i++) v[i].clear(); for(i=1;i<=x;i++) scanf("%d",&a[i]),dp[i][1]=a[i]; while(1) { scanf("%d%d",&y,&z); if(y==0&&z==0) break; du[y]++; v[z].push_back(y); } for(i=1;i<=x;i++) if(du[i]==0) { dfs(i,1); break; } printf("%d\n",max(dp[i][1],dp[i][0])); } return 0; }
hdu 1520 Anniversary party || codevs 1380 树形dp
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5671540.html