UVALive 2038 - Strategic game (经典树形DP)

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

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 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_identifiernumber_of_roads
    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.

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).

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

[]   [Go Back]   [Status]  



题意:

给定一棵树,选择尽量少的节点,使得每个没有选择的节点至少和一个已选择的节点相邻

更正:

大白上面的翻译就是上面那样,题目的翻译下面这样。

给定一棵树,选择尽量少的节点放置士兵,使得每条边都能被士兵照顾到。

如果是大白翻译那样,这样的做法就是错的,比如


14 
0:(3) 1 2 3
1:(3) 4 5 6
2:(1) 7
3:(3) 8 9 10
4:(0)
5:(0)
6:(0)
7:(3) 11 12 13
8:(0)
9:(0)
10:(0)
11:(0)
12:(0)
13:(0)


这个画出来就可以看到

按照大白的题意答案应该是3

而根据题面的答案应该是4


经典树形DP

dp[i][j] 表示i节点有没有选择j=0没有选择j=1选择


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 1500 + 20;
vector<int> G[maxn];
int dp[maxn][2];

int f(int u, int sel, int fa) {
    if(dp[u][sel] != -1) return dp[u][sel];
    int & ret = dp[u][sel];
    ret = sel;
    for(int i=0; i<G[u].size(); i++) {
        int v = G[u][i];
        if(v == fa) continue;
        if(sel) ret += min(f(v, 0, u), f(v, 1, u));
        else ret += f(v, 1, u);
    }
    return ret;
}

int main() {
    int n;

    while(scanf("%d", &n) != EOF) {
        for(int i=0; i<n; i++) G[i].clear();
        for(int i=0; i<n; i++) {
            int u, m;
            scanf("%d:(%d)", &u, &m);
            for(int i=0; i<m; i++) {
                int v;
                scanf("%d", &v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
        }
        memset(dp, -1, sizeof(dp));
        int c0 = f(0, 0, -1);
        int c1 = f(0, 1, -1);
        int ans = min(c0, c1);
        printf("%d\n", ans);
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值