poj 1463 Strategic game 树状dp

Strategic game

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 6607   Accepted: 3047

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

还是对树地守卫问题,不过这题是守卫所有的边。节点可以安排士兵,其能守卫相邻的边。求用最少的士兵,守卫所有的边。与皇宫守卫不同。这里只需要看节点是否安排守卫。如果不安排,则其所有子节点均要安排,如果安排守卫,则其子节点状态随意。

//dp[t][0/1] : 根节点为t的子树(0:根节点不安排守卫1:安排)的所有边被守卫的情况下的最小安排士兵数量。  

dp[t][0] = sum(dp[ti][1]);
dp[t][1] = sum(min(dp[ti][0], dp[ti][1])); 
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 999999999;

int n;
std::vector<int > v[2005];
bool vis[2005];
int dp[2005][2];

void Tdp(int t) {
    if (v[t].size() == 0) {
        dp[t][0] = 0;
        dp[t][1] = 1;
        return ;
    }
    for (int i=0; i<v[t].size(); i++) {
        Tdp(v[t][i]);
        dp[t][0] += dp[v[t][i]][1];
        dp[t][1] += min(dp[v[t][i]][0], dp[v[t][i]][1]);
    }
    dp[t][1]++;
}

void init() {
    memset(dp, 0, sizeof(dp));
    memset(vis, false, sizeof(vis));
    for (int i=0; i<1505; i++) {
        v[i].clear();
    }
}

int main () {
    while (scanf ("%d",&n)!=EOF) {
        init();
        for (int i=0; i<n; i++) {
            int a, b, c;
            scanf ("%d:(%d)", &a, &b);
            for (int j=0; j<b; j++) {
                scanf ("%d", &c);
                v[a].push_back(c);
                vis[c] = true;
            }
        }        int root;
        for (int i=0; i<n; i++) {
            if (!vis[i]) {
                root = i;
                break;
            }
        }
        Tdp(root);
        cout << min(dp[root][0], dp[root][1]) << endl;
    }
    return 0;
}

<!--话说这输入真他妈恶心-->

时间: 2024-10-28 19:10:43

poj 1463 Strategic game 树状dp的相关文章

POJ 2161 Chandelier(树状DP)

一.题意 首先是对题目的翻译.给出一个长长的字符串,这个字符串描述了一个吊灯.对于给字符串只有两种操作数--'a'为一个吊灯灯珠,将改灯珠入栈,一位阿拉伯数字K,代表一个环,将把该数字前面k位数都出栈并且穿成一个环,并将该环重新入栈(作为一个单元).由此可以得到一颗神奇的树--每个节点的若干子节点呈现循环数组的关系.因而此处有对于同构的定义为:再该环上各个小串的相对位置不变.于是,要求一个新的字符串,能够成上述字符转的一个同构的树,在这个基础上求出最小的"最大栈空间"大小. 二.思路

POJ 1463 树状dp

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6629   Accepted: 3058 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 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

POJ 2342 树状dp

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4606   Accepted: 2615 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

树状DP (poj 2342)

题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[i][0] += max(F[k][1], F[k][0]);(k为i的儿子) 第二种情况:第i个员工参与,F[i][1] += F[k][0]; F[i][j]表示第i个员工是否参与: 边界:F[i][0] = 0:F[i][1] = 其快乐指数: #include <iostream> #in

洛谷P1122 最大子树和 (树状dp)

题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明就向老师提出了这个问题: 一株奇怪的花卉,上面共连有N 朵花,共有N-1条枝干将花儿连在一起,并且未修剪时每朵花都不是孤立的.每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美丽指数”为负 数的,说明这朵花看着都让人恶心.所谓“修剪”,意为:去掉其中的一条枝条,这样一株花就成了两株,扔掉

poj1947--Rebuilding Roads(树状dp)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9496   Accepted: 4316 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan