hdu 1520 Anniversary party 【树形dp】

本文介绍了一种使用树形动态规划方法解决派对邀请问题的方法,旨在最大化派对参与者之间的愉悦度总和,确保没有直接上下级关系的员工同时出席。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:please click here!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18018    Accepted Submission(s): 6768


 

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

 

 

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。开始就仔细写全一点,留个好印象(给自己)!

先上一下自己的代码:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 1e4+10;
const int inf = 0x3f3f3f3f;
vector<int>G[maxn];
int dp[maxn][2],n; //dp[][]数组第二维大小开二就可以了,0代表不选,1代表选,
//vis数组是用来做记录,father[i]数组是记录i的前驱结点
int a[maxn],vis[maxn],father[maxn];
void dfs(int node)  //从根节点开始依次遍历到子叶节点
{
    vis[node] = 1;
    for(int i=0;i<G[node].size();i++)
    {
        if(!vis[G[node][i]])
        {
            dfs(G[node][i]);  //符合说明未到子叶节点,继续调用dfs
            //回溯的时候对于每种结果有下面两种选择
            dp[node][0] += max(dp[G[node][i]][1],dp[G[node][i]][0]);//dp[][0]代表父亲节点没选,其子节点可选可不选
            dp[node][1] += dp[G[node][i]][0]; //dp[][1]代表父亲节点选了,其子节点不可选
        }
    }
}
int main()
{
    ios::sync_with_stdio(false); //关闭缓冲区,避免由于c++输入输出造成的时间增加
    while(cin>>n && n)
    {
        memset(vis,0,sizeof vis);
        memset(dp,0,sizeof dp);
        memset(father,0,sizeof father);
        for(int i=1;i<=n;i++)
            G[i].clear();  //G[i]数组清空
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            dp[i][1] = a[i]; //这一步的初始化很重要,表示该节点要是选的话值就为输入的a[i]
        int code,forces;
        while(cin>>code>>forces && (code||forces))  //输入
        {
            father[code] = forces;  //code的前驱结点是forces
            G[forces].push_back(code); //code是从forces出发的一条边
        }
        int root  = 1;
        //下面是递归找根节点
        while(father[root])
            root = father[root];
        dfs(root); //从根节点开始搜索
        cout<<max(dp[root][0],dp[root][1])<<endl; //对于根节点有两种情况,选和不选,取他们两者的最大值
    }
    return 0;
}

 第二种风格的代码(讲道理,自己都觉得怪怪的,不过还是AC了)

#include<bits/stdc++.h>
using namespace std;
#define ms(a) memset(a,0,sizeof(a))
const int maxn = 1e4;
int pre[maxn],dp[maxn][2],zjj[maxn];
vector<int>son[maxn];
int dfs (int x)
{
    if(dp[x][1])  //记忆化,防止之后回溯的时候出现相同的x时重复计算 
       return max(dp[x][1],dp[x][0]);
    dp[x][1] = zjj[x];  //选的时候该节点的愉悦度为自身 
    dp[x][0] = 0;   //不选时为0 
    for(int i = 0;i < son[x].size();i++)
    {
    	dfs(son[x][i]);
    	dp[x][1] += dp[son[x][i]][0];
    	dp[x][0] += max(dp[son[x][i]][0],dp[son[x][i]][1]);
	}
	return max(dp[x][1],dp[x][0]);  //返回符合题意的最大值 
}
int main()
{
	int n;
	while(cin>>n)
	{
		ms(pre);
		ms(dp);
		ms(son);
		for(int i = 1;i <= n;i++)
		   son[i].clear(),cin>>zjj[i];
		int a,b;
		while(1)
		{
			cin>>a>>b;
			if(a == 0 && b ==0)
			   break;
			pre[a] = b;
			son[b].push_back(a);
		}
		int ans = 0;
		for(int i = 1;i <= n;i++)
		{
			if(pre[i] == 0)  //从树根开始搜索 
			   ans += dfs(i); 
		 } 
		cout<<ans<<endl;
	}
	return 0;
}

第三种风格的代码(希望大家一起学习)。感谢这位牛人:

#include<stdio.h>
#include<string.h>
#include<vector>
#define MAX 6000+10
using namespace std;
int max(int x,int y)
{
    return x>y?x:y; 
}
int dp[MAX][2],vis[MAX];
vector<int>tree[MAX];//存储子节点 
void dfs(int node)
{
    vis[node]=1;
    int i,j;
    int son;//子节点 
    for(i=0;i<tree[node].size();i++)
    {
        son=tree[node][i];
        if(!vis[son])
        {
            dfs(son);
            dp[node][1]+=dp[son][0];//上司去 下属不去 
            dp[node][0]+=max(dp[son][1],dp[son][0]); // 上司不去 下属去或不去 
        } 
    }
}
int main()
{
    int n,i;
    int root;//根节点 
    int x,y;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&dp[i][1]);
            dp[i][0]=0;
            vis[i]=1; 
            tree[i].clear();//清空 
        }
        while(scanf("%d%d",&x,&y)&&(x!=0||y!=0))
        {
            vis[x]=0;//x为下属 标记一下 
            tree[y].push_back(x);//建立关系 
        }
        root=0;
        for(i=1;i<=n;i++)//查询根节点 
        {
            if(vis[i])
            {
                root=i;//记录根节点 
                break;
            }
        }
        memset(vis,0,sizeof(vis));
        dfs(root);
        printf("%d\n",max(dp[root][0],dp[root][1]));
    }
    return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值