zoukankan      html  css  js  c++  java
  • HDU-1520 Anniversary party

    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的入门题目,俩月之前做过一次,不过当时感觉不是太明白,这次做基本就给搞懂了
    做这个题渐渐的有种感觉就是自己运用数据结构的能力有所增强,即每次要想实现什么东西感觉都不会太困难,或许是因为有了STL吧= =
    好吧,说正经的
    不知道怎么感觉DP和基础的递归越来越像,不过DP尤其是树形DP的关键似乎就在于找到父子结点之间的递推关系,即把原来的线性那一套玩儿法给挪到了树上,更奇妙
    对于某个结点来说,它能返回的最大值就是选他和不选他二者之间最大的那一个,然后我们要做的只是分别求出这两个值再取最大的
    随着时间的增加,越来越发现算导上说的那句话非常正确————DP的核心就是有重叠子问题

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #define MAX 6007
    #define max(a,b) (a)>(b)?(a):(b)
    using namespace std;
    
    vector<int> son[MAX];
    int f[MAX];
    int dp[MAX][2];
    int d_max[MAX];
    int c[MAX];
    
    int df(int x)
    {
    	if(d_max[x] != -207)
    		return d_max[x];
    	else 
    	{
    		int len = son[x].size();
    		for(int i = 0;i < len;i++) {
    			int s = son[x][i];
    			dp[x][0] += df(s);
    			dp[x][1] += dp[s][0];
    		}
    		dp[x][1] += c[x]; 
    		return d_max[x] = max(dp[x][0],dp[x][1]);
    	}
    }
    
    int main()
    {
    	int n;
    	while(cin>>n) {
    		for(int i = 1;i <= n;i++) {
    			cin>>c[i];
    			//循环数据的初始化 
    			d_max[i] = -207;
    			f[i] = -1;
    			dp[i][0] = dp[i][1] = 0;
    			son[i].clear();
    		}
    		int t1,t2;
    		while(scanf("%d%d",&t1,&t2) && t1 + t2) {
    			son[t2].push_back(t1);
    			f[t1] = t2;
    		}
    		//寻找树根 
    		int head;
    		for(int i = 1;i <= n;i++)
    			if(f[i] == -1) {
    				head = i;
    				break;
    			}
    		//初始化叶子节点
    		for(int i = 1;i <= n;i++) 
    			if(son[i].empty()) 
    				d_max[i] = dp[i][1] = c[i];
    		//输出最终的答案 
    		cout<<df(head)<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    PHP for循环的写法和示例
    PHP Socket(套接字连接)扩展简介和使用方法
    PHP exec()函数的介绍和使用DEMO
    PHP trim()函数的作用和使用方法
    Linux环境安装xmapp(PHP-Mysql集成环境)
    HP数组转JSON函数json_encode和JSON转数组json_decode函数的使用方法
    PHP 中使用explode()函数切割字符串为数组
    PHP获取随机数的函数rand()和mt_rand()
    PHP stripos()、strripos()和strrpos() 使用方法和区别
    常用工具说明--搭建基于rietveld的CodeReview平台(未测试)
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/5141501.html
Copyright © 2011-2022 走看看