HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
题目链接:https://vjudge.net/problem/HDU-1054
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8673 Accepted Submission(s): 4174
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<cstdio>
#include<cstring>
#include<vector>
using namespace std; vector<int>G[];
bool vis[];
int match[]; bool dfs(int u)
{
for(int i = ; i<G[u].size(); i++)
{
int t = G[u][i];
if(!vis[t])
{
vis[t] = true;
if(match[t]==- || dfs(match[t]))
{
match[t] = u;
return true;
}
}
}
return false;
} int main()
{
int n,m,k,a,ans;
while(scanf("%d",&n)==)
{
for(int i = ; i<n; i++)
G[i].clear();
for(int i = ; i<n; i++)
{
scanf("%d:(%d)",&m,&k);
for(int j = ; j<k; j++)
{
scanf("%d",&a);
G[m].push_back(a);
G[a].push_back(m);
}
} ans = ;
memset(match,-,sizeof(match));
for(int i = ; i<n; i++)
{
memset(vis,false,sizeof(vis));
if(dfs(i))
ans++;
} printf("%d\n",ans/);
}
}
树形DP:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; struct
{
int to, next;
}e[]; int h[], dp[][], num; void addedge(int u, int v)
{
e[num].to = v;
e[num].next = h[u];
h[u] = num++;
} int dfs(int u)
{
int v;
dp[u][] = ; dp[u][] = ;
for(int i = h[u]; i!=-; i = e[i].next)
{
v = e[i].to;
dfs(v);
dp[u][] += dp[v][];
dp[u][] += min(dp[v][],dp[v][]);
}
return min(dp[u][], dp[u][]);
} int main()
{
int n,m,u,v,k,rt;
while(~scanf("%d",&n))
{
memset(h,-,sizeof(h));
rt = -; num = ;
for(int i = ; i<n; i++)
{
scanf("%d:(%d)",&u,&k);
for(int j = ; j<k; j++)
{
scanf("%d",&v);
addedge(u,v);
} if(rt==-) rt = u;
} printf("%d\n",dfs(rt));
}
}
HDU1054 Strategic Game —— 最小点覆盖 or 树形DP的更多相关文章
- LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)
题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...
- POJ1463 Strategic game (最小点覆盖 or 树dp)
题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆 ...
- POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)
题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- HDU 1054 Strategic Game 最小点覆盖
最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...
- HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】
<题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
随机推荐
- 【02】koala编译中文出错(已放弃不用)
http://koala-app.com/index-zh.html koala 下载地址. sass.中文编译出错: 打开 Koala文件夹位置->rubygems->gems- ...
- 【01】sass安装
因为sass依赖于ruby环境,所以装sass之前先确认装了ruby.先导官网下载个ruby 下载地址:https://rubyinstaller.org/downloads/ ** ...
- HR面试你需要注意什么?
公司的面试流程一般是笔试—>技术面试—>hr面试,在大部分应聘测试工程师这种技术岗的应聘者理解中,通常认为通过技术面试了,后面的hr面试基本就是走流程过形式.也正因如此,我们习惯性地把精力 ...
- 大数据学习——hive使用
Hive交互shell bin/hive Hive JDBC服务 hive也可以启动为一个服务器,来对外提供 启动方式,(假如是在itcast01上): 启动为前台:bin/hiveserver2 启 ...
- Linux 下Python2.7解决list打印中文字符问题
在写一个爬取智联招聘数据的爬虫中,将所需内容匹配到后打印出现了utf-8字符,并没有出现中文字符. 例如: >>>listnine = ['梨', '橘子', '苹果', '香蕉'] ...
- python3 时间复杂度
时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...
- 数据库连接 Mysqli
//数据库连接 Mysqli $db= new Mysqli("localhost","root","root","asd_808 ...
- js清除非数字输入
function clearNoNum(obj) { obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符 ...
- C# CreateDataTable
public DataTable CreateDataTable() { DataTable dataTable = new DataTable(); ...
- POJ 3090 坐标系上的视线遮蔽问题
Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...