洛谷P2014选课

一道树形DP题。

f[i][j]表示i个点选j门课程的最大学分。

递推方程:

for(int a=n;a>0;a--)//总共选择多少
for(int b=0;b<a;b++)//分别选择多少(b,a-b)
f[x][a]=max(f[x][a],f[x][a-b]+f[u][b]);
//都不遍历0的原因是f[i][0]无论怎样都是0

我们可以证明在j<=m时值都是正确的,剩下的不用管啦么的时间!

 1 #include<iostream>
2 #include<cstdio>
3 #include<ctype.h>
4 #include<vector>
5 #include<algorithm>
6 #include<cstring>
7 using namespace std;
8 const int maxn=305;
9 inline int read()
10 {
11 int x,w=1;char c=getchar();
12 while(!isdigit(c))c=getchar();
13 if(c=='-')w=-1,c=getchar();
14 x=c-'0';c=getchar();
15 while(isdigit(c))x=x*10+c-'0',c=getchar();
16 return x*w;
17 }
18 int n,m;
19 int f[maxn][maxn];
20 vector <int>son[maxn];
21 void dfs(int x)
22 {
23 f[x][0]=0;
24 for(int i=0;i<son[x].size();i++)
25 {
26 int u=son[x][i];
27 dfs(u);
28 for(int a=n;a>0;a--)
29 for(int b=0;b<a;b++)
30 f[x][a]=max(f[x][a],f[x][a-b]+f[u][b]);
31 }
32 }
33 int main()
34 {
35 n=read(),m=read()+1;
36 for(int i=1;i<=n;i++){
37 son[read()].push_back(i); f[i][1]=read();
38 }
39 f[0][0]=f[0][1]=0;
40 n++;
41 dfs(0);
42 printf("%d\n",f[0][m]);
43 return 0;
44 }

最新文章

  1. oracle中TO_CHAR与TO_DATE
  2. 脚本录制(JMeter)
  3. Mysql复制语句
  4. 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0
  5. MyEclipse快捷键记录
  6. C# 中控件 WebBrowser 对 frameset/ iframe 操作和内容获取
  7. Delphi XE5 安卓手机要求
  8. Android 网络通信 HTTP
  9. [转]Windows Shell 编程 第九章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987969】
  10. Android Input设备debug技巧
  11. Qt的setMouseTracking使用
  12. uri&amp;url
  13. Win10系列:JavaScript综合实例3
  14. (转)OWASP ZAP下载、安装、使用(详解)教程
  15. spring.net框架配置和使用
  16. 城市边界线预测(根据灯光指数)(PUL)
  17. TFIDF练习
  18. 分享阿里云SLB-负载均衡的实现基本原理架构
  19. idea使用时,部分jdk的jar包(tool.jar com.sun.javadoc) 无法引入-gradle处理方案
  20. Dora.Interception, 为.NET Core度身打造的AOP框架:不一样的Interceptor定义方式

热门文章

  1. 认真总结Vue3中ref与reactive区别和isRef与isReactive 类型判断
  2. ES6中的新数据类型——Symbol
  3. mybatis自定义打印执行时间并格式化sql插件
  4. WPF中选择文件和选择文件夹的方法
  5. SQLLite数据库
  6. 服务器硬件和RAID配置
  7. (C++)戳西瓜
  8. MySql数据库缓存
  9. flask的常规使用二
  10. Linux账号和权限管理(第二回合)