POJ 1463 Strategic game(二分图最大匹配)

本文介绍了一种使用最小顶点覆盖算法解决迷宫守卫问题的方法,通过实例展示如何找到最少数量的守卫来观察所有路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

 

题目大意:给一棵树,求最小顶点覆盖(即用最小的点覆盖所有的边)。

思路:DP靠边。树肯定是二分图无误,在二分图中,最小顶点覆盖=最大匹配,直接匈牙利跑出答案。

 

代码(375MS):

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int MAXN = 1510;
 8 const int MAXE = MAXN << 1;
 9 
10 int head[MAXN];
11 int to[MAXE], next[MAXE];
12 int n, ecnt;
13 
14 void init() {
15     memset(head, 0, sizeof(head));
16     ecnt = 2;
17 }
18 
19 void add_edge(int u, int v) {
20     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
21     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
22 }
23 
24 int link[MAXN], dep[MAXN];
25 bool vis[MAXN];
26 
27 bool dfs(int u) {
28     for(int p = head[u]; p; p = next[p]) {
29         int &v = to[p];
30         if(vis[v]) continue;
31         vis[v] = true;
32         if(!~link[v] || dfs(link[v])) {
33             link[v] = u;
34             return true;
35         }
36     }
37     return false;
38 }
39 
40 int main() {
41     while(scanf("%d", &n) != EOF) {
42         init();
43         memset(dep, 0, sizeof(dep));
44         for(int i = 0; i < n; ++i) {
45             int a, b, c;
46             scanf("%d:(%d)", &a, &b);
47             while(b--) {
48                 scanf("%d", &c);
49                 dep[c] = dep[a] + 1;
50                 add_edge(a, c);
51             }
52         }
53         int ans = 0;
54         memset(link, 255, sizeof(link));
55         for(int i = 0; i < n; ++i) {
56             if(dep[i] & 1) continue;
57             memset(vis, 0, sizeof(vis));
58             if(dfs(i)) ++ans;
59         }
60         printf("%d\n", ans);
61     }
62 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/3297878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值