Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9361 Accepted Submission(s): 4016
Problem Description
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.
Input
Employees
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 0
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 0
Output
Output 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
为了解决清楚地建树方式,这道基础题写了快10遍了。。。。。。。。。。。
题意:
邀请公司的聚餐,但是每个人都不能和他的上司见面,每个人都有相应的价值,求邀请的最大的价值。
思路:
树形DP,用邻接表建立无向图,枚举每个点
一开始数组开小了,TLE了半天。
AC代码:
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace std; 5 const int MAX = 6010; 6 struct node 7 { 8 int is; 9 int next; 10 }; 11 node tree[MAX * 2]; 12 int head[MAX]; 13 int tol = 0; 14 15 int dp[MAX][2]; 16 int vis[MAX]; 17 int val[MAX]; 18 19 void add(int a, int b) 20 { 21 tree[tol].is = b; 22 tree[tol].next = head[a]; 23 head[a] = tol++; 24 25 tree[tol].is = a; 26 tree[tol].next = head[b]; 27 head[b] = tol++; 28 } 29 void dfs(int root) 30 { 31 if(vis[root]) 32 return; 33 vis[root] = 1; 34 int tep = head[root]; 35 dp[root][1] = val[root]; 36 37 while(tep != -1) 38 { 39 if(!vis[tree[tep].is]) 40 { 41 dfs(tree[tep].is); 42 dp[root][0] += max(dp[tree[tep].is][0], dp[tree[tep].is][1]); 43 dp[root][1] += dp[tree[tep].is][0]; 44 } 45 tep = tree[tep].next; 46 } 47 } 48 int main() 49 { 50 int n; 51 while(~scanf("%d", &n)) 52 { 53 memset(head, -1, sizeof(head)); 54 memset(dp, 0, sizeof(dp)); 55 memset(vis, 0, sizeof(vis)); 56 tol = 0; 57 for(int i = 0; i < MAX; i++) 58 { 59 tree[i].is = 0; 60 tree[i].next = -1; 61 } 62 for(int i = 1; i <= n; i++) 63 scanf("%d", &val[i]); 64 65 int a, b; 66 67 while(scanf("%d%d", &a, &b)) 68 { 69 if(a == 0 && b == 0) 70 break; 71 add(a, b); 72 } 73 dfs(1); 74 printf("%d ", max(dp[1][1], dp[1][0])); 75 } 76 return 0; 77 }
这是第二种方式,如果用memset初始化,就会超时,
View Code
View Code
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <vector> 5 using namespace std; 6 const int MAX = 6050; 7 vector <int> v[MAX]; 8 int f[MAX]; 9 int val[MAX]; 10 int dp[MAX][2]; 11 12 void dfs(int root) 13 { 14 dp[root][1] = val[root]; 15 for(int i = 0; i < v[root].size(); i++) 16 { 17 dfs(v[root][i]); 18 19 dp[root][0] += max(dp[v[root][i]][1], dp[v[root][i]][0]); 20 dp[root][1] += dp[v[root][i]][0]; 21 } 22 } 23 24 25 26 int main() 27 { 28 int n; 29 while(scanf("%d", &n) != EOF) 30 { 31 for(int i = 1; i <= n; i++) 32 { 33 scanf("%d", &val[i]); 34 f[i] = -1; 35 v[i].clear(); 36 dp[i][1] = dp[i][0] = 0; 37 } 38 int a, b; 39 while(scanf("%d%d", &a, &b)) 40 { 41 if(!a && !b) 42 break; 43 f[a] = b; 44 v[b].push_back(a); 45 } 46 int root = 1; 47 while(f[root] != -1) 48 root = f[root]; 49 dfs(root); 50 printf("%d ", max(dp[root][1], dp[root][0])); 51 } 52 53 54 return 0; 55 }
另外一种写的方式:
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace std; 5 const int MAX = 6050; 6 struct Node 7 { 8 int w; 9 int next; 10 }tree[MAX * 2]; 11 int head[MAX]; 12 int tol; 13 int val[MAX]; 14 15 16 int dp[MAX][2]; 17 18 void edge(int a, int b) 19 { 20 tree[tol].w = b; 21 tree[tol].next = head[a]; 22 head[a] = tol++; 23 24 tree[tol].w = a; 25 tree[tol].next = head[b]; 26 head[b] = tol++; 27 } 28 29 void dfs(int root, int f) 30 { 31 for(int i = head[root]; i != -1; i = tree[i].next) 32 { 33 int son = tree[i].w; 34 35 if(son == f) 36 continue; 37 dfs(son, root); 38 dp[root][1] += dp[son][0]; 39 dp[root][0] += max(dp[son][1], dp[son][0]); 40 } 41 } 42 43 44 int main() 45 { 46 int n; 47 while(scanf("%d", &n) != EOF) 48 { 49 memset(head, -1, sizeof(head)); 50 memset(dp, 0, sizeof(dp)); 51 52 for(int i = 1; i <= n; i++) 53 { 54 scanf("%d", &val[i]); 55 dp[i][1] = val[i]; 56 } 57 int a, b; 58 tol = 0; 59 while(scanf("%d%d", &a, &b)) 60 { 61 if(!a && !b) 62 break; 63 edge(a, b); 64 } 65 int root = 1; 66 dfs(root, -1); 67 printf("%d ", max(dp[root][1], dp[root][0])); 68 69 } 70 71 72 return 0; 73 }