J - Anniversary Party(30) URAL - 1039
Background
The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.
Problem
Your task is to make up a guest list with the maximal conviviality rating of the guests.
Input
The first line of the input contains a number N. 1 ≤ N ≤ 6000. 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 the supervisor relation tree goes. Each line of the tree specification has the form
<L> <K>
which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
0 0
Output
The output should contain the maximal total rating of the guests.
Example
input output
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn=6005;
vector<int> mq[maxn];
int dp[maxn][2];
void dfs(int cur,int p)
{
int i,nex;
for(i=0; i<mq[cur].size(); i++)
{
nex=mq[cur][i];
if(nex==p)
{
continue;
}
dfs(nex,cur); //先找从cur这个点可以扩展的dp
dp[cur][0]+=max(dp[nex][0],dp[nex][1]);
dp[cur][1]+=dp[nex][0];
}
}
int main()
{
int n,i;
int a,b;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(i=1; i<=n; i++)
{
mq[i].clear();
scanf("%d",&dp[i][1]);
}
while(scanf("%d%d",&a,&b)&&(a+b))
{
mq[a].push_back(b);
mq[b].push_back(a);
}
dfs(1,0);
int ans=max(dp[1][0],dp[1][1]);
printf("%d\n",ans);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<map>
#include<math.h>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int pre[6010];
vector<int> G[6010]; // 动态存储邻接矩阵
int dp[6010][2], vis[6010], n;
void dfs(int u, int father)
{
int d = G[u].size();
for(int i = 0; i < d; i++)
{
int v = G[u][i];
if(v != father)
{
dfs(v, pre[v] = u);
}
}
}
void d(int t)
{
vis[t] = 1;// 标记已被访问
for(int i = 1; i <= n; i++)
{
if(pre[i] == t && !vis[i])
{
d(i);//当遍历到叶子,求最优解
dp[t][0] += max(dp[i][0], dp[i][1]);
dp[t][1] += dp[i][0];
}
}
}
int main()
{
int i, u, v;
while(scanf("%d",&n) != EOF)
{
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n; i++)
{
scanf("%d",&dp[i][1]);
}
while(scanf("%d%d",&u,&v) && (u + v))
{
G[u].push_back(v);
G[v].push_back(u);
}
memset(pre, -1, sizeof(pre));//设置初始都指向-1位父亲结点
dfs(1,-1);
memset(vis, 0, sizeof(vis));
d(1);
printf("%d\n",max(dp[1][0], dp[1][1]));
}
return 0;
}
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define N 6005
int dp[N][2], val[N], visit[N];
int in[N];
struct node
{
int v;
int next;
} pe[N<<1];
int e, head[N];
void init ()
{
memset (head, -1, sizeof (head));
e = 0;
}
void addedge (int u, int v)
{
pe[e].v = v;
pe[e].next = head[u];
head[u] = e ++;
}
int dfs (int u)
{
visit[u] = 1;
dp[u][0] = 0;
dp[u][1] = val[u];
for (int i = head[u]; i != -1; i = pe[i].next)
{
int v = pe[i].v;
dfs(v);
dp[u][0] += max(dp[v][0], dp[v][1]);
dp[u][1] += dp[v][0];
}
return max(dp[u][0], dp[u][1]);
}
int main()
{
int n, v, u;
while (scanf ("%d", &n) != EOF)
{
init();
for (int i = 1; i <= n; ++i)
{
in[i] = 0;
visit[i] = 0;
scanf ("%d", &val[i]);
}
while (scanf ("%d%d", &v, &u) && v + u != 0)
{
addedge (u, v);
in[v]++;
}
int ans = 0;
for (int i = 1; i <= n; ++i)
{
if(!in[i])
{
ans += dfs(i);
}
}
printf("%d\n", ans);
}
return 0;
}