原题链接
Anniversary party
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6833 Accepted: 3941
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 N – 1 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
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
Source
这个题目题意就是说很多人一起参加Party,但是某个领导参加了他的下属就不能再参加了,每个人参加的话都会有不同的欢乐值,问最大的欢乐值是多少
首先注意到一点,如果某个下属的直系领导参加了,那么他就是不能参加的,如果他的领导没有参加,那么他就可以参加也可以不参加
所以就可以从最大的领导开始建树,一层层向下DFS后找出自己去的和不去的最大欢乐值,状态转移方程为
dp[root][0]+=max(dp[v][1],dp[v][0]);
dp[root][1]+=dp[v][0];
//这个做法就是简单地建立一棵树然后向下DFS而已
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=6000 + 10;
int w[maxn],dp[maxn][2],f[maxn];//dp[i][1]代表i这个人去
vector<int> a[maxn];//用来存储与i有关系的人
void dfs(int root){
dp[root][1]=w[root];
dp[root][0]=0;
int len=a[root].size();
for(int i=0;i<len;i++){
int v=a[root][i];
dfs(v);
dp[root][0]+=max(dp[v][1],dp[v][0]);
dp[root][1]+=dp[v][0];
}
return;
}
int main(){
int n,p,q,root;
while(scanf("%d",&n)==1){
memset(dp,0,sizeof(dp));
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
a[i].clear();
}
while(scanf("%d%d",&p,&q)==2 && p+q){
a[q].push_back(p);
f[p]=1;
}
for(int i=1;i<=n;i++)
if(f[i]==0){root=i;break;}
dfs(root);
printf("%d\n",max(dp[root][1],dp[root][0]));
}
return 0;
}
//下面这个写法是把树上的某一个结点当作起点来计算,这样其实完全可以,而且这种转化可能在未来也会有很大的用武之地
/*
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=6000 + 10;
int w[maxn],dp[maxn][2],vis[maxn];//dp[i][1]代表i这个人去,与上面不一样的是这里不能保证向下时他的儿子就一定是他的下属,但是由于下属参加了领导就不能参加,所以这个道理还是一样的
vector<int> a[maxn];//用来存储与i有关系的人
void dfs(int root){
dp[root][1]=w[root];
dp[root][0]=0;
vis[root]=1;
int len=a[root].size();
for(int i=0;i<len;i++){
int v=a[root][i];
if(vis[v]) continue;
dfs(v);
dp[root][0]+=max(dp[v][1],dp[v][0]);
dp[root][1]+=dp[v][0];
}
return;
}
int main(){
int n,p,q;
while(scanf("%d",&n)==1){
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
a[i].clear();
}
while(scanf("%d%d",&p,&q)==2 && p+q){
a[q].push_back(p);
a[p].push_back(q);
}
dfs(1);
printf("%d\n",max(dp[1][1],dp[1][0]));
}
return 0;
}
*/