POJ2342 Anniversary party

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2342 Anniversary party相关的知识,希望对你有一定的参考价值。

题解:

树形dp求最大独立集裸题

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))  
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 
#define INF 2097152
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=6010;
const int mod=1e9+7;

int h[maxn],not_boss[maxn],sum;
int DP[maxn][2];
vector<int> Edge[maxn];

int dp(int u,int j){
    int& ans=DP[u][j];
    if(ans>=0) return ans;
    ans=0;
    if(j==1) ans+=h[u];
    for(int i=0;i<Edge[u].size();i++){
        if(j) ans+=dp(Edge[u][i],0);
        else  ans+=max(dp(Edge[u][i],0),dp(Edge[u][i],1));
    }
    return ans;
}

int main(){
    int n,u,v,boss;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&h[i]);
    for(int i=1;i<=n-1;i++){
        scanf("%d%d",&v,&u);
        Edge[u].pb(v);
        not_boss[v]=1;
    }
    scanf("%d %d",&v,&u);
    for(int i=1;i<=n;i++){
        if(!not_boss[i]){
            boss=i;
            break;
        }
    }
    memset(DP,-1,sizeof(DP));
    sum=dp(boss,0);
    memset(DP,-1,sizeof(DP));
    sum=max(sum,dp(boss,1));
    printf("%d\n",sum);
}

 

以上是关于POJ2342 Anniversary party的主要内容,如果未能解决你的问题,请参考以下文章

poj2342 Anniversary party

poj2342 Anniversary party (树形dp)

POJ 2342 Anniversary party

[POJ2342]Anniversary party

POJ 2342 Anniversary party(树形dp)

POJ2342 Anniversary party