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. sharepoint2013配置开发环境

  2. 20.springboot项目部署到linux服务器文件上传临时路径处理问题

    1.前言 把项目部署到服务器上之后,文件上传默认会在/tmp路径中. 之前想了各种解决办法,比如如何更改这个上传路径...... 最后发现不是个好的方法,当然就想到了更好的解决方案. 就是我把上传文件 ...

  3. docker官方文档翻译3

    转载请标明出处: https://blog.csdn.net/forezp/article/details/80171723 本文出自方志朋的博客 第三部分: 服务 准备工作 安装Docker 1.1 ...

  4. android 省市区三级联动

    最近项目,需要用到三级联动,在网上找了一些例子,进行了修改,实现,提炼出来了给大家分享 实现思路是在三个wheelview 进行联动.选择了省,马上就关联到市和区,选择了市 ,马上就可以关联到区. 效 ...

  5. Oracle之视图

    Oracle之视图 2018.9.12 由于视图的数据与表数据互相关联,所以切记谨慎操作 建立视图 使用下面sql语句来完成视图的创建 create or replace view 视图名 as se ...

  6. 【JAVA】学习笔记

    对程序员来说,学习一门新的语言,最开始的代码当然是hello world!下面我们从这段代码入手,一步一步来学习Java基础知识. class Hello{ public static void ma ...

  7. 选择排序_C语言_数组

    选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...

  8. #leetcode刷题之路5-最长回文子串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1:输入: "babad"输出: "bab"注意: " ...

  9. layDate 闪现 循环一个以上会闪现

    一个render一次渲染一个日期组件,这个是内置的,所以需要循环绑定, 又不能确定页面有多少个,还好layDate 提供了内置方法, //同时绑定多个 lay('.test-item').each(f ...

  10. kali2.0 设置输入法 找了好久,亲测有效

    kali2.0更新源启用中文输入法 查看版本信(Version): uname -r uname -r 工具(Tools): fcitx fcitx fcitx-table-wbpy 更新源:(Sou ...