Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7930 Accepted Submission(s): 3777
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:
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
Source
Recommend
二分图匹配 之 最小点覆盖
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define maxn 1510 6 int n,head[maxn],tot,link[maxn]; 7 struct Edge{ 8 int from,to,next; 9 }e[maxn*2]; 10 bool exist[maxn]; 11 void Add_Edge(int u,int v){ 12 e[++tot].from=u;e[tot].to=v; 13 e[tot].next=head[u];head[u]=tot; 14 } 15 void Prepare(){ 16 memset(e,0,sizeof(e));tot=0; 17 memset(head,0,sizeof(head)); 18 memset(link,-1,sizeof(link)); 19 } 20 bool DFS(int u){ 21 for(int i=head[u];i;i=e[i].next){ 22 int v=e[i].to; 23 if(!exist[v]){ 24 exist[v]=true; 25 if(link[v]==-1 || DFS(link[v])){ 26 link[v]=u; 27 return true; 28 } 29 } 30 } 31 return false; 32 } 33 int main() 34 { 35 while(scanf("%d",&n)==1){ 36 Prepare(); 37 for(int i=0,m,u,v;i<n;i++){ 38 scanf("%d:(%d)",&u,&m); 39 for(int j=0;j<m;j++){ 40 scanf("%d",&v); 41 Add_Edge(u,v);Add_Edge(v,u); 42 } 43 } 44 int ans=0; 45 for(int i=0;i<n;i++){ 46 memset(exist,false,sizeof(exist)); 47 if(DFS(i))ans++; 48 } 49 printf("%d ",ans/2); 50 } 51 return 0; 52 }