题目链接: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. MyBatis 3 学习

    MyBatis是一款优秀的持久化框架,支持定制化SQL.存储过程以及高级映射.MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获得结果集.MyBatis可以使用简单的XML或注解来配置和映 ...

  2. python接口自动化-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

  3. zoj 1109 Language of FatMouse(map)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  4. 【转】关于大型网站技术演进的思考(十九)--网站静态化处理—web前端优化—上(11)

    网站静态化处理这个系列马上就要结束了,今天我要讲讲本系列最后一个重要的主题web前端优化.在开始谈论本主题之前,我想问大家一个问题,网站静态化处理技术到底是应该归属于web服务端的技术范畴还是应该归属 ...

  5. SpringMVC(8) - 处理器映射

    在以前的Spring版本中,用户需要在Web应用程序上下文中定义一个或多个HandlerMapping bean,以将传入的Web请求映射到适当的处理器.通过引入带注解的控制器,就不需要像之前那样定义 ...

  6. 为什么utf8占用3个字节

    UNICODE是万能编码,包含了所有符号的编码,它规定了所有符号在计算机底层的二进制的表示顺序.有关Unicode为什么会出现就不叙述了,Unicode是针对所有计算机的使用者定义一套统一的编码规范, ...

  7. dsu on tree:关于一类无修改询问子树可合并问题

    dsu on tree:关于一类无修改询问子树可合并问题 开始学长讲课的时候听懂了但是后来忘掉了....最近又重新学了一遍 所谓\(dsu\ on\ tree\)就是处理本文标题:无修改询问子树可合并 ...

  8. 2017多校Round7(hdu6120~hdu6132)

    补题进度:9/13 1001 待填坑 1002(数学推导) 题意 有一个按顺序的n个点的k叉树,问每个点子树个数的异或和是多少(n,k<=1e18) 分析 可以先求出最大的d,满足d以上都是满K ...

  9. Spring Security教程(5)---- 国际化配置及UserCache

    这一章是为了给后面的讲解打基础的,主要介绍下国际化的配置及UserCache的配置及使用 国际化配置 <!-- 定义上下文返回的消息的国际化 --> <bean id="m ...

  10. 磁盘显示为GPT(保护分区)

    问题描述:PE进入系统,在计算机管理里面磁盘显示为GPT(保护分区).此时硬盘是不能重新分区或者格式化的. 解决思路:低版本的WIndows(PE)是不支持GPT分区的,我们需要使用系统自带的Disk ...