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. Office常用快捷键大全,包含 Word、Excel、PowerPoint

    功能 快捷键 查找文字.格式和特殊项 Ctrl+G 使字符变为粗体 Ctrl+B 为字符添加下划线 Ctrl+U 删除段落格式 Ctrl+Q 复制所选文本或对象 Ctrl+C 剪切所选文本或对象 Ct ...

  2. Vue基础知识学习(后端)

    ### Vue学习(后端) Vue安装 -引入文件安装,直接在官网下载vue.js引入项目中 -直接引用CDN -NPM安装(构建大型应用使用,在这不用) -命令行工具(构建大型单页应用,在这不用) ...

  3. Python 中文件操作

    上代码: import os import os.path rootdir = "d:/code/su/data" # 指明被遍历的文件夹 for parent,dirnames, ...

  4. 使用google的guova开发高并发下的接口限流

    使用google的guova开发高并发下的接口限流 使用google的guova进行限流 1.guova的限流方式,在定时产生定量的令牌,令牌的数量限制了流量 2.增加一个订单接口限流类OrderRa ...

  5. Android 把枪/PDA 扫描头自回车没用 处理方法

    XML 控件加上属性 android:imeOptions="actionNone"

  6. Linux报错排解

    1.Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registe ...

  7. 【问题】Debian安装、配置sources.list、安装VMware Tools

    Debian安装: 我采用的是纯命令行安装方式.具体安装过程网上一大堆,不介绍了.需要强调一点,那个SSH Server必须选,否则像XShell这样的客户端不能访问Debian. 配置sources ...

  8. C 全局变量 本地变量

  9. linux下无法删除文件的解决办法

    1.使用 lsattr 命令查看文件的附加属性.查看文件是否被赋予了 a , i 属性,如果含有这两个属性,文件是不能被删除的. a:让文件或目录仅供附加用途: b:不更新文件或目录的最后存取时间: ...

  10. 用正则表达式处理一个复杂字符串(类似json格式)

    #利用正则输出{}中的内容 str1="""var local=[{provinceCode:'310000',   cityCode:'310100',   text: ...