poj 1463 Strategic game(树形dp)

poj 1463 Strategic game(树形dp)
Time Limit: 2000ms Memory Limit: 65536kB

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

Source
Southeastern Europe 2000


本题一开始模模糊糊地想过递归算法,分离问题然后利用子问题已经算过的数据企图搜索(记忆化搜索),可惜不是很明白,后来搜了题解,结果只看到题解的题目就有思路了。题解题目说是二分图(匈牙利算法)或树形dp做。看到树形dp四个字感觉一瞬间的顿悟划过脑子,之前没想明白的算法突然清晰起来了。以前虽然没学过树形dp,可是dp做的多了,大致也想明白了。
其实树形dp也是一种dp,只不过方程式转移方式在树的父子结点间转移(幸好本题数据给的方式很好,不需要自己建立一个树的边上点的父子关系了,一个有意思的问题是如果给的是邻接表如何自己建树),只需要明白儿子结点在自身染不染色是该子树分别需要最少几个染色(或说需要几个士兵),即可推算出父亲的状态,故dp参数为dp[i][j],表示第i个点,j表示自身染不染色(放不放士兵),情况下的最少染色数(士兵数)。


Accepted    5248kB  135ms   983 B   G++ 
#define MAX_N 1500 
#define MAX_D 10

#include<stdio.h>
#include<memory.h>

int level[MAX_N];
int d[MAX_N];
int son[MAX_N][MAX_D];
int n,name,root,d_now,ans=0;
int dp[MAX_N][2];

inline int Min(int a,int b)
{
    return a<b?a:b;
}

int tree_dp(int now,int state)
{
    //printf("%d %d\n",now,state);
    if (d[now]==0)
        return (state==1)?1:0;
    int s0=0,s1=0;
    if (dp[now][state])
        return dp[now][state];
    for (int j=0;j<d[now];j++)
        s0+=Min(tree_dp(son[now][j],0),tree_dp(son[now][j],1));
    for (int j=0;j<d[now];j++)
        s1+=tree_dp(son[now][j],1);
    if (state==0)
        dp[now][state]=s1;
    else
        dp[now][state]=s0+1;
    return dp[now][state];
}

int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        memset(level,0,sizeof(level));
        memset(dp,0,sizeof(dp));
        for (int i=1;i<=n;i++)
        { 
            scanf("%d:(%d)",&name,&d_now);
            if (i==1)
                root=name; 
            d[name]=d_now;
            for (int j=0;j<d_now;j++)
                scanf("%d",&son[name][j]);
        }
        printf("%d\n",Min(tree_dp(root,0),tree_dp(root,1)));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值