poj 1463(树形DP)

Strategic game

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 7584   Accepted: 3518

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

题意:一个游戏有n个点,在某些点上放置哨兵,每个哨兵可以监控和它有边相连的点,问监视所有的点需要的最少的哨兵数.

分析:树形DP,dp[i][0]表示第i个点不放哨兵需要的以i为根节点的子树最少需要的哨兵,dp[i][1]代表第i个点放哨兵需要的以i为根节点的子树最少需要的哨兵

如果某一点不放哨兵,那么他的子节点都需要设一个哨兵。

如果某一点放哨兵,那么就从子节点放还是不放中选最小的.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 1505
using namespace std;

int head[N];
struct Edge{
    int u,v,next;
}edge[N];
void addEdge(int u,int v,int &k){
    edge[k].u = u,edge[k].v = v;
    edge[k].next = head[u];head[u] = k++;
}
int dp[N][2];
int indegree[N];
void dfs(int u){
    for(int k = head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v;
        dfs(v);
        dp[u][0]+=dp[v][1]; ///父亲不放,孩子必放
        dp[u][1]+=min(dp[v][0],dp[v][1]); ///父亲放了,孩子在放与不放之间选哨兵数小的
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(indegree,0,sizeof(indegree));
        memset(head,-1,sizeof(head));
        int u,v,t;
        int tot=0;
        for(int i=0;i<n;i++){
            dp[i][0] = 0;
            dp[i][1] = 1;
            scanf("%d:(%d)",&u,&t);
            for(int j=0;j<t;j++){
                scanf("%d",&v);
                addEdge(u,v,tot);
                indegree[v]++;
                //printf("%d %d\n",u,v);
            }
        }
        int root;
        for(int i=0;i<n;i++) if(indegree[i]==0) {root=i;break;}
        //printf("%d\n",root);
        dfs(root);
        printf("%d\n",min(dp[root][0],dp[root][1]));
    }
    return 0;
}
时间: 2024-10-13 10:40:58

poj 1463(树形DP)的相关文章

Strategic game(POJ 1463 树形DP)

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

poj 1463 树形dp入门

很简单的树形dp题目,转移方程是: dp[u][0] += dp[v][1];     dp[u][1] += min( dp[v][0], dp[v][1] ); 其中u是v的父亲节点. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int N = 1500; 8 b

poj 1463树形dp 树的最小覆盖

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> using namespace std; // push_back inline int read(){ int sum=0,x=1; char ch=getchar(); while(ch<'0'||ch>'9

poj 3342(树形dp)

题意:在一个公司中要举办一个聚会,每一个员工有一个奉献值.为了和谐规定直接上下级不能一起出席.让你找出奉献值之和最大为多少. 思路:dp[v][1]表示当前结点选,能获得的最大奉献值,dp[v][0]表示当前节点不选能获得的最大奉献值.状态转移: dp[v][0] = max(dp[v][0], ∑max(dp[x][1], dp[x][0]))x为直接儿子 dp[v][1] = max(dp[v][1], ∑dp[x][0] + vex[v]) 最后答案是max(dp[root][0], dp

poj 1947(树形dp)

题意:一棵树上问你最少切掉几条边使得能分割出一个结点数正好为k的子树. 思路:dp[i][j]表示以i为根切掉j个结点最少要几条边. dp[v][j] = min(dp[v][j], dp[v][j-k] + dp[x][k]); 代码如下: 1 dp[v][j] = min(dp[v][j], dp[v][j-k] + dp[x][k]); 2 } 3 } 4 } 5 } 6 } 7 return vex[v]; 8 } 9 10 int main() 11 { 12 // freopen("

POJ 2486 树形DP

有一颗苹果树,每个节点上面有很多苹果,从一个节点到另外一个可以到达的节点花费1步,求k步最多能吃到多少苹果,起始点为1,可以不回到起始点. 这是典型的回溯型树状dp. dp[i][j][0]代表以i为根节点的子树最多j步后回到i能吃到的最多的苹果, dp[i][j][1]代表以i为根节点的子树最多j步后不回到i节点最多能吃到的子树.那么状态转移就分三步了. (1)dp[i][j+2][0] = max(dp[i][j+2][0], dp[i][j-k][0]+dp[son][k][0]); (2

POJ 3342 树形DP入门题

题目意思和POJ2342一样,只是多加了一个条件,如果最大方案数唯一,输出Yes,不唯一输出No dp的是时候多加一个变量记录答案是否唯一即可 #include "stdio.h" #include "string.h" #include "vector" using namespace std; struct node { int fa; vector<int>child; }data[210]; struct comp { int

POJ 1947 树形DP入门题

给出N个点,N-1个关系,建出树形图,问最少减去几个边能得到节点数为P的树.典型树形DP题 dp[cur][j] :记录cur结点,要得到一棵j个节点的子树去掉的最少边数 转移方程用的背包的思想 对当前树的每一个子树进行计算 砍掉此子树:   dp[cur][j]=dp[cur][j]+1; 不砍掉:           for (l=0;l<=j;l++)  dp[cur][j]=Min(dp[cur][j],dp[cur][l]+dp[next][j-l]); 枚举从该树中留l个节点其他由新

POJ 2342 树形DP入门题

有一个大学的庆典晚会,想邀请一些在大学任职的人来参加,每个人有自己的搞笑值,但是现在遇到一个问题就是如果两个人之间有直接的上下级关系,那么他们中只能有一个来参加,求请来一部分人之后,搞笑值的最大是多少. 树形DP入门题. DP部分: dp[i][0]表示职员i不来参加party,以i为根的子树的最大搞笑值, dp[i][1]表示职员i来参加party,以i为根的子树的最大搞笑值. 转移方程: dp[cur][1]+=dp[next][0]; dp[cur][0]+=Max(dp[next][1]