POJ1463Strategic game题解动态规划DP

来源:互联网 发布:高效程序员的45个习惯 编辑:程序博客网 时间:2024/05/18 21:38
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 2096 Accepted: 870

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

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12

Source

Southeastern Europe 2000
入门树状DP
题目描述了一个树形结构,在结点上可安放0个或1个士兵,
要求在满足每条边的两端至少有一个士兵的条件下求得士兵最少数
深度优先搜索遍历树的每一个节点,对于一个节点u,存在两种状态,
以1表示放置,0表示不放置,
d[u][1]表示节点u放置士兵情况下以u为节点子树的最优值,
同理d[u][0]表示不放置为最优值,
转移方程为:
for(v 属于 u的子节点) d[u][1] += max(d[v][0], d[v][1])
for(v 属于 u的子节点) d[u][0] += d[v][1]
最后结果为根节点放置和不放置情况中的最优值
原创粉丝点击