Anniversary party

来源:互联网 发布:java服务器空间 编辑:程序博客网 时间:2024/04/30 04:36
Problem DescriptionThere 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 Input711111111 32 36 47 44 53 50 0Sample Output5//题意:给出N个点,构成一棵树,每个点都有权值,求去没有直接亲属关系的点的权值,使它们的权值最大。//题解:树状DP,状态转移方程:dp[i][0] = max(dp[j][0],dp[j][1]), dp[i][1] += dp[j][0]; (j是i的子节点)dp[i][0]表示不去i点能够得到最大值,dp[i][1]表示取i点能够等到最大值。//标程:#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;vector<int> vec[6001];int dp[6005][2],vis[6001];void dfs(int x){    for(int i = 0; i < vec[x].size(); ++ i)    {        int y = vec[x][i];        dfs(y);        dp[x][1] += dp[y][0];        dp[x][0] += max(dp[y][0],dp[y][1]);    }}int main(){//    freopen("a.txt","r",stdin);    int n, i, j, a, b;    while(scanf("%d",&n)!=EOF)    {        memset(dp,0,sizeof(dp));        memset(vis,0,sizeof(vis));        for(i = 1; i <= n; ++ i)            vec[i].clear();        for(i = 1; i <= n; ++ i)            scanf("%d",&dp[i][1]);        while(scanf("%d%d",&a,&b)!=EOF)        {            if(a+b == 0) break;            vec[b].push_back(a);            vis[a] = 1;        }        for(i = 1; i <= n; ++ i)            if(vis[i] == 0)            {                dfs(i);                break;            }        cout << max(dp[i][0],dp[i][1]) << endl;    }    return 0;}

0 0
原创粉丝点击