题目地址:http://poj.org/problem?id=1463

题目:

Strategic game
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 7929   Accepted: 3692

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.

For example for the tree: 

the solution is one soldier ( at the node 1).

Input

The input 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_identifiernumber_of_roads 
    or 
    node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

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:

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

 
思路:明显树形dp嘛,然而写完一交,MLE的我不省人事,这题目居然这么变态卡内存,,
   我把记录边的vector改成链式前向星就ac了
   这个和上题思路一样,就不啰嗦了,可以去看看前一篇树形dp的
代码: 
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime> #define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
#define PI acos((double)-1)
#define E exp(double(1))
const int K=+;
short vis[K],head[K],cnt;
struct Edge
{
short next,to;
}edge[K*];
void add(int u,int v)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt++;
}
void dfs(int x,int &v1,int &v2)
{
v1=;v2=;
vis[x]=;
int x1,x2;
for(int i=head[x];~i;i=edge[i].next)
{
int v=edge[i].to;
if(vis[v])continue;
dfs(v,x1,x2);
v1+=min(x1,x2);
v2+=x1;
}
}
int main(void)
{
int n;
while(~scanf("%d",&n))
{
cnt=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
int u,v,k;
scanf("%d:(%d)",&u,&k);
while(k--)
scanf("%d",&v),add(u,v),add(v,u);
}
int x,y;
dfs(,x,y);
printf("%d\n",min(x,y));
}
return ;
}

poj 1463 Strategic game DP的更多相关文章

  1. POJ 1463 Strategic game(树形DP入门)

    题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...

  2. Strategic game POJ - 1463 树型dp

    //题意:就是你需要派最少的士兵来巡查每一条边.相当于求最少点覆盖,用最少的点将所有边都覆盖掉//题解://因为这是一棵树,所以对于每一条边的两个端点,肯定要至少有一个点需要放入士兵,那么对于x-&g ...

  3. poj 1463 Strategic game

    题目链接:http://poj.org/problem?id=1463 题意:给出一个无向图,每个节点只有一个父亲节点,可以有多个孩子节点,在一个节点上如果有一位战士守着,那么他可以守住和此节点相连的 ...

  4. POJ 1463 Strategic game(二分图最大匹配)

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

  5. POJ - 1463 Strategic game (树状动态规划)

    这题做的心塞... 整个思路非常清晰,d[i][0]表示第i个结点不设置监察的情况下至少需要的数量:d[i][1]表示第i个结点设置检查的情况下的最小需要的数量. 状态转移方程见代码. 但是万万没想到 ...

  6. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  7. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  8. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  9. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

随机推荐

  1. 转载 教你使用PS来制作unity3D随机地形

  2. nginx服务器是怎么执行php脚本的?

    简单的说: fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解 析php脚本,即php-fpm相 当于一个动态应用服务器,从而实现nginx动态解析 ...

  3. 高效构建Web应用 教你玩转Play框架 http://www.anool.net/?p=577

    Play 框架是一个完整的Web应用开发框架,覆盖了Web应用开发的各个方面.Play 框架在设计的时候借鉴了流行的 Ruby on Rails 和 Grails 等框架,又有自己独有的优势.使用 P ...

  4. Android文件操作

    将数据写入Internal Storage: String fileName = "myfile.txt"; String str="保存数据到内部存储"; t ...

  5. Android 开发组件

    每一个应用程序都有自己独立的运行沙盒(授予应用程序代码的访问权) Android操作系统是一个多用户的Linux系统,其中的每一个应用程序都是一个独立的用户. 系统会为每一个应用程序分配一个唯一的Li ...

  6. BI笔记-SSAS部署的几种方式及部署后的SSAS刷新

    SSAS的部署方式在哥本哈士奇的博客:BI笔记之--- SSAS部署的几种方式已经介绍了四种方式,在这里再介绍一种比较常用的快速部署方式. 环境约定:SQL Server 2008 R2 示例库:Ad ...

  7. Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

    Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Th ...

  8. mongodb--java操作

    一.mongodb客户端 mongodb提供诸多语言的客户端,也包括java的客户端.通过这些客户端,我们可以很方便地使用编写代码的方式对mongodb进行操作.这里使用java客户端进行示例.使用j ...

  9. PL/SQL之--函数

    一.函数 函数是作为数据库对象存储在oracle数据库中,函数又被称为PL/SQL子程序.oracle处理使用系统提供的函数之外,用户还可以自己定义函数.函数通常被作为一个表达式来调用或存储过程的一个 ...

  10. 更改ubuntu下mysql的密码

    1.首先,进入环境中去,即 mysql  -u  root  -p   ,然后输入原始密码 2.此时会出现 mysql > 3.开始修改密码: mysql >   use  mysql ; ...