HDU1054 Strategic Game(最小点覆盖)
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10153 Accepted Submission(s): 4744
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054
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
题意:
问在图中最少放置多少个士兵,可以看守所有的边。
题解:
最小点覆盖模板。
但由于数据量较大,边数较多,邻接矩阵要超时,我便改用vector存储。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std; const int N = ;
int check[N],match[N];
int n,ans,dfn;
vector<int> link[N]; inline int dfs(int x){
for(int i=;i<link[x].size();i++){
int now = link[x][i];
if(check[now]!=dfn){
check[now]=dfn;
if(match[now]==- || dfs(match[now])){
match[now]=x;
return ;
}
}
}
return ;
} int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) link[i].clear();
mem(link,);mem(match,-);ans=dfn=;
for(int i=,u,k;i<n;i++){
scanf("%d:(%d)",&u,&k);
for(int j=,v;j<=k;j++){
scanf("%d",&v);
link[u].push_back(v);
link[v].push_back(u);
}
}
for(int i=;i<n;i++){
dfn++;
if(dfs(i)) ans++;
}
printf("%d\n",ans/);
}
return ;
}
HDU1054 Strategic Game(最小点覆盖)的更多相关文章
- HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others) ...
- POJ1463 Strategic game (最小点覆盖 or 树dp)
题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆 ...
- HDU 1054 Strategic Game 最小点覆盖
最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...
- HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】
<题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- Strategic game POJ - 1463 【最小点覆盖集】
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)
题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...
- 最小点覆盖 hdu--1054
点击打开题目链接 最小点覆盖=最大二分匹配的 (单向图) ; 最小点覆盖=最大二分匹配的一半 (双向图) ; 证明 所以我们只需求最大匹配,用 匈牙利算法 求出最大匹配,除以二得到答案 具体算法都已经 ...
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
随机推荐
- 怎么用Python Flask模板jinja2在网页上打印显示16进制数?
问题:Python列表(或者字典等)数据本身是10进制,现在需要以16进制输出显示在网页上 解决: Python Flask框架中 模板jinja2的If 表达式和过滤器 假设我有一个字典index, ...
- vue---day03
1. Vue的生命周期 - 创建和销毁的时候可以做一些我们自己的事情 - beforeCreated - created - beforeMount - mounted - beforeUpdate ...
- R语言学习笔记(十四):零碎知识点(41-45)
41--ls( ) ls()可以用来列出现存的所有对象. pattern是一个具名参数,可以列出所有名称中含有字符串"s"的对象. > ls() [1] "s&qu ...
- 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 110077 ...
- 使用sqoop将mysql中表导入hive中报错
[hdfs@node1 root]$ sqoop import --connect jdbc:mysql://node2:3306/cm?charset-utf8 --username root -- ...
- 『JavaScript』封装
封装可以被定义为对对象的内部数据表现形式和实现细节进行隐藏.通过封装可以强制实施信息隐藏. 在JavaScript中,并没有显示的声明私有成员的关键字等.所以要想实现封装/信息隐藏就需要从另外的思路出 ...
- JMeter学习笔记(九) 参数化3--User Defined Variables
3.User Defined Variables 1)添加用户定义的变量 2)添加变量 3)添加HTTP请求,引用变量,格式:${} 4)执行HTTP请求,察看结果树 5)用户定义的变量,优缺点: * ...
- 监控memcache服务
监控memcache服务是否正常,模拟用户(web客户端)检测. 使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率. #!/bin/bash #################### ...
- react错误总结
react-native 错误总结 The development server returned response error code: 500 in react-native https://b ...
- JavaScript Map数据结构
Array.prototype.remove = function (s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...