Time Limit: 1000MS Memory Limit: 65536K
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
树形dp真.第一题。
经典树形dp,题意就略过了。树形dp通过这道题给我的第一印象是dp + dfs,这道题是标准的入门题,状态转移很简单:
dp[u][1] += dp[v][0];
dp[u][0] += max(dp[v][0], dp[v][1]);
其中dp[u][0]为第u个人不参加可得的最大值,dp[u][0]为第u个人参加可得的最大值。v是u的下属(表现在书上即为u的子节点)。
其他的实现细节我觉得看代码和下面的截图懂得更快。
日常犯蠢记录:
原本是想要对照题解好好学习树形dp的大致框架写法,谁曾想被自己带跑偏了。博主参考的代码有一些小trick,但原博主完全没有解释(具体参考数组版代码)!结果博主一开始用的vector建图,用于记录每个父节点的子节点,并把vector放在全局位置,总是re,后来把vector放进main函数以后又变成wa,但当时实在是想不出来是为什么。
最后实在没办法,把vector改成了“记录每个子节点的父节点”的数组才过(就是数组版的ac代码啦)。
疑惑之余,又用vector改回来,然后发现输出变成了0…
直觉告诉我问题就在这里……按照博主代码的写法,直接把最后一个fa传入dfs的实际值为0,由于只有father[5]的值是0(即5没有上级),第二次递归时就会回到正常运算。但是vector的话就会炸……
博主太蠢了Orz
迅速改掉,果然过了(vector版本)。
father数组版本:
#include
#include
#include
#include
#include
#include
#include
#define M 6005
#define INF 0x3f3f3f3f
using namespace std;
int dp[M][2], father[M], N;
bool vis[M];
void dfs(int u)
{
vis[u] = false;
for(int i = 1; i <= N; i++)
{
if(vis[i] && father[i] == u)
{
dfs(i);
dp[u][0] += max(dp[i][0], dp[i][1]);
dp[u][1] += dp[i][0];
}
}
}
int main()
{
while(~scanf("%d", &N))
{
memset(vis, true, sizeof(vis));
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= N; i++)
{
scanf("%d", &dp[i][1]);
}
int son, fa;
while(scanf("%d %d", &son, &fa) && (son + fa))
{
father[son] = fa;
}
dfs(fa);//等同于dfs(0),trick就在这里
printf("%d\n", max(dp[fa][1], dp[fa][0]));
}
return 0;
}
运行结果:
vector版本:
include
#include
#include
#include
#include
#include
#include
#define M 6005
#define INF 0x3f3f3f3f
using namespace std;
int dp[M][2];//vis也去掉了
void dfs(int u, vector<int> *super)
{
for(int i = 0; i < super[u].size(); i++)
{
int v = super[u][i];
dfs(v, super);
dp[u][0] += max(dp[v][0], dp[v][1]);
dp[u][1] += dp[v][0];
}
}
int main()
{
int N;
while(~scanf("%d", &N))
{
vector<int> super[M];
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= N; i++)
{
scanf("%d", &dp[i][1]);
}
int son, fa, rt = 1;
while(scanf("%d %d", &son, &fa) && (son + fa))
{
super[fa].push_back(son);
if(rt == son)//做了修改
{
rt = fa;
}
}
dfs(rt, super);
printf("%d\n", max(dp[rt][1], dp[rt][0]));
}
return 0;
}
运行结果:
论hdu就是比你快系列: