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

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MAXE = MAXN << ; int head[MAXN];
int to[MAXE], next[MAXE];
int n, ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} int link[MAXN], dep[MAXN];
bool vis[MAXN]; bool dfs(int u) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(vis[v]) continue;
vis[v] = true;
if(!~link[v] || dfs(link[v])) {
link[v] = u;
return true;
}
}
return false;
} int main() {
while(scanf("%d", &n) != EOF) {
init();
memset(dep, , sizeof(dep));
for(int i = ; i < n; ++i) {
int a, b, c;
scanf("%d:(%d)", &a, &b);
while(b--) {
scanf("%d", &c);
dep[c] = dep[a] + ;
add_edge(a, c);
}
}
int ans = ;
memset(link, , sizeof(link));
for(int i = ; i < n; ++i) {
if(dep[i] & ) continue;
memset(vis, , sizeof(vis));
if(dfs(i)) ++ans;
}
printf("%d\n", ans);
}
}

POJ 1463 Strategic game(二分图最大匹配)的更多相关文章

  1. POJ - 1422 Air Raid 二分图最大匹配

    题目大意:有n个点,m条单向线段.如今问要从几个点出发才干遍历到全部的点 解题思路:二分图最大匹配,仅仅要一条匹配,就表示两个点联通,两个点联通仅仅须要选取当中一个点就可以,所以有多少条匹配.就能够减 ...

  2. POJ 3041 Asteroids(二分图最大匹配)

    ###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...

  3. POJ 2446 Chessboard(二分图最大匹配)

    题意: M*N的棋盘,规定其中有K个格子不能放任何东西.(即不能被覆盖) 每一张牌的形状都是1*2,问这个棋盘能否被牌完全覆盖(K个格子除外) 思路: M.N很小,把每一个可以覆盖的格子都离散成一个个 ...

  4. poj 1463 Strategic game DP

    题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS   Memory Limit: 10000K Tot ...

  5. [POJ] 3020 Antenna Placement(二分图最大匹配)

    题目地址:http://poj.org/problem?id=3020 输入一个字符矩阵,'*'可行,'o'不可行.因为一个点可以和上下左右四个方向的一个可行点组成一个集合,所以对图进行黑白染色(每个 ...

  6. [POJ] 2239 Selecting Courses(二分图最大匹配)

    题目地址:http://poj.org/problem?id=2239 Li Ming大学选课,每天12节课,每周7天,每种同样的课可能有多节分布在不同天的不同节.问Li Ming最多可以选多少节课. ...

  7. POJ - 3020  Antenna Placement 二分图最大匹配

    http://poj.org/problem?id=3020 首先注意到,答案的最大值是'*'的个数,也就是相当于我每用一次那个技能,我只套一个'*',是等价的. 所以,每结合一对**,则可以减少一次 ...

  8. poj 1463 Strategic game

    题目链接:http://poj.org/problem?id=1463 题意:给出一个无向图,每个节点只有一个父亲节点,可以有多个孩子节点,在一个节点上如果有一位战士守着,那么他可以守住和此节点相连的 ...

  9. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)

    System Engineer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 507   Accepted: 217 Des ...

随机推荐

  1. 分组函数group by和Oracle中分析函数partition by的用法以及区别

    1.分组函数group by和Oracle中分析函数partition by的用法以及区别 2.开窗函数.

  2. stl之std::remove_copy

    template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (Inpu ...

  3. Percona-Tookit工具包之pt-table-sync

      Preface       We've used pt-table-checksum to checksum the different table data bwtween replicatio ...

  4. JS小数运算失精度的问题

    JS因为是解释性语言,在运算中会有丢失精度的问题,这种现象多出现在浮点型运算的情况下. 例如 5.11 * 100  得到的结果是 511.00000000000006 这种情况尤其是在处理金额的时候 ...

  5. winform窗体传值和动态添加控件

    1.跳转窗体时传值 //将要显示的页面实例化 RoleMenuForm rmf = new RoleMenuForm(); try { //在此给RoleMenuForm 窗体中的变量roleId传值 ...

  6. 主流浏览器内核,以及CSS3前缀识别码

    现在国内常见的浏览器有:IE.Firefox.QQ浏览器.Safari.Opera.Google Chrome.百度浏览器.搜狗浏览器.猎豹浏览器.360浏览器.UC浏览器.遨游浏览器.世界之窗浏览器 ...

  7. MySQL数据库操作(DDL)

    一.创建数据库 语法:create database 数据库名称 [库选项]; 库选项:(可选)数据库的属性,一般有字符集与校对集,保存在数据库所属文件夹下的opt文件 charset:字符集,表示该 ...

  8. fabric Report API

    1.Token生成 接口 : post https://fabric.io/oauth/token 请求头:Headers Content-Type : application/json 正文: bo ...

  9. SpringMVC+Mybatis框架搭建

    一.新建javaweb项目,并建好相应的包结构 二.添加项目jar到lib目录下 三.在config包中新建配置文件 sping-mvc.xml,内容如下: <?xml version=&quo ...

  10. python 面向对象 (多态)

    什么是多态?多态就像是人有多种心情,场景不一样心情就会不一样. class Dog: def print_self(self): print('this is dog') class Hsq(Dog) ...