For this type of question, each node has two different States, namely, the existence and absence of soldiers. You can find the minimum consumption of different States.
1: the current node is the root node, and the minimum consumption of the soldiers daemon road is arranged on this node. In this case, his child node can arrange soldiers or not. You can select the minimum consumption from the values of two sub-nodes in different States (there are soldiers, there are no soldiers), and then sum up to find the minimum consumption of soldiers on the current node.
2: the current node is the root node, and no soldiers exist. This situation is very clear, because there are no soldiers on the current node, there is no guardian on the road between the node and the child node, so the child node must arrange soldiers, so in this case. The minimum consumption of this node is the sum of the minimum consumption when each sub-node has soldiers.
Strategic game
Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2736 accepted submission (s): 1134
Problem descriptionbob 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 shocould 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 shoshould 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
40 :( 1) 11 :( 2) 2 32 :( 0) 3 :( 0) 53 :( 3) 1 4 21 :( 1) 02 :( 0) 0 :( 0) 4 :( 0)
Sample output
12# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <vector> # include <iterator> using namespace STD; # define maxn 1510 vector <int> node [maxn]; bool visited [maxn]; int DP [2] [maxn]; // DP [0] [I] node I puts the minimum consumption scheme of soldiers DP [1] [I] node I does not put the minimum scheme int n of soldiers; int min (int A, int B) {return a <B? A: B;} void Init () {int root, to; char a; int num; memset (DP, 0, sizeof (DP); memset (visited, 0, sizeof (visited); For (INT I = 0; I <= N; I ++) node [I]. clear (); For (INT I = 1; I <= N; I ++) {CIN> root> A> num>; for (Int J = 0; j <num; j ++) {CIN> to; node [root]. push_back (to); node [to]. push_back (Root) ;}} void DFS (INT root) {int temp; visited [root] = true; For (INT I = 0; I <node [root]. size (); I ++) {temp = node [root] [I]; If (visited [temp]) continue; DFS (temp ); DP [1] [root] + = DP [0] [temp]; DP [0] [root] + = min (DP [1] [temp], DP [0] [temp]);} DP [0] [root] + = 1;} int main () {While (CIN> N) {Init (); DFS (0); cout <min (DP [1] [0], DP [0] [0]) <Endl;} return 0 ;}