Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3772 Accepted Submission (s): 1663
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 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
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
Southeastern Europe 2000
Question:
A very clear problem of minimum point coverage in a bipartite matching graph;
Minimum Point Coverage = maximum number of matches; if you want to know why, you can read my senior blog: link address
The question is not too long. Let's see it for yourself. Note that this question is a two-way graph, so the result must be divided by 2. In addition, the data is too big and the use of the adjacent table is required. If you do not know how to use an adjacent table, go online to view it, or use it in your blog.
#include <stdio.h> #include <string.h> #define CL(x,v);memset(x,v,sizeof(x)); const int maxn = 1500 + 10; int n,top,link[maxn]; bool used[maxn]; int next[maxn*maxn],head[maxn*maxn],num[maxn*maxn]; int Find(int u) { for(int i = head[u];i != -1;i = next[i]) { int v = num[i]; if(!used[v]) { used[v] = u; if(link[v] == -1||Find(link[v])) { link[v] = u; return 1; } } } return 0; } int KM() { int res = 0; CL(link,-1); for(int u = 0;u < n;u++) { CL(used,0); res += Find(u); } return res; } int main() { int m,i,j,index,vex; while(~scanf("%d",&n)) { top = 0; CL(head,-1); for(i = 0;i < n;i++) { scanf("%d:(%d)",&index,&m); for(j = 0;j < m;j++) { scanf("%d",&vex); next[top] = head[index]; num[top] = vex; head[index] = top++; next[top] = head[vex]; num[top] = index; head[vex] = top++; } } printf("%d\n",KM()/2); } return 0; }