hdu---(1054)Strategic Game(最小覆盖边)
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5034 Accepted Submission(s): 2297
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:
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<cstring>
#include<cstdio>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=;
vector<vector<int> >grid(maxn);
bool vis[maxn];
int savx[maxn];
int n;
int km(int x){
vector<int>::iterator it;
for(it=grid[x].begin();it<grid[x].end();it++){
if(!vis[*it]){
vis[*it]=;
if(savx[*it]==-||km(savx[*it])){
savx[*it]=x;
return ;
}
}
}
return ;
} int main(){
int ans=;
int a,b,c;
int km(int );
while(scanf("%d",&n)!=EOF){
ans=;
memset(savx,-,sizeof(savx));
for(int i=;i<n;i++)
grid[i].clear();
for(int i=;i<n;i++){
scanf("%d:(%d)",&a,&b);
for(int j=;j<b;j++){
scanf("%d",&c);
grid[a].push_back(c);
grid[c].push_back(a);
}
}
for(int i=;i<n;i++){
memset(vis,,sizeof(vis));
ans+=km(i);
}
printf("%d\n",ans/);
}
return ;
}
hdu---(1054)Strategic Game(最小覆盖边)的更多相关文章
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- HDU 1054 Strategic Game(最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...
- HDU 1054:Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1054 Strategic Game(树形DP)
Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...
- HDU 1054 Strategic Game(树形DP)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1054 Strategic Game 最小点覆盖
最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...
- hdu 1054 Strategic Game 经典树形DP
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1054 Strategic Game (二分匹配)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1054 Strategic Game(tree dp)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU——1054 Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Linux 在一个命令行上执行多个命令
Linux 在一个命令行上执行多个命令 1. [ ; ] 如果被分号(;)所分隔的命令会连续的执行下去,就算是错误的命令也会继续执行后面的命令. 2. [ && ] 如果命令被 &am ...
- Excel2003命令栏操作
Excel2003个人觉得应该把它抛掉不管了,但还是没忍住想看下它里面的东东. 一.先列出一些重要对象 Commandbars:命令栏集合 ...
- ajax接收返回值获取不到问题
function testAsync() { //定义一个全局变量来接受$post的返回值 var result; //用ajax的“同步方式”调用一般处理程序 $.ajax({ url: " ...
- 关于seafile启动的问题解决
过了一个国庆,同事反映说seafile服务挂掉了,无法正常连接. 刚才解决了一下,把相关问题简要记录一下: 1.首先连接阿里云,获得相关IP地址(如果已知IP地址,则不需要该步骤)
- C#正则表达式编程(一):C#中有关正则的类
正则表达式是一门灵活性非常强的语言,匹配同样的字符串可能在不同的开发人员那里会得到不同的结果,在平常的时候也是用的时候看看相关资料,不用的时候就丢在脑后了,尽管在处理大部分情况下都能迅速处理,但是处理 ...
- iOS - Swift NSSize 尺寸
前言 结构体,这个结构体用来表示事物的宽度和高度. public typealias NSSize = CGSize public struct CGSize { public var width: ...
- Facebook内部分享:25个高效工作的小技巧
Facebook内部分享:25个高效工作的小技巧 Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下 ...
- struts2 if正确标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
- mysql概要(四)order by,group 的特点,子查询
1.order by 默认按升序排列(asc/desc),多字段排序 order by 字段 排序方式,字段2 排序方式,..:在分组排序中,排序是对分组后的结局进行排序,而不是在组中进行排序. 2. ...
- jquery使用技巧
1. 禁用右键点击(Disable right-click) <!DOCTYPE html> <html> <head> <meta charset=&quo ...