题目链接: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

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

最小点覆盖:

 #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的更多相关文章

  1. LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)

    题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...

  2. POJ1463 Strategic game (最小点覆盖 or 树dp)

    题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆 ...

  3. POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)

    题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...

  4. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  5. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  6. HDU 1054 Strategic Game 最小点覆盖

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

  7. HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】

    <题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...

  8. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  9. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

随机推荐

  1. Web框架下安全漏洞的测试反思

    此文已由作者王婷英授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在平时的测试中,一般情况下,我们都是比较关注功能业务测试,以及对应的接口测试,很少去关注对应的业务设计上存在 ...

  2. 在mysql的操作界面中,如何清屏幕

    1.快捷键:Ctrl+L2.通过执行SHELL命令: \! clear实际上 \! 用来执行操作系统的shell命令,不仅是clear,其他命令也可以.shell命令执行完成后,会返回mysql Us ...

  3. Vim增强工具设置

    Vim增强工具设置操作准备:vim ~/.vimrc11. 缩进 & 制表符使 Vim 在创建新行的时候使用与上一行同样的缩进: set autoindent 2. 设置文件里的制表符 (TA ...

  4. Tsinghua OJ Zuma

    Description Let's play the game Zuma! There are a sequence of beads on a track at the right beginnin ...

  5. CodeForces 596A Wilbur and Swimming Pool

    水题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  6. Spring Data JPA 进阶

    Java持久化查询语言概述 Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起,使用这种语言编写的查询是可移植的,可以被编 ...

  7. Entity framework自定义字段实现思路

    ublic class MyModel { public int MyModelID { get; set; } public string FixedProperty1 { get; set; } ...

  8. Java基础教程:tutorialspoint-java

    来自turorialspoint的Java基础教程(英文),官网:https://www.tutorialspoint.com/java/index.htm 这个教程在国内已经被翻译成中文(不过是属于 ...

  9. Eclipse打war包方法以及Eclipse移植项目时JDK版本不匹配Project facet Java version 1.7 is not supported

    打包时: 在项目上右键选择Export,如图: 然后选择WAR file,如图所示.接着再:其中web projecct为打出来包的名字, Destination,打包后存的位置,点击Browse.. ...

  10. WebLogic"域"的概念

    WebLogic Server中的域是逻辑上相关的一组 WebLogic Server 资源,可以作为一个单元进行管理.一个域中包含一台或多台 WebLogic Server,也可以包含 WebLog ...