Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5812 Accepted Submission(s): 2690
Problem 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.
The input file 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_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
the solution is one soldier ( at the node 1).
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 table:
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
The input file 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_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
![](https://i-blog.csdnimg.cn/blog_migrate/375f7d616782d8d9876a06e39b58fc3c.gif)
the solution is one soldier ( at the node 1).
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 table:
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
题意大概就是给你n个点建成的树, 让你在里面放士兵, 士兵可以观察到临近点, 问至少放多少士兵可以观察到所有的点。
本题容易理解错误, 士兵是覆盖边而不是覆盖点。 也就是说士兵可以观察他临近的点。
因为是树, 我们可以随便取一个点作为根节点。
状态转移方程就是
dp[root][0] = 0;
dp[root][1] = 1;
dp[root][0] += dp[root][1];
dp[root][1] += min(dp[root][0], dp[root][1]);
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <cstring>
#define UFOR(i, a, b) for(int i = a; i <= b; i++)
#define DDOR(i, a, b) for(int i = a; i >= b; i--)
#define MEM(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
//typedef __int64 LL;
typedef long long LL;
const int MXN = 1500 + 10;
const int MXM = 5e5 + 10;
const int HS = 1000007;
const int INF = 20000;
vector<int> G[MXN];
int dp[MXN][2];
void dfs(int root, int fa)
{
dp[root][1] = 1;
dp[root][0] = 0;
UFOR(i, 0, G[root].size() - 1) {
if (G[root][i] == fa) continue;
dfs(G[root][i], root);
dp[root][0] += dp[G[root][i]][1];
dp[root][1] += min(dp[G[root][i]][0], dp[G[root][i]][1]);
}
}
int main ()
{
int n;
while (~scanf("%d", &n)) {
UFOR(i, 0, MXN) G[i].clear();
MEM(dp);
UFOR(i, 1, n) {
int u, m;
scanf("%d:(%d)", &u, &m);
UFOR(j, 1, m) {
int v;
scanf("%d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
}
dfs(0, -1);
printf("%d\n", min(dp[0][0], dp[0][1]));
}
return 0;
}
另外一种方法就是用拓扑排序。
因为入度为0的点, 我们当然不用放士兵。
那么我们只要先将入度为0的点全部找出来放入队列, 然后依次从队列里面取出点, 将其子节点的入度减一。
如果子节点没有放士兵, 父节点一定要放士兵。