TYVJ p1052 没有上司的舞会//树状DP

     
 
From Admin
没有上司的舞会
 
     
     
  描述 Description  
  Ural大学有N个职员,编号为1~N。他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。每个职员有一个快乐指数。现在有个周年庆宴会,要求与会职员的快乐指数最大。但是,没有职员愿和直接上司一起与会。
     
     
  输入格式 Input Format  
  第一行一个整数N。(1<=N<=6000)
接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)
接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。
最后一行输入0,0。
     
     
  输出格式 Output Format  
  输出最大的快乐指数。
     
     
  样例输入 Sample Input  
  7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
     
     
  样例输出 Sample Output  
  5
     
     
  时间限制 Time Limitation  
  各个测试点1s
     
     
 
Flag
  Accepted
题号
  P1052
类型(?)
  动态规划
通过
  213人
提交
  424次
通过率
  50%
难度
  2
 
     
     
 
提交 讨论 题解 数据
 
     

 

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct EDGE { int v,next; }edge[6010]; int c[6005]; bool in[6005]; int f[6005][2]; int cnt; int head[6010]; void addedge(int u,int v) { edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void dp(int u) { f[u][1]=c[u]; f[u][0]=0; for(int p=head[u];p!=-1;p=edge[p].next) { int v=edge[p].v; dp(v); f[u][1]=max(f[u][1],f[v][0]+f[u][1]); f[u][0]=f[u][0]+max(f[v][1],f[v][0]); } } int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&c[i]); cnt=0; memset(in,false,sizeof(in)); memset(head,-1,sizeof(head)); int x,y; for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); in[x]=true; addedge(y,x); } scanf("%d%d",&x,&y); memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) if(!in[i]) { dp(i); printf("%d/n",f[i][0]>f[i][1]?f[i][0]:f[i][1]); break; } return 0; }  

你可能感兴趣的:(c,测试,ini,input,output)