标签:closed col clu poj imm mes employees from head
hdu 1520,poj2342
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12664 Accepted Submission(s): 5106
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define clr(x) memset(x,0,sizeof(x)) 6 #define clr_1(x) memset(x,-1,sizeof(x)) 7 using namespace std; 8 const int N=1e4+10; 9 struct edg 10 { 11 int next,to; 12 }edge[N*2]; 13 int head[N],ecnt; 14 int dp[N][2],value[N],fa[N]; 15 void addedge(int u,int v) 16 { 17 edge[++ecnt]=(edg){head[u],v}; 18 head[u]=ecnt; 19 return ; 20 } 21 void dfs(int u,int pre) 22 { 23 dp[u][0]=dp[u][1]=0; 24 for(int i=head[u];i!=-1;i=edge[i].next) 25 { 26 if(edge[i].to!=pre) 27 { 28 dfs(edge[i].to,u); 29 dp[u][0]+=max(dp[edge[i].to][1],dp[edge[i].to][0]); 30 dp[u][1]+=dp[edge[i].to][0]; 31 } 32 } 33 dp[u][1]+=value[u]; 34 return ; 35 } 36 int main() 37 { 38 int n,u,v,root,m; 39 while(scanf("%d",&n)!=EOF && n) 40 { 41 for(int i=1;i<=n;i++) 42 scanf("%d",&value[i]); 43 clr_1(head); 44 ecnt=0; 45 clr(fa); 46 while(scanf("%d%d",&u,&v) && u|v) 47 { 48 addedge(v,u); 49 addedge(u,v); 50 fa[u]=v; 51 } 52 for(int i=1;i<=n;i++) 53 if(fa[i]==0) 54 { 55 root=i; 56 break; 57 } 58 dfs(root,0); 59 printf("%d\n",max(dp[root][0],dp[root][1])); 60 } 61 return 0; 62 }
标签:closed col clu poj imm mes employees from head
原文地址:http://www.cnblogs.com/wujiechao/p/7571737.html