Strategic game
Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and T Hen 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 in the nodes so, they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob have to put for a given tree.
For example for the tree:
The solution is a soldier (at the Node 1).
Input
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_identifiernumber_of_roads
Or
Node_identifier: (0)
the node identifiers is integer numbers between
0 and
n-1, for
n nodes (
0 < n≤15 ). Every edge appears only once in the input data.
Output
The output should is printed on the standard output. For each given input data set, print one, integer number in a, gives the result (the minimum number of sold iers).
Sample INPUT40: (1) 11: (2) 2 32: (0) 3: (0) 53: (3) 1 4 21: (1) 02: (0) 0: (0) 4: (0)Sample Output
1
2
Test instructions: Given a tree, select as few points as possible so that each unselected node is adjacent to at least one of the selected nodes. Outputs the minimum number of nodes to select.
Idea: The classic two-dimensional minimum vertex overlay, is also the classic tree-shaped DP.
minimum vertex override = = Maximum match (bidirectional graph)/2
Data is large with adjacency table. Otherwise it will time out.
<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <set > #include <map>using namespace std;const int maxn = 1510;int NX, Ny;int used[maxn];int CX[MAXN], cy[maxn];vector& Lt;int>g[maxn];int Find (int u) {for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; if (!used[v]) {used[v] = 1; if (Cy[v]==-1 | | Find (Cy[v])) {Cy[v] = u; Cx[u] = v; return 1; }}} return 0;} int Hungary () {int res = 0; memset (CX,-1, sizeof (CX)); memset (CY,-1, sizeof (CY)); for (int i = 0; i < NX; i++) {if (cx[i] = = 1) {memset (used, 0, sizeof (used)); if (Find (i)) res++; }} return res;} int MAIn () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); int t, n, num; int x, y; while (cin>>n) {if (!n) break; NY = NX = n; t = N; for (int i = 0; i < n; i++) g[i].clear (); while (t--) {scanf ("%d: (%d)", &x, &num); while (num--) {scanf ("%d", &y); G[x].push_back (y); G[y].push_back (x); }} printf ("%d\n", Hungary ()/2); } return 0;} </span>
UVa 2038-strategic Game (binary minimum vertex overlay or tree DP)