UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)

该博客介绍了如何利用二分图最小顶点覆盖或树形动态规划(DP)策略来帮助Bob解决一个计算机游戏问题。Bob需要在构成树形结构的中世纪城市道路上布置最少数量的士兵,确保他们能监视所有道路。博客通过举例说明,当给定特定树形结构时,如何找到所需的最小士兵数。例如,对于某个特定的树形结构,解决方案只需要部署一名士兵(位于节点1)。
摘要由CSDN通过智能技术生成

Strategic game

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


题意:给定一棵树,选择尽量少的点,使得每个没有选中的结点至少和一个已经选中的结点相邻。输出最少需要选择的节点数。

思路:经典的二分图最小顶点覆盖, 也是经典的树形 DP 。

最小顶点覆盖 == 最大匹配(双向图)/2
数据较大,用邻接表。不然会超时。


<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAXN = 1510;
int nx, ny;
int used[MAXN];
int cx[MAXN], cy[MAXN];
vector<int>g[MAXN];

int Find(int u)
{
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(!used[v])
        {
            used[v] = 1;
            if(cy[v]==-1 || Find(cy[v]))
            {
                cy[v] = u;
                cx[u] = v;
                return 1;
            }
        }
    }
    return 0;
}

int Hungary()
{
    int res = 0;
    memset(cx, -1, sizeof(cx));
    memset(cy, -1, sizeof(cy));
    for(int i = 0; i < nx; i++)
    {
        if(cx[i] == -1)
        {
            memset(used, 0, sizeof(used));
            if(Find(i))
                res++;
        }
    }
    return res;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int t, n, num;
    int x, y;
    while(cin>>n)
    {
        if(!n)
            break;
        ny = nx = n;
        t = n;
        for(int i = 0; i < n; i++)
            g[i].clear();
        while(t--)
        {
            scanf("%d:(%d)", &x, &num);
            while(num--)
            {
                scanf("%d", &y);
                g[x].push_back(y);
                g[y].push_back(x);
            }
        }
        printf("%d\n", Hungary()/2);
    }
    return 0;
}</span>


树形DP解法:

dp[u][0]:  表示不选 i 结点,覆盖这个子树所需的最少点
dp[u][1]:表示选 i 结点,覆盖这个子树所需的最少点

状态转移方程:

u 到 v 有一条无向边,v 是 u 的子结点
dp[u][1] = sum{  min{dp[u][0]dp[u][1]  }
dp[u][0] = sum{  dp[u][1]  }



#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAXN = 1510;
int nx, ny;
int vis[MAXN];
int cx[MAXN], cy[MAXN];
vector<int>g[MAXN];
int dp[MAXN][MAXN];

void dfs(int u)
{
    vis[u] = 1;
    dp[u][0] = 0;
    dp[u][1] = 1;
    for(int j = 0; j < g[u].size(); j++)
    {
        int v = g[u][j];
        if(!vis[v])
        {
            dfs(v);
            dp[u][0] += dp[v][1];
            dp[u][1] += min(dp[v][0], dp[v][1]);
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int t, n, num;
    int x, y;
    while(cin>>n)
    {
        if(!n)
            break;
        ny = nx = n;
        t = n;
        for(int i = 0; i < n; i++)
            g[i].clear();
        while(t--)
        {
            scanf("%d:(%d)", &x, &num);
            while(num--)
            {
                scanf("%d", &y);
                g[x].push_back(y);
                g[y].push_back(x);
            }
        }
        memset(dp, 0, sizeof(dp));
        memset(vis, 0, sizeof(vis));
        dfs(0);
        printf("%d\n", min(dp[0][0], dp[0][1]));
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值