HDU1520

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

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

考虑i结点:

1.i结点参加舞会,则它的儿子结点都不能参加舞会

2.i结点不参加舞会,则它的儿子结点可以参加也可以不参加舞会

记录上述两种情况的最大值。

从根结点开始跑一次dfs,沿路更新状态。

(感觉bfs也可以,一层一层的更新,但就是WA,想到原因再来补上)

#include
#include
#include
#include
#include
#include
using namespace std;
const int INF = 1<<29;
const int MAX = 6005;
int N;
int rate[MAX];
vector parent[MAX];    //父结点指向子结点
vector son[MAX];   //子结点指向父结点
int visit[MAX];
int f1[MAX];    //f1[i]表示结点i参加舞会,i的子树的最优值
int f2[MAX];    //f2[i]表示结点i不参加舞会,i的子树的最优值
//假设v为u的儿子结点,则有
//f1[u] += f2[v] f2[u] += max(f1[v], f2[v])

void init()
{
    memset(f2, 0, sizeof(f2));
    memset(visit, 0, sizeof(visit));
    for (int i = 1; i <= N; i++)
    {
        parent[i].clear();
        son[i].clear();
    }
}

//从根结点往叶子结点dfs
void dfs(int i)
{
    f1[i] = rate[i];
    for (int k = 0; k < parent[i].size(); k++)
    {
        dfs(parent[i][k]);
        f1[i] = f1[i] + f2[parent[i][k]];
        f2[i] = f2[i] + max(f1[parent[i][k]], f2[parent[i][k]]);
    }
}

void bfs()
{
    for (int i = 1; i <= N; i++)
        f1[i] = rate[i];
    queue Q;
    //先把叶子结点入队列
    for (int i = 1; i <= N; i++)
    {
        if (parent[i].size() == 0)
        {
            Q.push(i);
            visit[i] = 1;
        }
    }

    while (!Q.empty())
    {
        int t = Q.front(); Q.pop();
        for (int i = 0; i < parent[t].size(); i++)
        {
            int s = parent[t][i];
            f1[t] = f1[t] + f2[s];   //t参加舞会的情况(其儿子结点不能参加舞会)
            f2[t] = f2[t] + max(f1[s], f2[s]);    //t不参加舞会(其儿子结点可以参加舞会,也可以不参加舞会)
        }
        //把t的父结点加入到队列中
        for (int i = 0; i < son[t].size(); i++)
        {
            if (visit[son[t][i]] == 0)
            {
                visit[son[t][i]] = 1;
                Q.push(son[t][i]);
            }
        }
    }
}

int main()
{
    while (cin >> N)
    {
        init();
        for (int i = 1; i <= N; i++)
            cin >> rate[i];
        int L, K;
        while ((cin >> L >> K) && L && K)
        {
            parent[K].push_back(L);
            son[L].push_back(K);
        }
        int root;
        int M = 0;
        for (int i = 1; i <= N; i++)
            if (son[i].size() == 0)
            {
                root = i;
                dfs(root);
            }
        
        M = max(f1[root], f2[root]);
        cout << M << endl;
    }

    return 0;
}

 

你可能感兴趣的:(区间DP和树形DP(8.3))