Description

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 最小路径覆盖:一个有向图(无向图也可),每次可以从任意一个点出发访问一条链,求覆盖整个图所用的最少次数。
建立匹配:将每个点 i 拆为 i 和 i',如果在原图中 i -> j 有边(有向边),那么在新图中连边 i -> j'
ans=边集大小V-匹配数
理解:首先决定用V次访问,每次访问都只访问一个点来完成覆盖任务。若i和j匹配,则认为到i的路径下一步经过j,那么每一个匹配都会减少一次访问。 本题还有些特殊,每个点都可以重复经过。
处理方法:认为一条链上i->j->k->p->q,i可以飞到k、p、q,j可以飞到p、q,其他同理
交了两遍,第一遍板子写错了呜呜呜
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
int N,M,lin[1010],len;
struct edge{
int y,next;
}e[250100];
bool f[510][510];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
}
void floyd(){
for(int i=1;i<=N;i++)
f[i][i]=1;
for(int k=1;k<=N;k++)
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
f[i][j]|=(f[i][k]&f[k][j]);
len=0;
memset(lin,0,sizeof(lin));
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j&&f[i][j])
insert(i,j+N);
}
int match[1010],vis[1010],tim,pretim,ans;
bool hun(int x){
for(int i=lin[x];i;i=e[i].next){
if(vis[e[i].y]<=pretim){
vis[e[i].y]=++tim;
if(match[e[i].y]==0||hun(match[e[i].y])){
match[x]=e[i].y;
match[e[i].y]=x;
return 1;
}
}
}
return 0;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
while(1){
N=read(),M=read();
if((!N)&&(!M))
break;
memset(f,0,sizeof(f));
for(int i=1;i<=M;i++){
int t1=read();
int t2=read();
f[t1][t2]=1;
}
floyd();
memset(match,0,sizeof(match));
memset(vis,0,sizeof(vis));
tim=pretim=ans=0;
for(int i=1;i<=N;i++)
if(!match[i]){
pretim=tim;
vis[i]=++tim;
if(hun(i))
ans++;
}
printf("%d\n",N-ans);
}
return 0;
}

  

 

poj2594——最小路径覆盖的更多相关文章

  1. poj2594最小路径覆盖+floyd

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8909   Accepted: 3 ...

  2. POJ2594 最小路径覆盖

    题意:       题意就是给你个有向无环图,问你最少放多少个机器人能把图全部遍历,机器人不能走回头路线. 思路:      如果直接建图,跑一遍二分匹配输出n - 最大匹配数会跪,原因是这个题目和以 ...

  3. POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包

    题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 655 ...

  4. poj2594 (最小路径覆盖 + floyd)

    题目链接  http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...

  5. poj2594 机器人寻找宝藏(最小路径覆盖)

    题目来源:http://poj.org/problem?id=2594 参考博客:http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641. ...

  6. POJ2594 Treasure Exploration(最小路径覆盖)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8550   Accepted: 3 ...

  7. POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9794   Accepted: 3 ...

  8. POJ-2594 Treasure Exploration,floyd+最小路径覆盖!

                                                 Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...

  9. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8130   Accepted: 3 ...

随机推荐

  1. Sql Server 优化 SQL 查询:如何写出高性能SQL语句

    1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来从一个 10万条记录的表中查1条 ...

  2. jQuery——入口函数

    中文网 http://www.css88.com/jqapi-1.9/ 版本兼容问题 版本一:1.x版本,兼容IE678 版本二:2.x版本,不兼容IE678 入口函数区别 <script> ...

  3. String数据类型转换

    String是final类,提供字符串不可修改.强制类型转换,String类型无处不在.下面介绍一些常见的String数据类型转换. String数据类型转换成long.int.double.floa ...

  4. CorelDRAW快速制作抖音幻影图像效果

    本教程讲解非常受欢迎的幻影图像效果(Anaglyph 3d),也叫图像分色立体效果,这其中我们要用到CorelDRAW中的透明度工具. 在开始实施Anaglyph效应之前,应当知道,Anaglyph  ...

  5. 20190625 Oracle优化查询(一)

    与其惴惴不安,不如定心应变 前提:我的Oracle服务器是安装在Windows环境中的,没有上到Linux 查看表结构 查询全表 查找空值, 使用“=”是没有结果的,应该使用IS NULL

  6. Asp.Mvc 常用

    url转义 var address = "http://www.cnblog.com"; var a22 = Uri.EscapeDataString(address); var ...

  7. 手撸HashMap实现

    前言 HashMap是Java中常用的集合,而且HashMap的一些思想,对于我们平时解决业务上的一些问题,在思路上有帮助,基于此,本篇博客将分析HashMap底层设计思想,并手写一个迷你版的Hash ...

  8. input chrome下输入之后背景变为黄色的解决办法

    之所以Input输入之后背景原因色变为可恶的黄色,是因为在chrome 下input加上了input:-webkit-autofill这个属性,里面写的就是这个问题出现的原因 代码就是:input:- ...

  9. postgresql数据库部署

    运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 一.postgresql数据库部署 1.前往postgresql安装包的目录(这里我部署的是10.5的版本) [root@web1 ...

  10. axios请求中跨域及post请求问题解决方案

    闲话不多说,用到vue的童鞋们应该大部分都会遇到请求中的各种奇葩问题,昨天研究一天,终于搞出来个所以然了,写篇文章拯救一下广大的童鞋们,某度娘当然也可以搜到,但一般解决了一个问题后就会出现另外一个问题 ...