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
题解:这题可能是多组数据,wa的想吐。。。可以看作是求树的最大独立集,固定套路。不过注意这颗树的边是有方向的,所以要找它的根,开始用并查集,后来发现其实用不上
1 #include<vector> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxn=6005; 9 10 int n,ans; 11 int Fa[maxn],dp[maxn][2]; 12 13 vector<int> G[maxn]; 14 15 void DFS(int pa,int u){ 16 for(int i=0;i<G[u].size();i++){ 17 int v=G[u][i]; 18 if(pa==v) continue; 19 DFS(u,v); 20 dp[u][0]+=max(dp[v][1],dp[v][0]); 21 dp[u][1]+=dp[v][0]; 22 } 23 } 24 25 int main() 26 { while(~scanf("%d",&n)){ 27 for(int i=1;i<=n;i++){ 28 Fa[i]=0; 29 dp[i][0]=0; 30 G[i].clear(); 31 } 32 for(int i=1;i<=n;i++) scanf("%d",&dp[i][1]); 33 for(int i=1;i<=n;i++){ 34 int u,v; 35 scanf("%d%d",&u,&v); 36 if(u==0&&v==0) break; 37 Fa[u]=v; 38 G[v].push_back(u); 39 } 40 int temp=n; 41 while(Fa[temp]) temp=Fa[temp]; 42 43 DFS(0,temp); 44 cout<<max(dp[temp][0],dp[temp][1])<<endl; 45 } 46 }