Strategic game POJ - 1463 【最小点覆盖集】

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。题意:在无向图中,在某一点放置士兵,该士兵可以守护与该点相邻的其他点,求出要保护所有点的最少士兵数。思路:求最小点覆盖。。最小点覆盖数 = 最大匹配数AC代码:
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>

using namespace std;
#define maxn 2500
int vis[maxn];
int match[maxn];
vector<int> v[maxn];
int n;
int dfs(int u){ // 匈牙利模板
    for(int i=0;i<v[u].size();i++){
        int temp=v[u][i];
        if(vis[temp]==0){
            vis[temp]=1;
            if(match[temp]==0||dfs(match[temp])){
                match[temp]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main(){
    while(~scanf("%d",&n)){
        for(int i=0;i<=n;i++)
            v[i].clear();
        for(int i=1;i<=n;i++){
            int x,m,y;
            scanf("%d:(%d)",&x,&m);
            for(int j=0;j<m;j++){
                scanf("%d",&y);
                v[x].push_back(y);
                v[y].push_back(x);
            }
        }
        memset(match,0,sizeof(match));
        int ans=0;
        for(int i=0;i<n;i++){
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans/2);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/pengge666/p/11624407.html

时间: 2024-10-19 15:35:09

Strategic game POJ - 1463 【最小点覆盖集】的相关文章

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

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

POJ 3401 Asteroids 求最小点覆盖集

把行和列都看做是点,小行星看成是边的话,那么这个显然就是求一个最小点覆盖集的问题. 最小点覆盖 == 最大匹配 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #inc

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

B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处理的. 这个树形dp是从底往根部来递推,所以每一个点,都是由它的根节点来递推的. 如果一个根节点的子节点放了东西,那么这个根节点就可以有选择,但是如果没有放东西,那么这个根节点就必须放东西. E - Cell Phone Network POJ - 3659 题目大意:给你一棵树,让你用最小的东西来覆盖所有

POJ2226 Muddy Fields(二分图最小点覆盖集)

题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作为XY部,每一块泥地看作边.这样就构造出了一个二分图. 那么,问题就是在这个二分图中就是选出最少的点覆盖所有的边,即二分图最小点覆盖集,而二分图最小点覆盖集=二分图最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<qu

Jewelry Exhibition(最小点覆盖集)

Jewelry Exhibition 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry exhibition at night, the security agency has decided to use a new laser beam system, consisting of sender-receiver pairs. Each pair generates a strip o

ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

//匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define MAX 105 #define INF 0x3f3f3f3f int n,m,k; int gp[MAX][MAX]; bool sx[MAX],s

Asteroids POJ - 3041 【最小点覆盖集】

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortun

poj1463 Strategic game 树的最小点覆盖

http://poj.org/problem?id=1463 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6413   Accepted: 2973 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast