Hdu1054 Strategic Game(最小覆盖点集)
Strategic Game
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:
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)
2
————————————————————————————————
题目的意思是给出一棵树,在顶点上放置士兵,使得每条边至少有一个顶点有士兵
思路:即求最小覆盖点集,根据最小覆盖点集=最大二分匹配,求出最大二分匹配即可
#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;
}
最新文章
- Android--sharepreference总结
- SQL Server 2008中的数据压缩
- mvc url 伪静态
- 帮助你在 Photoshop 中轻松实现长阴影效果的工具
- Lazarus如何变成XE的界面
- IDEA中 @override报错的处理步骤
- Be quiet
- Azure构建PredictionIO和Spark的推荐引擎服务
- Javascript中布尔运算符的高级应用
- Redis集群伸缩
- ZJOI 2019 游记
- hbase-连接流程
- [JXOI2018]游戏
- 理解并设计rest/restful风格接口
- 委托/lambda表达式/事件
- 1-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(方案总揽)
- 【转载】掌握 HTTP 缓存——从请求到响应过程的一切(下)
- 利用lipo编译合并iPhone模拟器和真机通用的静态类
- Python中list,tuple,dict,set的区别和用法(转)
- Python配置tab自动补全功能