hdu 1151 Air Raid(二分图最小路径覆盖)

时间:2022-08-31 12:18:20

http://acm.hdu.edu.cn/showproblem.php?pid=1151

Air Raid
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7469   Accepted: 4451

Description

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets form no cycles.

With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.

Input

Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:

no_of_intersections 
no_of_streets 
S1 E1 
S2 E2 
...... 
Sno_of_streets Eno_of_streets

The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.

There are no blank lines between consecutive sets of data. Input data are correct.

Output

The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town. 

Sample Input

2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3

Sample Output

2
1

最小路径覆盖 = 总点数 - 最大匹配数

最小路径覆盖:

用最少的路径覆盖所有点(把有向图转化为二分图+无向边):

(前提是没有环的有向图)转化为二分图后既为无向边,从左边点到右边点表示了方向。

最终找到的匹配边对,代表了右边的点可以由其他未被选中的右边序号的点所到达,

因此,用总点数-匹配的对数 = 需要作为出发的点数。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define N 310
#define INF 0x3f3f3f3f using namespace std; int G[N][N], vis[N], used[N];
int n; bool Find(int u)
{
int i;
for(i = ; i <= n ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} int main()
{
int m, i, a, b, t, ans;
scanf("%d", &t);
while(t--)
{
ans = ;
memset(G, , sizeof(G));
scanf("%d%d", &n, &m);
while(m--)
{
scanf("%d%d", &a, &b);
G[a][b] = ;
}
memset(used, , sizeof(used));
for(i = ; i <= n ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", n - ans);
}
return ;
}

hdu 1151 Air Raid(二分图最小路径覆盖)的更多相关文章

  1. HDU 1151 Air Raid(最小路径覆盖)

    题目大意: 有n个城市,m条道路,城市的道路是单向.  现在我们的伞兵要降落在城市里,然后我门的伞兵要搜索所有道路.问我们最少占领多少个城市就可以搜索所有的道路了. 我们可以沿着道路向前走到达另一个城 ...

  2. HDU - 1151 Air Raid (最小路径覆盖)

    题意:给定一个有向无环图,求最少划分几条路径,使之能够覆盖所有点. 分析:这可以转化为DAG上的最小路径覆盖问题. 路径覆盖的定义:有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且 ...

  3. hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 题目大意: 城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少 ...

  4. Air Raid(最小路径覆盖)

    Description Consider a town where all the streets are one-way and each street leads from one interse ...

  5. HDU 6311 Cover (无向图最小路径覆盖)

    HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...

  6. &lbrack;bzoj2150&rsqb;部落战争&lowbar;二分图最小路径覆盖

    部落战争 bzoj-2150 题目大意:题目链接. 注释:略. 想法: 显然是最小路径覆盖,我们知道:二分图最小路径覆盖等于节点总数-最大匹配. 所以我们用匈牙利或者dinic跑出最大匹配,然后用总结 ...

  7. Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

    Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...

  8. hdu - 1151 Air Raid&lpar;有向无环图的最小路径覆盖&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 在一个城市里有n个地点和k条道路,道路都是单向的,并且不存在环.(DAG) 现在伞兵需要去n个地点视察,伞 ...

  9. &lpar;step6&period;3&period;4&rpar;hdu 1151&lpar;Air Raid——最小路径覆盖&rpar;

    题意:     一个镇里所有的路都是单向路且不会组成回路. 派一些伞兵去那个镇里,要到达所有的路口,有一些或者没有伞兵可以不去那些路口,只要其他人能完成这个任务.每个在一个路口着陆了的伞兵可以沿着街去 ...

随机推荐

  1. 深入解读Linux与Android的相互关系(转-lining)

    大家都知道Android是基于Linux内核的操作系统,也曾经和Linux基金会因为内核问题产生过分歧,本文将开始对Android的内核进行剖析,主要介绍Android和Linux之间的关系,后续还会 ...

  2. Hark的数据结构与算法练习之奇偶排序

    算法说明 奇偶排序又叫奇偶换位排序,砖排序.它是一种交换排序,也是冒泡的一个变种 顾名思义,奇偶排序,其实就是先循环奇数位,然后将奇数位与偶数位比较计算. 然后再循环偶数位,再和奇数位比较运算.看一下 ...

  3. namespace用法

    1.在WCF.Controller中定义了一个UserModel,标记为① 2.在WCF.Controller.Model中定义了一个UserModel(同上,namespace不同),标记为② 3. ...

  4. mybatis 无法转换为内部表示 解决

    出现这个问题,一般是JavaBean定义的时候数据类型不准造成的. 其中javabean定义的字段的类型并不需要完全和数据库中的字段一样. 在mapper.xml中可能做了转换  比如 CASE WH ...

  5. LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  6. PushMeBaby 使用

    github 下载地址 https://github.com/stefanhafeneger/PushMeBaby 1.执行假设报错,那么导入CoreServices.framawork 替换这句 # ...

  7. Redis简介 Linux安装Redis Redis使用

    其他一些操作(包括 APPEND.GETRANGE.MSET 和 STRLENGTH 也可用于字符串.请参见http://doc.redisfans.com/string/index.html ) 使 ...

  8. &lbrack;转&rsqb; 一张图理解prototype、proto和constructor的三角关系

    前面的话 javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor ...

  9. set的经典应用

    set的经典应用 刚开始用map标记一个测试点超时了 ┭┮﹏┭┮: 用set的find 减少了循环提高了效率 #include <bits/stdc++.h>using namespace ...

  10. 题解——洛谷P3390 【模板】矩阵快速幂(矩阵乘法)

    模板题 留个档 #include <cstdio> #include <algorithm> #include <cstring> #define int long ...