Strategic game
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 6607 | Accepted: 3047 |
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_roadsor
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
还是对树地守卫问题,不过这题是守卫所有的边。节点可以安排士兵,其能守卫相邻的边。求用最少的士兵,守卫所有的边。与皇宫守卫不同。这里只需要看节点是否安排守卫。如果不安排,则其所有子节点均要安排,如果安排守卫,则其子节点状态随意。
//dp[t][0/1] : 根节点为t的子树(0:根节点不安排守卫1:安排)的所有边被守卫的情况下的最小安排士兵数量。 dp[t][0] = sum(dp[ti][1]); dp[t][1] = sum(min(dp[ti][0], dp[ti][1]));
#include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int INF = 999999999; int n; std::vector<int > v[2005]; bool vis[2005]; int dp[2005][2]; void Tdp(int t) { if (v[t].size() == 0) { dp[t][0] = 0; dp[t][1] = 1; return ; } for (int i=0; i<v[t].size(); i++) { Tdp(v[t][i]); dp[t][0] += dp[v[t][i]][1]; dp[t][1] += min(dp[v[t][i]][0], dp[v[t][i]][1]); } dp[t][1]++; } void init() { memset(dp, 0, sizeof(dp)); memset(vis, false, sizeof(vis)); for (int i=0; i<1505; i++) { v[i].clear(); } } int main () { while (scanf ("%d",&n)!=EOF) { init(); for (int i=0; i<n; i++) { int a, b, c; scanf ("%d:(%d)", &a, &b); for (int j=0; j<b; j++) { scanf ("%d", &c); v[a].push_back(c); vis[c] = true; } } int root; for (int i=0; i<n; i++) { if (!vis[i]) { root = i; break; } } Tdp(root); cout << min(dp[root][0], dp[root][1]) << endl; } return 0; }
<!--话说这输入真他妈恶心-->