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
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
Source
Ural State University Internal Contest October’2000 Students Session
思路:
你会发现,奇迹再现它和没有上司的舞会是一道题,标都没改!!我就不解释了
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=6e4+10;
int n,dis[N],u,v,tot,hd[N],f[N][2],ans,vis[N];
struct node
{
int u,v,next;
} e[N];
void add(int x,int y) {e[++tot]=(node){x,y,hd[x]},hd[x]=tot;}
void tree_dp(int root)
{
f[root][1]=dis[root];
for(int i=hd[root];i;i=e[i].next)
{
tree_dp(e[i].v);
f[root][1]+=f[e[i].v][0];
f[root][0]+=max(f[e[i].v][1],f[e[i].v][0]);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&dis[i]);
scanf("%d%d",&u,&v);
while(u&&v)
{
add(v,u),vis[u]=1;
scanf("%d%d",&u,&v);
}
for (int i=1;i<=n;i++)
{
if(!vis[i])
{
tree_dp(i),ans=max(f[i][1],f[i][0]);
break;
}
}
printf("%d",ans);
return 0;
}
探讨在大学80周年庆典上,如何通过算法选择嘉宾名单,以最大化派对氛围。考虑到员工间的上下级关系,避免直接上下级同时出席,通过动态规划求解最优组合。
&spm=1001.2101.3001.5002&articleId=108019024&d=1&t=3&u=ab0886e2e5694c0c9139005ef8b69666)
288

被折叠的 条评论
为什么被折叠?



