Strategic game
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 7486 | Accepted: 3481 |
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:
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
Source
题目链接:http://poj.org/problem?id=1463
题目大意:求树的最小点覆盖
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1505;
int head[MAX], cnt;
int dp[MAX][2];
struct EDGE
{
int to, next;
}e[MAX * MAX / 2];
void Add(int u, int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void DFS(int u, int fa)
{
dp[u][1] = 1;
dp[u][0] = 0;
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(v != fa)
{
DFS(v, u);
dp[u][1] += min(dp[v][0], dp[v][1]);
dp[u][0] += dp[v][1];
}
}
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
cnt = 0;
memset(head, -1, sizeof(head));
int u, num, v, rt;
for(int i = 0; i < n; i++)
{
scanf("%d:(%d)", &u, &num);
if(i == 0)
rt = u;
for(int j = 0; j < num; j++)
{
scanf("%d", &v);
Add(u, v);
Add(v, u);
}
}
if(n == 1)
{
printf("1\n");
continue;
}
memset(dp, 0, sizeof(dp));
DFS(rt, -1);
printf("%d\n", min(dp[rt][0], dp[rt][1]));
}
}
本文探讨了如何使用最小点覆盖算法解决树形结构中的问题,具体为在一个树状道路网络中,最少需要多少士兵来观察所有路径。通过输入节点数量和每个节点的连接情况,算法能计算出所需的最小士兵数量。
688

被折叠的 条评论
为什么被折叠?



