题意:

给你一棵树,树的每一个节点可以守护与其相连的所有边,问你最少用多少个节点可以守护这整棵树

思路:

仔细思考不难发现,要想守护一条边,边的两个端点必须有一个可以被选(两个都选也可以),然后这个问题就变成了翻版的没有上司的舞会

定义:dp[i][0]表示不选i,守护其子树需要多少点

    dp[i][0]表示选上i,守护其子树需要多少点

状态转移方程:

    dp[i][0] =  ∑dp[j][1]  (i为j的父亲节点)  

     dp[i][1] = 1+∑min(dp[j][1],dp[j][0])  (i为j的父亲节点)

vetcor忘记清空T了好几次。。。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
int dp[maxn][],n,fa[maxn];//dp[i][0]代表不选i,dp[i][1]代表选i
vector<int> edge[maxn];
void dfs(int x,int fa)
{
dp[x][]=,dp[x][]=;
for(int i=;i<edge[x].size();i++){
if(edge[x][i]!=fa){
dfs(edge[x][i],x);
dp[x][]+=dp[edge[x][i]][];
dp[x][]+=min(dp[edge[x][i]][],dp[edge[x][i]][]);
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF){
int x,num,y;
for(int i=;i<n;i++)
edge[i].clear();
for(int i=;i<n;i++){
num=;
scanf("%d:(%d)",&x,&num);
for(int j=;j<=num;j++){
scanf("%d",&y);
edge[x].push_back(y);
edge[y].push_back(x);
}
}
dfs(,-);
printf("%d\n",min(dp[][],dp[][]));
}
}

(树形DP)Strategic game POJ - 1463的更多相关文章

  1. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  2. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

  3. Strategic game POJ - 1463

    题目链接 依旧是树形dp啦,一样的找根节点然后自下而上更新即可 设\(dp_{i,0}\)表示第i个不设,\(dp_{i,1}\)表示第i个设一个,容易得到其状态转移 \(dp_{i,0} = \su ...

  4. Strategic game POJ - 1463 树型dp

    //题意:就是你需要派最少的士兵来巡查每一条边.相当于求最少点覆盖,用最少的点将所有边都覆盖掉//题解://因为这是一棵树,所以对于每一条边的两个端点,肯定要至少有一个点需要放入士兵,那么对于x-&g ...

  5. 树形dp入门(poj 2342 Anniversary party)

    题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...

  6. Strategic game POJ - 1463 【最小点覆盖集】

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...

  7. Strategic game POJ - 1463 dfs

    题意+题解: 1 //5 2 //1 1 3 //2 1 4 //3 1 5 //1 1 6 //给你5个点,从下面第二行到第五行(称为i行),每一行两个数x,y.表示i和x之间有一条边.这一条边的长 ...

  8. POJ 1463 Strategic game(树形DP入门)

    题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...

  9. poj 1463 Strategic game DP

    题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS   Memory Limit: 10000K Tot ...

随机推荐

  1. vue-router再学习

    vue路由: 1:动态路由配置 import Vue from 'vue' import Router from 'vue-router' import Index from '@/view/inde ...

  2. eslint在webstorm中有错误警告

    1. 报错Missing space before function parentheses的问题 解决:在代码目录中,打开.eslint文件,并在rules中添加如下一行代码即可: "sp ...

  3. jQuery 工具类函数-检测对象是否为原始对象

    调用名为$.isPlainObject的工具函数,能检测对象是否为通过{}或new Object()关键字创建的原始对象,如果是,返回true,否则,返回false值,调用格式为: $.isPlain ...

  4. Python3装饰器的使用

    装饰器 简易装饰器模板 def wrapper(func): def inner(*args,**kwargs): print('主代码前添加的功能') ret=func(*args,**kwargs ...

  5. 2018-8-10-WPF-修改图片颜色

    title author date CreateTime categories WPF 修改图片颜色 lindexi 2018-08-10 19:16:53 +0800 2018-07-03 15:4 ...

  6. vue-learning:9-template-v-model

    表单元素的双向绑定指令v-model 目录 v-model的基础用法 v-model双向绑定实现的原理 v-model绑定值的输出类型(字符串.数组.布尔值.自定义) v-model修饰符:.lazy ...

  7. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  8. slim的简单使用

    1.在命令行进入项目根目录,然后用composer下载slim composer require slim/slim "^3.0" 2.下载slim完成后,在php文件中引入req ...

  9. 008.MFC_ScrollBar

    滚动条 CScrollBar 水平滚动条控件和垂直滚动条 滚动条消息 SB_THUMBTRACK SB_LINELEFT SB_LINERIGHT SB_PAGELEFT SB_PAGERIGHT 一 ...

  10. 拒绝FileNotFoundException!总结了这几个读取jar包外配置文件的知识点

    前言 相信很多人遇到过这个问题:本地运行的好好的程序,怎么部署到线上就报找不到配置呢? 初识getResource 案例一 FieldMapConfig.class.getResource(" ...