1. 程式人生 > >Anniversary party(樹形DP入門)

Anniversary party(樹形DP入門)

Anniversary party

 HDU - 1520 

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings. 

InputEmployees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form: 
L K 
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 
0 0OutputOutput should contain the maximal sum of guests' ratings. 
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
題意:多組輸入,每組輸入的第一行一個N代表N個人,接下來N行每行一個數代表如果這個人蔘加party的歡樂值,接下來許多行,每行兩個數u,v 表示v是u的直接上屬。舉行一個party,如果邀請了這個人的直接上司就不能邀請這個人,要求最後總的歡樂值最大,問歡樂值最大為多少。
思路:
  開一個二維dp dp[x][0]代表沒有邀請x的最大歡樂值、
         dp[x][1]代表邀請了x的最大歡樂值。初始化dp[x][0]均為0 dp[x][1]為a[x]。dfs從樹根遍歷這棵樹。具體看程式碼
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn=6010;
 8 vector<int>v[maxn];
 9 int dp[maxn][2],in[maxn],x,y;
10 int n,vis[maxn];
11 int a[maxn];
12 void dfs(int t)
13 {
14     dp[t][0]=0;        //dp陣列的初始化。 
15     dp[t][1]=a[t];
16     vis[t]=1;        //vis陣列的意義如同記憶化搜尋,如果這個葉子節點已經同時是其他節點的葉子節點,那麼它已經被訪問過,dp值已經更新了,不用再往下搜,因為下面都已經被搜過了,直接拿來用就行。 
17     for(int i=0;i<v[t].size();i++)
18     {
19         int to=v[t][i];
20         if(vis[to])    //這個題資料比較水,如果不加vis陣列也能過,但是時間會長。 
21             continue;
22         dfs(to);
23         dp[t][0]+=max(dp[to][0],dp[to][1]);//如果不邀請t,那麼就可以邀請他的直系下屬 
24         dp[t][1]+=dp[to][0];               //如果邀請t,那麼久不能邀請他的直系下屬 
25     }
26     return ;
27 }
28 int main()
29 {
30     while(~scanf("%d",&n))
31     {
32         for(int i=1;i<=n;i++)
33             v[i].clear();
34         memset(in,0,sizeof(in));
35         memset(dp,0,sizeof(dp));
36         for(int i=1;i<=n;i++)
37         {
38             scanf("%d",&a[i]);
39         }
40         while(~scanf("%d%d",&x,&y)&&(x+y))
41         {
42             v[y].push_back(x);
43             in[x]++;
44         }
45         memset(vis,0,sizeof(vis));
46         for(int i=1;i<=n;i++)
47         {
48             if(!in[i])
49             {
50                 dfs(i);
51                 printf("%d\n",max(dp[i][0],dp[i][1]));
52                 break;
53             }
54         }
55     }
56 }