HDU 1054 Strategic Game(二分图最小覆盖集)
2016-02-15 22:00
246 查看
题意:给你一颗具有N个节点的树的所有边信息.现在问你最少需要多少个点放到树的节点上,使得树的任意一条边都至少有一个端点被覆盖.(其实就是最小覆盖集)
思路:明显的最小覆盖集. 且由于树天然就是二分的,所以我们只需要求该树的最小覆盖集点数即可.
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
Sample Output
思路:明显的最小覆盖集. 且由于树天然就是二分的,所以我们只需要求该树的最小覆盖集点数即可.
#include<cstdio> #include<cstring> #include<vector> using namespace std; const int maxn=1500+5; struct Max_Match { int n; vector<int> g[maxn]; bool vis[maxn]; int left[maxn]; void init(int n) { this->n=n; for(int i=0;i<n;i++) g[i].clear(); memset(left,-1,sizeof(left)); } bool match(int u) { for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!vis[v]) { vis[v]=true; if(left[v]==-1 || match(left[v])) { left[v]=u; return true; } } } return false; } int solve() { int ans=0; for(int i=0;i<n;i++) { memset(vis,0,sizeof(vis)); if(match(i)) ++ans; } return ans; } }MM; int main() { int n; while(scanf("%d",&n)==1) { MM.init(n); for(int i=0;i<n;i++) { int u,num,v; scanf("%d:(%d)",&u,&num); while(num--) { scanf("%d",&v); MM.g[u].push_back(v); MM.g[v].push_back(u); } } printf("%d\n",MM.solve()/2); } return 0; }
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
相关文章推荐
- JavaScript 总结几个提高性能知识点
- Yii RESTful Quick Start
- USACO 2.1 frac1
- android ViewPager 不带滑动效果切换item
- codeforces626F Group Projects(dp)
- spring简单概念
- MyEclipse 启动不了
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject(1)-通过属性域注入
- python排序-希尔排序
- 【数据结构】链表中倒数第k个结点
- Quartz与Spring的整合使用
- 图片压缩
- 归并排序
- **LeetCode 76. Minimum Window Substring
- JAVA判断对象是不是属于哪一个类 instanceof , isInstance ,isAssignableFrom
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- Layout布局相关笔记
- 【设计模式】代理模式
- ASPNET5 管理应用程序的状态
- android 优化策略