【树形dp】poj1463

47 篇文章 0 订阅
33 篇文章 0 订阅
Strategic game

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

For example for the tree: 

the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description: 

  • the number of nodes 
  • the description of each node in the following format 
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads 
    or 
    node_identifier:(0) 

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output

1
2
 
题目大意:给你一棵树,在其一些节点上放上最少的士兵,使每一条都能被士兵观察到(每个士兵都只能看到与他所在的节点直接相连的边)
树形dp妥妥的~~
 做完了才猛然意识到这原来就是传说中的最小点覆盖,贪心的那个方法还不会~~
 
f[x][1]以x为根的子树在x上放置的士兵的最少所需的士兵数目
f[x][0]以x为根的子树x上不放置的士兵的最少所需的士兵数目
那么假设k1,k2……ki是x的儿子:
f[x][1] =1 + min(f[k1][0],f[k1][1]) +min(f[k2][0],f[k2][1])+……+ min(f[ki][0],f[ki][1])
//x上放置的士兵,于是它的儿子们可放可不放!
f[x][0] =f[k1][1] +f[k2][1]+……+f[ki][1]
//x上不放置的士兵,它的儿子们都必须放!
刚刚在网上翻题解想确认它是不是真是最小点覆盖,发现有些题解在翻译题目的时候说的是:“要求找到最少放几个士兵才能将所有点都看守到。”但是事实上题目中说的是He has to put the minimum number of soldiers on the nodes so that they can observe all the edges
如果要求看到所有的点的话应该是树的最小支配集了~~~
(如图:1————2————3————4 选1和4,对于最小点覆盖(监视所有的边)是非法决策, 但是对于最小支配集(监视所有的点)就是合法的了)
一个单词没看清楚题目就大不一样了orz……
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const long N = 1600;
long n;
bool b[N][N];
bool v[N];
long f[N][2];
void init()
{
    memset(f, 0, sizeof(f));
    memset(b, 0, sizeof(b));
    long k, m, l;
    for (long i=1; i<=n; i++)
    {
        scanf("%d:(%d)", &k, &m);
        for (long j=1; j<=m; j++)
        {
            scanf("%d", &l);
            b[k][l] = 1;
            b[l][k] = 1;
        }
    }

    for (long i=0; i<n; i++)
    {
        f[i][1] = 1;
    }


    memset(v, 0, sizeof(v));
}

void dfs(long x)
{

    v[x] = 1;
    for (long i=0; i<n; i++)
    {
        //printf("%d  %d  %d\n", x, i, v[i]);
        if ((!v[i]) && (b[x][i]))
        {
            dfs(i);
            f[x][0] += f[i][1];
            f[x][1] += min(f[i][0], f[i][1]);
        }
    }

}
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        init();
        dfs(0);
        // printf("%d\n", root);
        printf("%d\n", min(f[0][0], f[0][1]));
    }


    return 0 ;
}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值