Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5034    Accepted Submission(s): 2297

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
 
Recommend
 
代码:  这里是最少覆盖边,开始一直搞最小覆盖点,然后各种不得劲,.....后面看了以下提示,然后就明白了。但是改了之后有开始tie,没办法,只能最后用起了邻接表(我用 的是vector来替代的)  最小覆盖点 =顶点数- 最大匹配数  、  最小覆盖边= 等于(无向图)最大匹配/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(最小覆盖边)的更多相关文章

  1. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

  2. HDU 1054 Strategic Game(最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...

  3. HDU 1054:Strategic Game

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

  5. HDU 1054 Strategic Game(树形DP)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDU 1054 Strategic Game 最小点覆盖

     最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...

  7. hdu 1054 Strategic Game 经典树形DP

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu 1054 Strategic Game (二分匹配)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1054 Strategic Game(tree dp)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. HDU——1054 Strategic Game

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. JS如何将CST格式的日期转换为制定格式String

    <html> <body> <script type="text/javascript"> var d = new Date() dateFor ...

  2. [C和指针]第四部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. 把多个JavaScript函数绑定到onload事件处理函数上

    为了让函数只在页面加载完毕后才得到执行,我们会把函数绑定到onload事件上: window.onload = userFunction 但如果有两个函数:firstFunction() 和 seco ...

  4. 关于linux 卸载问题

    网上找了一套引擎 非用protocbuff 2.4.1 结果机器上已经装好了2.6.1 网上找了好多办法都行不通 最后终于在一个群里问到  mark一下 例如 我想卸载当前得protoc 那么 第一步 ...

  5. centos下安装nginx和php-fpm

    安装这两个花了大约七个小时,简直呵呵,安装nginx就是直接 yum install nginx ,但发现一打开php文件就是直接下载该php文件,也就是不能识别php文件,解决这个花了好久,但其实看 ...

  6. Tomcat6.0的Thisisverylikelytocreateamemoryleak异常

    從Apache Tomcat 5.5升級到6.0,通常不用太大的修改,原有的Web Application就能繼續運作.不過在server.xml中設定MySQL Datasource,卻出現一串惱人 ...

  7. 进击的Hybrid App,量身定做缓存机制

    引用张图,简单粗俗的解释下 Native App.Web App 和 Hybrid App Navtie App: 使用平台系统提供的原生语言来编写的 App,如果Android用java,ios用o ...

  8. mysql 倒引号

    1.在mysql中,保留字不能作为表名,字段名等用处,如下:mysql> alter table student add column desc varchar(16) after name;1 ...

  9. access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

    在开启derby服务出现该错误(测试hibernate 连接数据库时  使用myeclipse2014自带的数据库--windows->show view->other->Myecl ...

  10. Linux小知识

    1,ubuntu下,开机如何进行命令行? 图形模式下,首先进入终端: 1. 找到 /etc/default/grub文件: 2. 修改 GRUB_CMDLINE_LINUX_DEFAULT=" ...