poj2342 Anniversary party (树形Dp)
2017-08-19 16:26
357 查看
传送门 Time Limit: 1000MS Memory Limit: 65536K
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
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
但是这些员工具有上下级关系,接下来任意行输入两个数,前面是下属后面是上司
当输入 0 0 的时候结束输入,并且上司不能和下属同时出现在派对上,
请你设计一个算法计算活跃的最大值
其实直接建树做也是可以的,但是我这里用下树形Dp吧
设 f[i][j],i 表示第几位员工,j代表来或者不来
那方程很容易出来了:
f[i][1]=sum(f[son[i]][0]);
f[i][0]+=max(f[son[i]][1],f[son[i]][0]);
只要记录一下谁是谁父亲就可以了
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
71
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Sample Output
5题目大意
有一场聚会,邀请同一所学校的n个员工参加,接下来输入n行是他们在派对上的活跃值但是这些员工具有上下级关系,接下来任意行输入两个数,前面是下属后面是上司
当输入 0 0 的时候结束输入,并且上司不能和下属同时出现在派对上,
请你设计一个算法计算活跃的最大值
思路
这道题肯定是要建树的,上司就是父节点,下属就是子节点其实直接建树做也是可以的,但是我这里用下树形Dp吧
设 f[i][j],i 表示第几位员工,j代表来或者不来
那方程很容易出来了:
f[i][1]=sum(f[son[i]][0]);
f[i][0]+=max(f[son[i]][1],f[son[i]][0]);
只要记录一下谁是谁父亲就可以了
代码
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[6005],dp[6005][2],n,root,son,fa,ans; bool vis[6005]; void dfs(int x) { vis[x]=1; for(int i=1;i<=n;i++) if(!vis[i]&&f[i]==x) { dfs(i); dp[x][1]+=dp[i][0]; //上司来,所有下属都不能来 dp[x][0]+=max(dp[i][1],dp[i][0]); //上司不来,下属可以来可以不来 } } int main() { while(scanf("%d",&n)!=EOF) { memset(f,0,sizeof f); memset(dp,0,sizeof dp); memset(vis,0,sizeof vis); for(int i=1;i<=n;i++) scanf("%d",&dp[i][1]); //他们参加时的活跃 while(scanf("%d%d",&son,&fa),son||fa) f[son]=fa; //记录父子关系 for(int i=1;i<=n;i++) if(f[i]==0) { root=i;break; } //找根节点(校长) dfs(root); ans=max(dp[root][0],dp[root][1]); //校长来或者不来 printf("%d\n",ans); } return 0; }
相关文章推荐
- poj2342 Anniversary party【树形dp】
- POJ2342:Anniversary party(树形DP)
- POJ2342 Anniversary party(树形DP)
- POJ2342 Anniversary party(树形dp)
- poj2342 Anniversary party (树形dp)
- poj2342 Anniversary party (树形DP)
- [poj2342]Anniversary party_树形dp
- poj2342 Anniversary party(树形dp)
- POJ2342 ->Anniversary party(树形DP入门题)
- Anniversary party_poj2342_树形dp
- [poj2342]Anniversary party树形dp入门
- HDU 1520 Anniversary party【树形DP】
- HDU 1520 Anniversary party (树形DP,入门)
- POJ 2342 Anniversary party (树形dp 入门题)
- POJ 2342 Anniversary party (树形dp)
- poj 2342 Anniversary party(树形DP基础题)(树形dp模板)
- HDU 1520 & POJ 2342 Anniversary party(树形dp)
- URAL 1039 Anniversary Party (树形DP)
- poj 2342 Anniversary party 树形DP
- hdu1520 Anniversary party(树形DP)