题意很简单,就是顶头上司不能和下属一起去 Party
然后每个人都有一个价值,取价值最优的参加 Party 的方案
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
const int MAX = 1e5;
vector<int> mp[MAX];
int val[MAX], n, m;
int dp[MAX][2], vis[MAX];
void init(){
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; ++i)
mp[i].clear();
}
void dp_func(int rt){
if(vis[rt])
return;
vis[rt] = 1;
int sz = mp[rt].size();
for(int i = 0; i < sz; ++i){
int now = mp[rt][i];
if(!vis[now]){
dp_func(now);
dp[rt][1] += dp[now][0];
dp[rt][0] += max(dp[now][1], dp[now][0]);
}
}
dp[rt][1] += val[rt];
}
int main(){
int s, t;
while(~scanf("%d", &n)){
init();
for(int i = 1; i <= n; ++i)
scanf("%d", val + i);
while(scanf("%d%d", &s, &t) && s + t){
mp[s].push_back(t);
mp[t].push_back(s);
}
dp_func(1);
printf("%d\n", max(dp[1][0], dp[1][1]));
}
return 0;
}