Anniversary party HDU 1520树形dp

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
#include <bits/stdc++.h>
using namespace std;
//typedef long long ll;
using ll = long long;
const ll inf = 4e18+10;
const int mod = 1000000007;
const int mx = 200010; //check the limits, dummy
typedef pair<int, int> pa;
const double PI = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define swa(a,b) a^=b^=a^=b
#define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) ((x)&(x-1))
#define mkp make_pair
void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
ll  m, n,y,z,k,sum=0,ans=0;
const int N = 6030;
int val[N],father[N];
ll dp[N][2];
int s, t;
vector<int>tree[N];
void dfs(int u) {
    dp[u][0] = 0;//初值:不参加
    dp[u][1] = val[u];//初值:参加
    re(i, 0, tree[u].size()) {//逐一处理这个父节点的每个子节点
        int son = tree[u][i];
        dfs(son);//深搜子节点
        dp[u][0] += max(dp[son][1], dp[son][0]);//父节点不选,子节点可选可不选
        dp[u][1] += dp[son][0];//父节点选,子节点不选
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while (cin>>n)
    {
        re(i, 1, n + 1) {
            cin >> val[i];
            tree[i].clear();
            father[i] = -1;//给个初值,还没建立关系
        }
        while (1)
        {
            int a, b;
            cin >> a >> b;
            if (a == 0 && b == 0)break;
            tree[b].push_back(a);//用邻接表建树
            father[a] = b;//父子关系
        }
        t = 1;
        while (father[y]!=-1)//查找树的根节点
        {
            t = father[t];
        }
        dfs(t);//从根节点开始,dfs遍历整个树
        cout << max(dp[t][1], dp[t][0]) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xxxsans/p/12715008.html