URAL 1039/poj2342-anniversary party-Tree DP

Source: Internet
Author: User

http://poj.org/problem?id=2342

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17663


Test instructions

A company will hold a party, but in order to make the party more active, each party will not want to see his direct boss at the party, now known to everyone's active index and boss relationship (of course, there is no link), ask who (how many people) to invite who can make the total activity of the party is the largest.

States are the trade-offs of each point.

can be used dp[i,1] and dp[i,0, respectively) to express the first person to come and not to come.


When I come, dp[i][1] + = dp[j][0];//j is the subordinate of I

When I do not come, dp[i][0] +=max (dp[j][1],dp[j][0]);//j is the subordinate of I


#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using
namespace Std; 
typedef unsigned __INT64 ull;
Const double Pi=acos (-1.0);
 Double eps=0.000001;
int max (int a,int b) {return a<b?b:a;} int tm[6005];
int n;
int vis[6005];
vector< Vector<int > > MP (6005);
int dp[6005][2];
	void Dfs (int x) {int i;
	DP[X][1]=TM[X];
	dp[x][0]=0;
		For (I=0;i<mp[x].size (); i++) {int v=mp[x][i];
		DFS (v);
		DP[X][1]+=DP[V][0];
	Dp[x][0]+=max (Dp[v][1],dp[v][0]);
	}} int main () {cin>>n; int i;
for (i=1;i<=n;i++) scanf ("%d", &tm[i]);
	int x, y;
		for (i=1;i<n;i++) {scanf ("%d%d", &x,&y);
		Mp[y].push_back (x);
	Vis[x]=1;
	} scanf ("%d%d", &x,&y);
	for (i=1;i<=n;i++) {if (!vis[i]) break;
	} int root=i;
 DFS (root); printf ("%d\n", Max (dp[root][1],dp[root][0]));
	
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.