POJ 1463 Strategic game( 树形DP )

题意:一颗 N 个节点的树,在某一个节点上放置一个兵则可以守住与它相邻的边。最少放置多少个兵才可以守住所有的边。

#include <cstdio>
#include <deque>

using namespace std;

#define ABANDON 0
#define GET 1

deque< int > graph[2010];
int DP[2010][2];

void DFS( int start, int parent ){

    DP[start][ABANDON] = 0;
    DP[start][GET]     = 1;

    int target;

    if( graph[start].size() == 1 && parent != -1 )
        return;

    for( int i = 0; i < graph[start].size(); ++i ){

        target = graph[start][i];

        if( target == parent )
            continue;

        DFS( target, start );
        DP[start][ABANDON] += DP[target][GET];
        DP[start][GET] += min( DP[target][GET], DP[target][ABANDON] );

    }

}

int main(){

    int nodes;
    int start, roads, target;

    while( scanf( "%d", &nodes ) != EOF ){

        for( int i = 0; i <= nodes; ++i )
            graph[i].clear();

        for( int i = 0; i < nodes; ++i ){

            scanf( "%d:(%d)", &start, &roads );

            while( roads-- ){

                scanf( "%d", &target );
                graph[start].push_back( target );
                graph[target].push_back( start );

            }
        }

        DFS( 0, -1 );

        printf( "%d\n", min( DP[0][ABANDON], DP[0][GET] ) );

    }

    return 0;
}</span>
时间: 2024-08-26 06:48:06

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

POJ 1463 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

poj 2324 Anniversary party(树形DP)

/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ---于是当i去时,i的所有儿子都不能去:dp[i][1]=sum(dp[j][0])+a[i],其中j是i的儿子节点. ---当i不去时,i的儿子可去也可不去:dp[i][0]=sum(max(dp[j][0],dp[j][1])),j是i的儿子节点 ---边界条件:当i时叶子节点时,dp[i][

Strategic Game(树形DP)

目录 Strategic Game(树形DP) 题目 题意 思路 题解 Strategic Game(树形DP) 题目 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

poj 1463 Strategic game DP

题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7929   Accepted: 3692 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu

[POJ 1947]Rebuilding Roads (树形dp)

题目链接:http://poj.org/problem?id=1947 题目大意:给你一棵树,树上N个节点.问最少拆掉多少条边使得存在一个联通块,有P个节点. 树形dp,设计状态:dp[u][i]代表以u为根节点的剩下i个节点最少需要拆掉多少条边. 状态转移:dp[u][i+j] = min(dp[u][i+j],dp[u][i]+dp[v][j]-1); 其中v是u的儿子节点. 相当于在树上跑01背包,即每个儿子节点去掉剩下j个的但是要连上u-v边,或者不去掉剩下j个的. 代码: 1 impo

POJ 1947 Rebuilding Roads (树形dp 经典题)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9499   Accepted: 4317 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

POJ - 3162 Walking Race 树形dp 单调队列

POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这样的区间最长的长度? 一开始楞是没看懂题意,最讨厌这种四级题,这是在刁难我英语小能手(能用翻译的就不自己动手).而且这题感觉单调队列那里的处理更难一点,不过还是来说一说怎么树形dp取得最远距离,先画个简简单单丑丑的图 我们直接从1作为根节点开始dfs的话,可以处理1的最远距离,并且可以得出到其它节点

POJ 3162 Walking Race 树形DP+线段树

给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成为一段长度为n的数列 现在mm想要从这数列中选出一个连续的区间,要求这个区间的max-min<=m 输出最长的区间 做了一个下午 思路: 分成2个部分: 1.求出数列,即对于一棵树,求出每一个节点能到达的最远距离 2.对于这段数列,选出一个区间,使得区间的max-min<=m,并且使得区间长度尽量

POJ 2152 Fire (树形DP,经典)

题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站距离上限,求保证该树安全的最小花费. 思路: 要选择部分点来建站解决消防问题,而总花费是有最优解的. 如何进行树形DP? 假设某点t的所有子树的消防问题都解决,而且已经获得最优解了,那么现在考虑的是点t的最优解问题,点t可以依靠任何点只要不超过距离限制即可,那枚举一下所有点试试,一旦t依靠某个点j解