Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2925    Accepted Submission(s): 1222

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
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int brother,child;
int yes;//该点放士兵
int no; //该点不放士兵
}tree[];
int n,i,j,k,root,num,origin; void dfs(int root)
{
int child=tree[root].child;
while(child>)
{
dfs(child);
tree[root].yes+=min(tree[child].yes,tree[child].no);
//父亲结点放置了,儿子结点可以放置也可以不放置
tree[root].no+=tree[child].yes;
//父亲结点没有放置,儿子结点必须放置
child=tree[child].brother;
}
}
int main()
{
while(~scanf("%d",&n))
{
for(i=;i<=n;i++)
{
tree[i].brother=tree[i].child=;
tree[i].yes=;
tree[i].no=;
}
for(int t=;t<=n;t++)
{
scanf("%d:(%d)",&root,&num);
root++;
if (t==) origin=root;
for(i=;i<=num;i++)
{
int x;
scanf("%d",&x);
x++;
tree[x].brother=tree[root].child;
tree[root].child=x;
}
}
dfs(origin);
printf("%d\n",min(tree[origin].yes,tree[origin].no));
} return ;
}

代码二:

dproot[ i ]表示以i为根的子树,在i上放置一个士兵,看守住整个子树需要多少士兵。

all[ i ]表示看守住整个以i为根的子树需要多少士兵。

状态转移方程:

叶子节点: dproot[k] =1; all[k] = 0;

非叶子节点: dproot[i] = 1 + ∑all[j](j是i的儿子);

      all[i] = min( dproot[i], ∑dproot[j](j是i的儿子) );

#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=+;
int dproot[M],all[M];
int i,n,x,y,k;
vector<int> v[M];
void dfs(int x,int fa)
{
int tmp=;
for(int i=;i<v[x].size();i++)
{
int k=v[x][i];
if (k==fa) continue;
dfs(k,x);
dproot[x]+=all[k];
tmp=tmp+dproot[k];
}
all[x]=min(dproot[x],tmp);
return;
}
int main()
{
while(~scanf("%d",&n))
{
for(i=;i<n;i++)
{
v[i].clear();
all[i]=;
dproot[i]=;
}
for(int t=;t<=n;t++)
{
scanf("%d:(%d)",&x,&y);
for(i=;i<=y;i++)
{
scanf("%d",&k);
v[x].push_back(k);
v[k].push_back(x);
}
}
dfs(,-);
printf("%d\n",all[]);
}
return ;
}

  

Source

HDU 1054 Strategic Game(树形DP)的更多相关文章

  1. HDU 1054 Strategic Game (树形dp)

    题目链接 题意: 给一颗树,用最少的点覆盖整棵树. 每一个结点可以防守相邻的一个边,求最少的点防守所有的边. 分析: 1:以当前节点为根节点,在该节点排士兵守护道路的最小消耗.在这种情况下,他的子节点 ...

  2. hdu 1054 Strategic Game(tree dp)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. hdu1054 Strategic Game 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 思路:树形DP,用二分匹配也能解决 定义dp[root][1],表示以root 为根结点的子树且 ...

  4. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

  5. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

  6. hdu 1054 Strategic Game (简单树形DP)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

  8. Strategic game(树形DP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:一棵树,要放置哨兵,要求最少放置多少哨兵能监视到所有的结点 题目分析: 放置哨兵无非两 ...

  9. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  10. POJ1463:Strategic game(树形DP)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

随机推荐

  1. 高精度运算专题1-加法运算(The addition operation)

    这个专题呢,我就来讲讲高精度的加法,下面是一个计算加法的函数(用数组a加上数组b结果存到数组c里面). 思路:先测一下数组a和数组b的长度,分别放到a[0].b[0]里面去,再从第二位开始相加,记得满 ...

  2. 生成SQL脚本的方法

    2点需要注意的关键: (1)选择特定数据库对象不包含用户选项: (2)要编写脚本的数据的类型选择"架构和数据".

  3. finally语句包含return的情况

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  4. 711B - Chris and Magic Square 模拟

    题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...

  5. mongoDB3--mongoDB的基本操作。

    [MongoDb探究]03-mongodb基本操作语句 标签: mongodbdb.createCollectiondb.collectionName.indb.dropDatabasedb.coll ...

  6. Chapter 2 Open Book——3

    But when I walked into the cafeteria with Jessica — 但是当我和Jessica 一起走进自助餐厅的时候 trying to keep my eyes ...

  7. Web开发人员不要错过的60款用户界面设计工具(上)

    Web开发大师们,干货再次来袭!小编为大家盘点了60款功能丰富类型各异的用户界面设计工具,本系列将以上中下三篇分别为大家呈现.今天盘点的这20款工具囊括了大量界面原型设计工具,有免费的在线原型工具,有 ...

  8. 十三章:使用WEB字体

    1.WEB字体可以使用一系列文件类型,下面介绍三种字体类型: (1)内嵌OpenType (2)TrueType和OpenType台式机使用的标准字体文件类型 (3)WEB开放字体格式. 2.构造子集 ...

  9. Entity Framework技巧系列之十四 - Tip 56

    提示56. 使用反射提供程序编写一个OData Service 在TechEd我收到一大堆有关将数据作为OData暴露的问题. 到目前为止你大概知道可以使用数据服务与Entity Framework将 ...

  10. LeetCode OJ 99. Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...