当前位置:网站首页>DP: tree DP
DP: tree DP
2022-07-05 19:58:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
The more, The Better
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5414 Accepted Submission(s): 3217
Problem Description
ACboy I like playing a strategy game very much , On a map , Yes N A castle . Every castle has a certain treasure , In every game ACboy Agree to conquer M Build a castle and get the treasures inside . But because of the geographical location . Some castles cannot be conquered directly , To conquer these castles, you must first conquer a specific Castle . You can help ACboy Figure out which treasure to conquer if you want to get as many as possible M A castle ?
Input
Each test case first contains 2 It's an integer ,N,M.(1 <= M <= N <= 200); In the following N line . Each row contains 2 It's an integer .a,b. In the i That's ok ,a It means to conquer the i A castle must be conquered first a A castle , hypothesis a = 0 Then it means that you can directly overcome the i A castle .
b On behalf of the i The number of treasures in a castle , b >= 0. When N = 0, M = 0 End of input .
Output
For each test case . Output an integer . representative ACboy Conquered M The number of treasures obtained by the castle .
Sample Input
3 2
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0
Sample Output
5
13
dp[i][j] Said to i Root node j Maximum value of child nodes .#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
typedef long long LL;
using namespace std;
const int maxn=220;
int v[maxn];
int n,m;
int dp[maxn][maxn];
vector<int>s[maxn];
void tree_dp(int n,int f)
{
int len=s[n].size();
dp[n][1]=v[n];
for(int i=0;i<len;i++)
{
if(f>1) tree_dp(s[n][i],f-1);
for(int j=f;j>=1;j--)
{
for(int k=1;k<=j;k++)
dp[n][j+1]=max(dp[n][j+1],dp[n][j+1-k]+dp[s[n][i]][k]);
}
}
}
int main()
{
int f;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
v[0]=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++)
s[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&f,&v[i]);
s[f].push_back(i);
}
tree_dp(0,m+1);
printf("%d\n",dp[0][m+1]);
}
return 0;
}
Anniversary party
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 4329
Accepted: 2463
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=6005;
int dp[maxn][2],pre[maxn];
int visit[maxn],n;
void tree_dp(int x)
{
visit[x]=1;
for(int i=1;i<=n;i++)
{
// cout<<"111 "<<i<<endl;
if(!visit[i]&&pre[i]==x)
{
tree_dp(i);
dp[x][1]+=dp[i][0];
dp[x][0]+=max(dp[i][1],dp[i][0]);
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
memset(visit,0,sizeof(visit));
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)
scanf("%d",&dp[i][1]);
int x,y,root;
while(~scanf("%d%d",&x,&y)&&(x+y))
{
pre[x]=y;
root=y;
}
while(pre[root])
root=pre[root];
// cout<<"fuck "<<root<<endl;
tree_dp(root);
printf("%d\n",max(dp[root][0],dp[root][1]));
}
return 0;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117734.html Link to the original text :https://javaforall.cn
边栏推荐
- c語言oj得pe,ACM入門之OJ~
- 建议收藏,我的腾讯Android面试经历分享
- How about testing outsourcing companies?
- That's awesome. It's enough to read this article
- leetcode刷题:二叉树12(二叉树的所有路径)
- 中金财富在网上开户安全吗?
- 处理文件和目录名
- Debezium series: parsing the default value character set
- leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
- Summer Challenge harmonyos - realize message notification function
猜你喜欢
aggregate
【硬核干货】数据分析哪家强?选Pandas还是选SQL
C application interface development foundation - form control (5) - grouping control
再忙不能忘安全
leetcode刷题:二叉树15(找树左下角的值)
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
如何安全快速地从 Centos迁移到openEuler
安卓面试宝典,2022Android面试笔试总结
leetcode刷题:二叉树12(二叉树的所有路径)
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
随机推荐
股票开户哪里好?网上客户经理开户安全吗
函数的概念及语法
Is it safe to open a mobile stock account? Is it reliable?
力扣 1200. 最小绝对差
解决php无法将string转换为json的办法
【c语言】快速排序的三种实现以及优化细节
深度學習 卷積神經網絡(CNN)基礎
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
再忙不能忘安全
图嵌入Graph embedding学习笔记
[untitled]
ICTCLAS用的字Lucene4.9捆绑
使用 RepositoryProvider简化父子组件的传值
Thread pool parameters and reasonable settings
That's awesome. It's enough to read this article
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
40000 word Wenshuo operator new & operator delete
leetcode刷题:二叉树10(完全二叉树的节点个数)
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
95后阿里P7晒出工资单:狠补了这个,真香...