Strategic Game

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:
 
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
题意:给出n个点的联通情况,在顶点上安排一个士兵,求最小数量的士兵是的士兵能够直接联通到图中所有的点
题解:最小定点覆盖题目,最小顶点覆盖=最大匹配
此题用邻接表进行存储,邻接矩阵会超时。用vector数组实现
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int p[1500][1500];
int link[1500];
int used[1500];
int d[1500];
int pp[1500];
int n;
vector<int> mp[1500];
bool dfs(int u)
{
 int v;
 for(v=0;v<mp[u].size();v++)
 {
     int k=mp[u][v];
  if(!used[k])
  {
   used[k]=true;
   if(link[k]==-1||dfs(link[k]))
   {
              link[k]=u;
     return true;
   }
  }
 }
 return false;
}
int main()
{
 int i,m,x,j,q;
    while(scanf("%d",&n)!=EOF)
 {
  memset(link,-1,sizeof(link));
    for(i=0;i<n;i++) mp[i].clear();
  for(i=0;i<n;i++)
  {
           scanf("%d:(%d)",&m,&x);
     for(j=0;j<x;j++)
     {
      scanf(" %d",&q);
      mp[m].push_back(q);
      mp[q].push_back(m);
     }
  }
  int cnt=0;
  for(i=0;i<n;i++)
  {
   memset(used,0,sizeof(used));
   if(dfs(i)) cnt++;
  }
        printf("%d\n",cnt/2);
 }
 return 0;
}
posted @ 2013-09-18 21:32  forevermemory  阅读(236)  评论(0编辑  收藏  举报