Strategic Game
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.

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_identifier 
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.

For example for the tree:

the solution is one soldier ( at the node 1).

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

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)

Output

1
2

题意:在一棵树里面查找最小的点覆盖。

_______________________________________________________________________________________________________

求最小点覆盖,匈牙利算法。

匈牙利算法,求二分图最大匹配。

一些定义,自己的话:

二分图:可以把图中的点看出人,男女分成2组,男男,女女同性之间没有直接连边。

匹配:二分图中,2中中各取一个人,有边相连,就是一个匹配。就是两夫妻一个在这边一个在那边,这就是匹配,当然一夫一妻制。

最大匹配:最多可组成的夫妻对数就是了。

下面有一些定理(引自网络):

(1)二分图的最小顶点覆盖 

最小顶点覆盖要求用最少的点(X或Y中都行),让每条边都至少和其中一个点关联。

Knoig定理:二分图的最小顶点覆盖数等于二分图的最大匹配数。

 

(2)DAG图的最小路径覆盖

用尽量少的不相交简单路径覆盖有向无环图(DAG)G的所有顶点,这就是DAG图的最小路径覆盖问题。

结论:DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)

 

(3)二分图的最大独立集

最大独立集问题: 在N个点的图G中选出m个点,使这m个点两两之间没有边.求m最大值

结论:二分图的最大独立集数 = 节点数(n)— 最大匹配数(m)

匈牙利算法可以参考程序中的代码,如果看不懂(本人水平实在有限)可以参考http://blog.csdn.net/dark_scope/article/details/8880547 真正的通俗易懂!

双向边要除以2,切记!

_______________________________________________________________________________________________________

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=;
int n;
int head[maxn],js;
struct edge
{
int u,v,next;
}e[maxn*];
int link[maxn];
bool vis[maxn];
void init()
{
memset(e,,sizeof(e));
memset(head,,sizeof(head));
js=;
memset(link,-,sizeof(link));
}
void addage(int u,int v)
{
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
}
bool dfs(int u)
{
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!vis[v])
{
vis[v]=;
if(link[v]==- || dfs(link[v]))
{
link[v]=u;
return ;
}
}
}
return ;
}
int main()
{
while(scanf("%d",&n)==)
{
init();
for(int u,s,i=;i<n;i++)
{
scanf("%d:(%d)",&u,&s);
for(int v,j=;j<s;j++)
{
scanf("%d",&v);
addage(u,v);
addage(v,u);
}
}
int ans=;
for(int i=;i<n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))ans++;
}
printf("%d\n",ans/);
}
return ;
}

HDU1054 Strategic Game——匈牙利算法的更多相关文章

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

    //匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...

  2. 匈牙利算法——S.B.S.

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  3. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  4. poj1274(匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22809   Accepted: 101 ...

  5. 匈牙利 算法&模板

    匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...

  6. 【入门】匈牙利算法+HNOI2006 hero超级英雄

    一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...

  7. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  8. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  9. Poj(1466),最大独立集,匈牙利算法

    题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total S ...

随机推荐

  1. Esfog_UnityShader教程_遮挡描边(实现篇)

     在上一篇中,我们基本上说明了遮挡描边实现的一种基本原理.这一篇中我们将了解一下基于这种原理的具体实现代码.本篇中的内容和前几篇教程相比,相对比较难一些,建议先有一些基本的Unity的C#脚本编程经验 ...

  2. 【HEVC】1、HM-16.7编码器的基本结构

    编码器在整个HM解决方案中的工程名为TAppEncoder,入口点函数位于encmain.cpp文件中: int main(int argc, char* argv[]) { TAppEncTop c ...

  3. RDD常用方法之subtract&intersection&cartesian

    subtract Return an RDD with the elements from `this` that are not in `other` .     def subtract(othe ...

  4. sqlmap用户手册

    http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数2.判断可以用那种SQL注入 ...

  5. 转@ManyToMany- annotation关系映射篇(下)

    原文:http://blog.sina.com.cn/s/blog_6fef491d0100obdd.html 终于要说ManyToMany了 场景:Product和Customer. 先看TestP ...

  6. 移动web开发实践-css3(1)盒模型display:-webkit-box;的使用

    提到移动布局不得不提到盒模型display:-webkit-box;这个属性,在移动布局中浮动已经不在重要,相反自适应成为主要的需求,所以display:-webkit-box;变得尤为重要. box ...

  7. cs端调用Ajax

    private static string Descoder() { //ajax地址 string MealFilePath = "http://***/user/SetWebsite.a ...

  8. leetcode-【中等题】2. Add Two Numbers

    题目 You are given two linked lists representing two non-negative numbers. The digits are stored in re ...

  9. 从request获取远程IP地址

    public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-F ...

  10. 使用FIO对SATA、SSD和PCIe Flash进行测试

    首先声明,同事做的实验 使用fio对SATA.SSD.PCIE进行了测试 测试说明: 1.测试命名   sync_write_4k_32      sync表示测试方式,可以是sync或者libaio ...