题目链接:http://poj.org/problem?id=1947

看了很多题解都是直接一遍dfs就搞定的方法,但是我实在是没看懂那个转移方程。最后在茫茫博客中终于发现了一个有逻辑的方法,但是复杂度好像要高一些,但是还是把这个题过了。http://www.chongchonggou.com/g_91242661.html

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std; const int maxn=;
vector<int> G[maxn]; const int INF=0x3f3f3f3f;
int dp[][maxn];
int f[maxn][maxn];
int res[maxn][maxn]; void dfs(int u,int fa)
{
if (G[u].size()==)
{
f[u][]=f[u][]=;
if (fa==-) res[u][]=res[u][]=;
else res[u][]=res[u][]=;
}
else
{
for (int i=;i<G[u].size();i++)
{
int v=G[u][i];
dfs(v,u);
}
memset(dp,INF,sizeof(dp));
dp[][]=;
for (int i=;i<=G[u].size();i++)
{
for (int j=;j<=;j++)
{
dp[i&][j]=INF;
for (int k=;k<=j;k++)
{
dp[i&][j]=min(dp[i&][j],dp[i&^][j-k]+f[G[u][i-]][k]+(k==));
}
}
}
f[u][]=;
if (fa==-) res[u][]=;
else res[u][]=;
for (int i=;i<=;i++) f[u][i]=dp[G[u].size()&][i-];
for (int i=;i<=;i++) res[u][i]=f[u][i]+(fa!=-);
}
} int main()
{
memset(f,INF,sizeof(f));
memset(res,INF,sizeof(res));
int n,P;
scanf("%d%d",&n,&P);
for (int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
dfs(,-);
int ans=INF;
for (int i=;i<=n;i++) ans=min(ans,res[i][P]);
printf("%d\n",ans);
return ;
}

[poj 1947]树dp+背包问题的更多相关文章

  1. POJ 1947 树DP获得冠军

    特定N点,N-1的关系.建立了一棵树,问至少减去几个边缘节点可以被作为得到P树.树典型DP称号 dp[cur][j] :记录cur节点,为了得到一个j除去该子树的节点的边的最小数目 对当前树的每个子树 ...

  2. poj 1947 树形dp

    思路:dp[i][j]表示,以i节点为根,删去j个节点最少要断几条边. 那么dp[u][j]=min(dp[u][j],dp[v][k]+dp[u][j-k]);//选取最优状态 dp[u][j]=m ...

  3. poj 1947(树形DP+背包)

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10663   Accepted: 4891 ...

  4. poj 3162 树DP+单调队列

    http://acm.hust.edu.cn/vjudge/problem/11552 http://blog.csdn.net/woshi250hua/article/details/7727677 ...

  5. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  6. POJ3417 LCA+树dp

    http://poj.org/problem?id=3417 题意:先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂 ...

  7. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  8. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

  9. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

随机推荐

  1. POJ2762 单向连通图(缩点+拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

  2. 014---Django的中间件

     前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...

  3. Android log 里面快速搜索错误堆栈 ( 关键字)

    有时候,别人给你的log 文件,是一个文件夹,里面放了很多文件.但是可能你需要的log 只有几行.这时候不可能手工搜索的. 那怎么办呢?使用FileLocationPro.下载地址: https:// ...

  4. Hbase读写流程和寻址机制

    写操作流程 (1) Client通过Zookeeper的调度,向RegionServer发出写数据请求,在Region中写数据. (2) 数据被写入Region的MemStore,直到MemStore ...

  5. Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/

    Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/ ...

  6. SGU 101 Domino 题解

    鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...

  7. python统计日志小脚本

    日志格式如下: [ 2016-06-28T00:10:33-03:00 ] xxx.xx.xx.xxx /api/index/xxx/ ERR: code:400 message: params: c ...

  8. 不得不服!Python速度虽然慢,但是它工作效率很高!

    写在前面 让我们来讨论一个我最近一直在思考的问题:Python 的性能.顺便说一下,我是 Python 的忠实拥趸,我在各种情况下都会积极尝试使用 Python 来解决问题.大家对 Python 最大 ...

  9. 九度OJ--1165(C++)

    #include <iostream>#include <string>#include <vector> using namespace std; int mai ...

  10. iOS-Hello World

    尝试练习一些简单的app,能快速上手开发环境和开发流程.基础Start Developing iOS Apps (Swift)https://developer.apple.com/library/c ...