程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> ural 1039 Anniversary Party

ural 1039 Anniversary Party

編輯:關於C++

1039. Anniversary Party

Time limit: 0.5 second
Memory limit: 8 MB

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
 
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.

Sample

input output
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5

沒有上司的年會。ural要舉辦場年會,請你邀請來賓。要求每位客人的直接上司不被邀請,否則會很尴尬。每位客人有一個興趣值。要求使來賓的興趣值之和最大。

每個人可以選擇去或者不去。所以:

dp[t][0] 表示以t為最高上司的子樹不安排t參加的最大興趣值。

dp[t][1] 表示以t為最高上司的子樹安排t參加的最大興趣值。

不安排t參加的話,其直系下屬可以去或者不去。dp[t][0] = sum(max(dp[ti][0], dp[ti][1]));

安排t參加的話,其直系下屬均不能參加。dp[t][1] = sum(dp[ti][0]) + c[t];

#include 
#include 
#include 
#include 
using namespace std;

int n;
int c[6006];
std::vector v[6006];
int dp[6006][2];

void Tdp(int t) {
	if(v[t].size() == 0) {
		dp[t][0] = 0;
		dp[t][1] = c[t];
		return ;
	}
	for (int i=0; i> n;
	for (int i=1; i<=n; i++) {
		cin >> c[i] ;
	}
	int l ,k;
	int vis[6006];
	memset(vis, false, sizeof(vis));
	while (cin >> l >> k) {
		if (l == 0 && k == 0) break;
		v[k].push_back(l);
		vis[l] = true;
	}
	int root;
	for (int i=1; i<=n; i++) {
		if (!vis[i]) {
			root = i;
			break;
		}
	}
	Tdp(root);
	cout << max(dp[root][0], dp[root][1]) <

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved