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
一般的树形DP;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
vector<int>v[1500];
int vis[1500];
int dp[1500][2];
int n,m;
void dfs(int x)
{
if(vis[x]) return ;
vis[x]=1;
dp[x][1]=1;
dp[x][0]=0;
for(int i=0;i<v[x].size();i++)
{
if(!vis[v[x][i]])
{
dfs(v[x][i]);
dp[x][0]+=dp[v[x][i]][1];
dp[x][1]+=min(dp[v[x][i]][1],dp[v[x][i]][0]);
}
}
}
int main()
{
int x,y,temp;
while(~scanf("%d",&n))
{
REP(i,n)
v[i].clear();
REPF(i,1,n)
{
scanf("%d:(%d)",&x,&y);
while(y--)
{
scanf("%d",&temp);
v[x].push_back(temp);
v[temp].push_back(x);
}
}
CLEAR(vis,0);
dfs(1);//因为是无向图能够在0~n中随变找一个点
printf("%d\n",min(dp[1][1],dp[1][0]));
}
return 0;
}

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)

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

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

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

  7. Strategic game(树形DP入门)

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

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

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

  9. POJ1463:Strategic game(树形DP)

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

随机推荐

  1. DOM中的动态NodeList与静态NodeList

    GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...

  2. 怎样用Google APIs和Google的应用系统进行集成(1)----Google APIs简介

    Google的应用系统提供了非常多的应用,比方 Google广告.Google 任务,Google 日历.Google blogger,Google Plus,Google 地图等等非常的多的应用,请 ...

  3. 全面剖析Redis Cluster原理和应用 (转)

    1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最核心的目标有三个: 性能:这是Redis赖以生 ...

  4. 左右mysql事务提交

    package com.itheima.trans; import java.sql.Connection; import java.sql.PreparedStatement; import jav ...

  5. poj2348(博弈)

    poj2348 给定两个数a,b,大的数能减少小的数的倍数,不能是的数小于0,谁先使得数等于0,谁就赢了 有三种情况 ① a % b ==0  这个状态是必胜的 ② a - b < b  这个状 ...

  6. uptime

    linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行 ...

  7. Android asynctask使用

    继承asynctask,有三个參数 三个參数的含义是第一个表示输入參数.第二个为progress,表示当前的进度,第三个为doInbackground    返回值 须要一个參数传入url,返回一个r ...

  8. Add/Remove listview web part in publish site via powershell

    1. Here is the code: Add WebPart in Publish Site Example : AddWebPartPublish http://localhost  " ...

  9. Gitblit配置

    Gitblit的安装配置及访问-windows (2013-09-11 11:52:31) 转载▼   分类: android基础 Git 是现在很流行的分布式版本控制工具,github更是人人皆知. ...

  10. ASP.NET回车提交事务

    浅析ASP.NET回车提交事件[转] ASP.NET回车提交事件其实说到底并不是ASP.NET 的编程问题,却是关于html form 中的submit 按钮就是如何规划的具体讨论. 也可归于ASP. ...