好久没更新博客了,其实也没什么可以更新。。。但是既然做过了,就写下了,不为其他,只为纪念
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1825 Accepted Submission(s): 797
Problem 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 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
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.
这题是TreeDP的入门题,没什么可说的 dp[i][j] 表示第I个点 J=1 为取。。。J=0为不取时可取得的最大值。
/*
* =====================================================================================
*
* Filename: HDU_P1520.cpp
*
* Description:
*
* Version: 1.0
* Created: 2012年08月02日 09时05分50秒
* Revision: none
* Compiler: gcc
*
* Author: Mad13 (),
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#define Mid(a) ((a)>>1)
#define R(a) (((a)<<1)+1)
#define L(a) ((a)<<1)
#define maxn 7000
#define MAXX 1000000
#define Clear(a,b) memset(a,b,sizeof(a))
using namespace std;
int Max(int a,int b)
{
if (a > b) return a;
return b;
}
int Min(int a,int b)
{
if (a > b) return b;
return a;
}
vector <int> son[maxn];
int fa[maxn],val[maxn],dp[maxn][2];
int Tdp(int k,int mark)
{
int j,sum = 0,m;
if (dp[k][mark] != MAXX) return dp[k][mark];
if (mark == 1) {
for(j = 0,m = son[k].size();j < m;j++)
sum += Tdp(son[k][j],0);
sum += val[k];
}
else {
for(j = 0,m = son[k].size();j < m;j++)
sum += Max(Tdp(son[k][j],0),Tdp(son[k][j],1));
}
return dp[k][mark] = sum;
}
int work()
{
int i,a,b,n;;
Clear(fa,0);Clear(val,0);
for(i = 0;i < maxn;i++){
son[i].clear();
dp[i][0] = dp[i][1] = MAXX;
}
if (scanf("%d",&n) == EOF) return 0;
for(i = 1;i <= n;i++) scanf("%d",&val[i]);
while(scanf("%d%d",&a,&b),a != 0 && b != 0) {
son[b].push_back(a);
fa[a] = b;
}
for(i = 1;i <= n;i++) {
if (fa[i] == 0) son[0].push_back(i);
}
printf("%d\n",Tdp(0,0));
return 1;
}
int main()
{
while(work());
return 0;
}