标签:title mod 关系 hiera with end similar void sizeof
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12770 Accepted Submission(s): 5142
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<bitset> using namespace std; #define PI acos(-1.0) #define eps 1e-8 typedef long long ll; typedef pair<int,int> P; const int N=1e5+100,M=1e5+100; const int inf=0x3f3f3f3f; const ll INF=1e18+7,mod=1e9+7; struct edge { int from,to; int next; }; edge es[M]; int cnt,head[N]; int in[N]; int dp[N][2]; void init() { cnt=0; memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); memset(dp,0,sizeof(dp)); } void addedge(int u,int v) { cnt++; es[cnt].from=u,es[cnt].to=v; es[cnt].next=head[u]; head[u]=cnt; } void dfs(int u) { for(int i=head[u]; i!=-1; i=es[i].next) { int v=es[i].to; dfs(v); dp[u][1]+=dp[v][0]; dp[u][0]+=max(dp[v][1],dp[v][0]); } ///cout<<u<<" * "<<dp[u][0]<<" * "<<dp[u][1]<<endl; } int main() { int n; while(~scanf("%d",&n)) { init(); for(int i=1; i<=n; i++) scanf("%d",&dp[i][1]); int u,v; while(scanf("%d%d",&u,&v)&&!(u==0&&v==0)) { addedge(v,u); in[u]++; } int root; for(int i=1; i<=n; i++) if(!in[i]) root=i; dfs(root); printf("%d\n",max(dp[root][0],dp[root][1])); } return 0; }
HDU 1520.Anniversary party 基础的树形dp
标签:title mod 关系 hiera with end similar void sizeof
原文地址:http://www.cnblogs.com/GeekZRF/p/7609044.html