题目链接: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. RobotFramework:切换页面和Frame框架

    切换页面主要有以下两种情况 在浏览器上打开多个窗口(Windows),在窗口内切换 打开多个浏览器(Browser),在多个浏览器内切换 1. 切换窗口 该操作适用于:打开两(多)个窗口页面,在打开的 ...

  2. Linux 下Python2.7解决list打印中文字符问题

    在写一个爬取智联招聘数据的爬虫中,将所需内容匹配到后打印出现了utf-8字符,并没有出现中文字符. 例如: >>>listnine = ['梨', '橘子', '苹果', '香蕉'] ...

  3. Binary mod and divide(模拟+大数)

    描述 Most people know that the binary operations. Do you know the binary mod and divide? Now give the ...

  4. 数据库连接 Mysqli

    //数据库连接 Mysqli $db= new Mysqli("localhost","root","root","asd_808 ...

  5. FZU-2148-Moon Game,,几何计算~~

    Problem 2148 Moon Game Time Limit: 1000 mSec Memory Limit : 32768 KB  Problem Description Fat brothe ...

  6. bzoj4027 [HEOI2015]兔子与樱花 树上贪心

    [HEOI2015]兔子与樱花 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1320  Solved: 762[Submit][Status][Di ...

  7. 2017"百度之星"程序设计大赛 - 初赛(A)数据分割

    n<=100000条相等/不等关系描述<=100000个数,把这些数据分割成若干段使得每一段描述都出现冲突且冲突只出现在最后一行. 相等关系具有传递性,并查集维护:不等关系根据相等关系进行 ...

  8. IE下IFrame引用跨域站点页面时,Session失效问题解决

    问题场景:在一个应用(集团门户)的某个page中, 通过IFrame的方式嵌入另一个应用(集团实时监管系统)的某个页面. 当两个应用的domain 不一样时, 在被嵌入的页面中Session失效.(s ...

  9. HDU 6333 莫队+组合数

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  10. 集群架构和CentOS7安装RabbitMQ集群(单机版)

    1. 集群架构 1.1 四种内部元数据 队列元数据.交换器元数据.绑定元数据.vhost元数据. 单一节点中:会将数据存储到内存,同时将持久化元数据保存到硬盘. 集群中: 存储到磁盘上.内存中. 集群 ...