1、http://poj.org/problem?id=1463
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、题目:
Strategic game
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).
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
![](https://i-blog.csdnimg.cn/blog_migrate/43f52b994a402a64db401be44050adc4.jpeg)
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 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.
- 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
4、AC代码:
#include<stdio.h>
#include<vector>
using namespace std;
#define N 1505
vector<int> vec[N];
int f[N];
int dp[N][2];
void dfs(int root)
{
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]);
}
}
int main()
{
int n,a,m,b;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
vec[i].clear();
f[i]=-1;
dp[i][0]=0;
dp[i][1]=1;
}
for(int i=0; i<n; i++)
{
scanf("%d:(%d)",&a,&m);
for(int i=1; i<=m; i++)
{
scanf("%d",&b);
vec[a].push_back(b);
f[b]=a;
}
}
int a=0;
while(f[a]!=-1)
{
a=f[a];
}
dfs(a);
printf("%d\n",min(dp[a][0],dp[a][1]));
}
return 0;
}