Strategic Game——二分图最大匹配(最小顶点覆盖)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054

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:

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)

Output

1
2

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

‎鲍勃喜欢玩电脑游戏,尤其是战略游戏,但有时他找不到足够快的解决方案,然后他很伤心。现在他有以下问题。他必须保卫一个中世纪的城市,那里的道路是一棵树。他必须把最少的士兵数量放在节点上,以便他们能够观察所有的边缘。你能帮他吗?‎
‎您的程序应找到 Bob 为给定树放置的最小士兵‎
‎数。‎
‎输入文件包含多个文本格式的‎
‎数据集。每个数据集‎

‎表示具有以下说明的树:节点数,‎
‎每个节点的描述‎
‎如下,节点节点_标识符:(数字_道路)节点_标识符1 节点_标识符2...节点_标识符‎
‎或‎
‎节点标识符:(0)‎

‎节点标识符是介于 0 和 n-1 之间的整数,对于 n 个节点(0 <n <= 1500)。每个边只出现在输入数据中一次。‎

‎例如,‎
‎树:‎
‎解决方案是一个士兵(在节点1)。 ‎
‎ ‎

‎输出应打印在标准输出‎
‎上。对于每个给定的输入数据集,在一行中打印一个整数,以给出结果(最小士兵数)。

 

 

拆点把原图转换成二分图,而题目要求的是最少的点使得其他至少一条边和这个点集相连,其实就是最小顶点覆盖。

而且在二分图中,最小顶点覆盖就是最大匹配数,而且这里是无向图,注意除2.

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=1500+7;
int n;
vector<int> G[maxn];
int nxt[maxn],visited[maxn],cnt;
int find(int x){
	for(int i = 0;i<G[x].size();++i){
		int v=G[x][i];
		if(!visited[v]){
			visited[v]=1;
			if(nxt[v]==-1||find(nxt[v])){
				nxt[v]=x;
				return 1;
			}
		}
	}
	return 0;
}
int match(){
	int ans=0;
	memset(nxt,-1,sizeof(nxt));
	for(int i = 0;i<n;++i){
		memset(visited,0,sizeof(visited));
		if(find(i)) ans++;
	} 
	return ans;
}
int main(int argc, char** argv) {
	while(~scanf("%d",&n)){
		for(int i = 0;i<=1500;++i) G[i].clear();
		for(int i = 0;i<n;++i){
			int x,y,num;
			scanf("%d:(%d)",&x,&num);
			if(!num) continue;
			while(num--){
				scanf("%d",&y);
				G[x].push_back(y);
				G[y].push_back(x);
			} 
		}
		printf("%d\n",match()/2);
	}
	return 0;
}

关于二分图匹配的算法可以参考我的这篇文章:https://blog.csdn.net/qq_43472263/article/details/96831025

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值