标签:test rip sar mis ++ turn void contains logs
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
#include <iostream> #include<cstdio> #include<vector> #include<algorithm > using namespace std; bool vis[6005]; int tree[6005][2],w[6005],fa[6005]; vector<int> mp[6005]; int n; void dfs(int root) { vis[root]=1; tree[root][1]=w[root]; for(int i=0;i<mp[root].size();i++) { dfs(mp[root][i]); tree[root][0]+=max(tree[mp[root][i]][0],tree[mp[root][i]][1]); tree[root][1]+=tree[mp[root][i]][0]; } } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1;i<=n;i++) { fa[i]=0; vis[i]=0; tree[i][0]=0; tree[i][1]=0; mp[i].clear(); } int a,b; while(scanf("%d%d",&a,&b)) { if (a==0 && b==0) break; mp[b].push_back(a); fa[a]=b; } for(int i=1;i<=n;i++) if (fa[i]==0) { dfs(i); printf("%d\n",max(tree[i][0],tree[i][1])); break; } } /* for(int i=1;i<=n;i++) printf("%d %d\n",tree[i][0],tree[i][1]);*/ return 0; }
HDU 1520 Anniversary party(树形DP)
标签:test rip sar mis ++ turn void contains logs
原文地址:http://www.cnblogs.com/stepping/p/6391514.html