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
 
Source
 

————————————————————————————————

题目的意思是给出一棵树,在顶点上放置士兵,使得每条边至少有一个顶点有士兵

思路:即求最小覆盖点集,根据最小覆盖点集=最大二分匹配,求出最大二分匹配即可

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; const int MAXN=2000;
int uN,vN; //u,v数目
vector<int>g[MAXN];
int linker[MAXN];
bool used[MAXN]; bool dfs(int u)
{
int v;
int siz=g[u].size();
for(v=0; v<siz; v++)
if(!used[g[u][v]])
{
used[g[u][v]]=true;
if(linker[g[u][v]]==-1||dfs(linker[g[u][v]]))
{
linker[g[u][v]]=u;
return true;
}
}
return false;
}
int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0; u<uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main()
{
int n,m,x,y;
while(~scanf("%d",&n))
{
for(int i=0;i<MAXN;i++)
g[i].clear();
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&x,&m);
for(int j=0;j<m;j++)
{
scanf("%d",&y);
g[x].push_back(y);
g[y].push_back(x);
}
}
uN=vN=n;
printf("%d\n",hungary()/2); }
return 0;
}

最新文章

  1. Android--sharepreference总结
  2. SQL Server 2008中的数据压缩
  3. mvc url 伪静态
  4. 帮助你在 Photoshop 中轻松实现长阴影效果的工具
  5. Lazarus如何变成XE的界面
  6. IDEA中 @override报错的处理步骤
  7. Be quiet
  8. Azure构建PredictionIO和Spark的推荐引擎服务
  9. Javascript中布尔运算符的高级应用
  10. Redis集群伸缩
  11. ZJOI 2019 游记
  12. hbase-连接流程
  13. [JXOI2018]游戏
  14. 理解并设计rest/restful风格接口
  15. 委托/lambda表达式/事件
  16. 1-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(方案总揽)
  17. 【转载】掌握 HTTP 缓存——从请求到响应过程的一切(下)
  18. 利用lipo编译合并iPhone模拟器和真机通用的静态类
  19. Python中list,tuple,dict,set的区别和用法(转)
  20. Python配置tab自动补全功能

热门文章

  1. 日期控件 DatePicker 在ie8不能用
  2. Activity(活动)
  3. The valid characters are defined in RFC 7230 and RFC 3986问题
  4. RNA测序相对基因表达芯片有什么优势?
  5. Laravel 利用 observer 类基于状态属性,对进行删除和修改的控制
  6. JSR 规范目录
  7. swift NSdata 转换 nsstring
  8. 企业类Web原型制作分享-Kraftwerk
  9. 企业官网原型制作分享-Starbucks
  10. python正则表达式获取两段标记内的字符串