ASC(1)C(树形DP)

New Year Bonus Grant

Special Judge Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Problem Description
      All programmers of Mocrosoft software company are organized in a strict subordination hierarchy. Every programmer has exactly one chief, except Bill Hates who is also the head of the company and has no chief.
      Due to the celebration of the new 2003 year, chief accountant of Mocrosoft decided to pay a New Year Bonus Grant of 1000 dollars to some programmers. However being extremely concerned of the company wealth she would like to designate the least possible amount of money for these grants. On the other hand she didn’t want to be accused of being too greedy or of giving preferences to some programmers. To do this, she developed the following scheme of grants appointment:
• Each programmer may either assign a grant to one of his subordinates or have a grant assigned to him by his chief or none of the above.
• No programmer can simultaneously receive a grant and assign a grant to one of his subordinates.
• No programmer can assign a grant to more than one of his subordinates
      The scheme seemed to be designed perfectly — nobody would like to assign a grant to anybody since in this case he himself would not receive money. But programmers somehow discovered the plan of chief accountant and decided to make a trick to get the most money possible and share them fairly afterwards. The idea was to make such grant assignments that the total amount of grant money received is maximum possible.
      You were selected to write the program which will find the optimal grants appointment.
Input
      The first line of the input file contains integer N — the number of programmers in Mocrosoft company (2 ≤ N ≤ 500 000). Each programmer is assigned his unique identifier — integer number ranging from 1 to N . Bill Hates has number 1 and each programmer has the number greater then the number of his chief. The second line of the input file contains N − 1 integers, i-th of which being the number of the chief of the worker whose number is (i + 1).
Output
      On the first line of the output file print the maximum possible amount of money workers can get. On the second line output the numbers of programmers that will receive grant in ascending order.
Sample Input
4
1 1 2
Sample Output
2000
3 4

题意:给了一棵树,求出最大的匹配,每个点要么与父亲匹配,要么与一个孩子匹配,要么不匹配

思路:n=500000,二分图匹配算法显然不能做

            因为是一棵树,果断可以树形DP

           dp[u][0]表示该点不与父亲匹配,其子树的最大匹配数

           dp[u][1]表示该点与其父亲匹配,其子树的最大匹配数

           转移很容易想到,dp[u][1]只能由dp[v][0]累加得到,其中v为u的孩子,因为u与其父亲匹配以后就不能再与任何孩子匹配

          dp[u][0]可以初始化为dp[u][1]表示u即不与父亲匹配也不与孩子匹配

          然后再用dp[v][1]去最大化dp[u][0]

          最后还要求输出具体方案,直接按树的层次打印路径即可

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; const int MAXN = 500010; int head[MAXN]; int next[MAXN<<1]; int edge[MAXN<<1]; int son[MAXN]; int esz; void addedge(int u,int v) { edge[esz]=v; next[esz]=head[u]; head[u]=esz++; } int dp[MAXN][2]; int q[MAXN]; int vis[MAXN]; int p[MAXN]; void bfs() { int rear=0,front=0; p[1]=0; q[rear++]=1; while(front 
         
           =0;i--){ int v=q[i]; int fa=p[v]; dp[fa][0]+=dp[v][0]; dp[fa][1]+=dp[v][0]; if(dp[fa][0] 
          
            =dp[1][1])bfs2(0); else bfs2(1); if(sz>0)sort(stk,stk+sz); for(int i=0;i 
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值