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. 一个关于echo的小知识点

    一个关于echo的小知识点     echo一个布尔值时,如果是true,输出1,而如果是false,将什么都不输出! 网上搜的一个解释: 对于数字类型来说,false 确实 是 0, 而对strin ...

  2. Xcode error: conflicting types for 'XXXX'

    问题描述:在main方法中调用了一个写在main方法后面的方法,比如: void main(){ A(); } void A(){} Xcode编译后就报错:conflicting types for ...

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

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

  4. STL--map

    map--概述: 映射(Map)和多重映射(Multimap)是基于某一类型Key的键集的存在,提供对TYPE类型的数据进行快速和高效的检索. l对Map而言,键只是指存储在容器中的某一成员. lMu ...

  5. 自定义表单input

    我想实现下面这个效果?应该怎么写最方便呢?最有效,兼容性最好呢 我使用<p>标签套lable,加input的组合,p标签绝对定位,input标签铺满,用padding填充. 主要css . ...

  6. 数据结构B树

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  7. js分组排序算法, OrderBy

    由于项目中需要对数据进行分组排序,类似于sql中 order by column1,column2....  实现的关键是 分组排序,第一个column1,排序完成之后,对其分组,然后按照column ...

  8. js 立即执行函数,() .则前面的function 是表达式,不能是函数申明

    fnName(); function fnName(){     ... }//正常,因为‘提升’了函数声明,函数调用可在函数声明之前 fnName(); var fnName=function(){ ...

  9. 设计js通用库

    设计js通用库的四个步骤: 1.需求分析:分析库需要完成的所有功能. 2.编程接口:根据需求设计需要用到的接口及参数.返回值. 3.调用方法:支持链式调用,我们期望以动词方式描述接口. (ps:设计链 ...

  10. 十种JAVA排序算法实例

    一.冒泡(Bubble)排序 复制代码 代码如下: void BubbleSortArray() {       for(int i=1;i<n;i++)       {         for ...