Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?

Input

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output

For each test of the input, print a line containing the least robots needed.

Sample Input

1 0
2 1
1 2
2 0
0 0

Sample Output

1
1
2
题意:在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。
思路:这是个最小路径覆盖问题,但是因为有的点可以重复访问,所以最小路径是可以相交的,我们就用传递闭包建立新图(G’),转化为一般的路径覆盖,然后就是跟 poj1422 一样了。
   最小路径覆盖 = 图的顶点数 – 最大匹配数,所以只要用匈牙利算法求出最大匹配数,然后用顶点数一减就出来了。
AC代码:
 #include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 666
int match[maxn];
int vis[maxn];
int e[maxn][maxn];
int n,m;
int dfs(int u){
for(int i=;i<=n;i++){
if(!vis[i]&&e[u][i]){
vis[i]=;
if(match[i]==||dfs(match[i])){
match[i]=u;
return ;
}
}
}
return ;
}
void floyd(){
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(e[i][k]&&e[k][j])
e[i][j]=;
}
int main(){
while(~scanf("%d%d",&n,&m)&&(n+m)){
int x,y;
memset(match,,sizeof(match)); for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
e[x][y]=;
}
floyd();
int ans=;
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
int res=n-ans;
printf("%d\n",res);
for(int i=;i<=maxn;i++)
for(int j=;j<=maxn;j++)
e[i][j]=;
}
return ;
}
												

Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题的更多相关文章

  1. K - Treasure Exploration - POJ 2594(最小路径覆盖+闭包传递)

    题意:给一个有向无环图,求出来最小路径覆盖,注意一个点可能会被多条路径重复 分析:因为有可能多条路径走一个点,可又能会造成匹配的不完全,所以先进行一次闭包传递(floyd),然后再用二分匹配的方法求出 ...

  2. poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)

    1 #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> ...

  3. Treasure Exploration POJ - 2594(最小边覆盖)

    因为是路  所以 如果 1——3  2——3    3——4   3——5 则 1——4  1——5  2——4   2——5 都是是合法的 又因为机器人是可以相遇的  所以 我们把所有的点 分别放在 ...

  4. loj 1429(可相交的最小路径覆盖)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...

  5. POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】

    题目链接:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K T ...

  6. poj 2594(可相交的最小路径覆盖)

    题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...

  7. POJ 1422 Air Raid(二分图匹配最小路径覆盖)

    POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...

  8. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. POJ:3020-Antenna Placement(二分图的最小路径覆盖)

    原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...

随机推荐

  1. redis 基本数据类型及使用

    文章目录 相对其它 nosql 数据库的优势 杂项知识 基本数据类型 Key 关键字 String 类型(单键单值) List 类型 (单键多值) Set 类型 (单键多值) Hash类型 (KV模式 ...

  2. 剑指offer58:对称的二叉树。判断一颗二叉树是不是对称的,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的

    1 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 2 思路和方法 定义一种遍历算法,先遍历右子结点再遍历左子结点:如对称先序 ...

  3. PCA降维笔记

    PCA降维笔记 一个非监督的机器学习算法 主要用于数据的降维 通过降维, 可以发现更便 于人类理解的特征 其他应用:可视化:去噪 PCA(Principal Component Analysis)是一 ...

  4. JS 03事件

    <script type="text/javascript"> function getUserInput() { //获取用户输入的内容 var val = docu ...

  5. 如何知道外围器件的器件地址PHY_ADDR

    由于 S5P6818 现有的uboot没有网卡的初始化代码实现,在尝试移植网卡驱动而跟踪有关的源码的时候,不止一次地看到了类似下面的需要修改器件地址的有关写法. 根据原理图的有关硬件连接,找到对应的元 ...

  6. excelize

    // 参考:https://gitee.com/xurime/excelize // github.com/360EntSecGroup-Skylar/excelize

  7. 入手线段树 hdu1754

    今天学习了线段树的三个基本操作 建树 更新 查找 先理解下什么是线段树就这个题目而言 如果我们用普通的数组去存放 然后依次遍历访问的话 时间太多了线段树利用了二分的思想 把数据以段的形式进行储存 这样 ...

  8. 解决 Linux grep 不高亮显示

    今天偶然发现一个问题,在 grep 日志的过程中,搜出来一大坨但是被 grep 的那一段未高亮显示,属实有些难受,高亮显示是 Linux 的高亮本来就是 Linux 的功能,与连接工具(我用的 xsh ...

  9. TCP/IP及http协议 SOAP REST

    TCP/IP及http协议: TCP/IP协议主要解决数据如何在网络中传输, 而HTTP是应用层协议,主要解决如何包装数据 SOAP:简单对象访问协议(Simple Object Access Pro ...

  10. 使用element-ui中的el-upload组件时携带其他参数

    解决方法:// template <el-upload action="/api/oss/file/add" :headers="headers" // ...