本文共 1997 字,大约阅读时间需要 6 分钟。
1、
2、题目大意:
给定一棵树,有n个结点,现在要在每个结点上布兵,使得这些士兵可以监视到所有的路线,问最少放多少士兵?
若果父节点放士兵,那么孩子结点可以放,可以不放,如果父节点不放士兵,那么孩子结点必须放士兵。
int dp[i][0]表示在i结点不放置士兵,初始值为0
int dp[i][1]表示在i结点放置士兵,那么初始值就是1
状态转移方程
for(int i=0; i<vec[root].size(); i++) { int v=vec[root][i]; dfs(v); dp[root][0]+=dp[v][1]; dp[root][1]+=min(dp[v][0],dp[v][1]); //printf("%d %d %d\n",root,dp[root][0],dp[root][1]); }
3、题目:
Time Limit: 2000MS | | Memory Limit: 10000K |
Total Submissions: 5830 | | Accepted: 2657 |
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
4、AC代码:
#include #include using namespace std;#define N 1505vector vec[N];int f[N];int dp[N][2];void dfs(int root){ for(int i=0; i
转载地址:http://hhddi.baihongyu.com/