POJ2342 Anniversary party
Posted SummerSky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2342 Anniversary party相关的知识,希望对你有一定的参考价值。
Anniversary party
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 N – 1 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
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.
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 #define rep(i,a,n) for(int i=a;i<=n;i++) 13 #define per(i,n,a) for(int i=n;i>=a;i--) 14 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 15 #define clr(a) memset(a,0,sizeof(a)) 16 #define pb push_back 17 #define mp make_pair 18 #define fi first 19 #define sc second 20 #define pq priority_queue 21 #define pqb priority_queue <int, vector<int>, less<int> > 22 #define pqs priority_queue <int, vector<int>, greater<int> > 23 #define vec vector 24 ld eps=1e-9; 25 ll pp=1000000007; 26 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 27 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 28 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 29 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 30 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 31 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar(); 32 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar(); 33 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar(); 34 if(last==‘-‘)ans=-ans; return ans; 35 } 36 const int N=10000; 37 int v[N],Next[N],head[N],dp[N][2],a[N],in[N],e; 38 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e; } 39 void dfs(int x,int f){ 40 for (int i=head[x];i;i=Next[i]) 41 if (v[i]){ 42 dfs(v[i],x); 43 dp[x][1]+=dp[v[i]][0]; 44 dp[x][0]+=max(dp[v[i]][0],dp[v[i]][1]); 45 } 46 } 47 int main(){ 48 int n=read(); 49 for (int i=1;i<=n;i++) a[i]=read(),dp[i][1]=a[i]; 50 int a_,b_,root; 51 while (~scanf("%d%d",&a_,&b_)){ 52 if (a_==0 && b_==0) break; 53 add(b_,a_); in[a_]++; 54 } 55 for (int i=1;i<=n;i++) 56 if (!in[i]) root=i; 57 dfs(root,-1); 58 printf("%d",max(dp[root][1],dp[root][0])); 59 return 0; 60 }
以上是关于POJ2342 Anniversary party的主要内容,如果未能解决你的问题,请参考以下文章
poj2342 Anniversary party (树形dp)