Treasure Exploration

Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
 
 
题目大意:多组数据。每组给n,m。表示有n个点,m条有向边。让机器人访问n个点,每个机器人不能走回头路,但是不同的机器人可以走其他机器人走过的点。问你最少需要多少个机器人。
 
解题思路:最小路径覆盖。首先用floyd传递闭包。然后再跑最大匹配。
 
样例:   5 4
    1 2
    3 2
    2 4
    2 5
按照一般的最小路径覆盖,那么需要3个机器人,但是经过floyd传递闭包后,其实只需要2个即可。
 
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 600;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
int Map[maxn][maxn];
bool SearchP(int _n){
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){
dy[v] = dx[u] + 1;
if(My[v] == -1){
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){
continue;
}
if(My[v] == -1 || dfs(My[v])){
Mx[u] = v;
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){
int ret = 0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(SearchP(ln)){
memset(used,0,sizeof(used));
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m, N, M, k;
while(scanf("%d%d",&N,&M)!=EOF&&(N+M)){
for(int i = 0; i <= N; i++){
G[i].clear();
}
int a,b;
memset(Map,0,sizeof(Map));
for(int i = 1; i <= M; i++){
scanf("%d%d",&a,&b);
Map[a][b] = 1;
}
for(int k = 1; k <= N; k++){
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
Map[i][j] = Map[i][j]|| Map[i][k]&&Map[k][j];
}
}
} for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(Map[i][j]){
G[i].push_back(j);
}
}
}
n = m = N;
int res = MaxMatch(n,m);
printf("%d\n",N-res);
}
return 0;
}

  

最新文章

  1. JavaScript基本数据类型和引用数据类型
  2. android 关闭多个或指定activity
  3. 用PowerMock mock final类
  4. C++语言-03-类与对象
  5. NetworkComms网络通信框架V3结构图
  6. 151. Reverse Words in a String
  7. JSONP实例
  8. Error running app: Instant Run requires &#39;Tools | Android | Enable ADB integration&#39; to be enabled.解决办法
  9. 抽屉显示控件SlidingDrawer入门
  10. 菜鸟学习SSH(一)——Struts实现简单登录(附源码)
  11. light oj 1047-neighbor house
  12. Android事件机制
  13. jacascript 构造函数、原型对象和原型链
  14. java泛型【收藏】
  15. c++之sizeof的用法
  16. 黑客帝国效果赏析(包含ES6的语法)
  17. IO流(字节流,字符流,缓冲流)
  18. CSS :invalid 选择器
  19. 【388】※ Some useful websites for learning Python
  20. eclipse java build path问题汇总

热门文章

  1. 如何在Linux上使用x2go设置远程桌面
  2. 本地机器和windows2003远程桌面之间复制粘贴文件
  3. 基于vue框架项目开发过程中遇到的问题总结(一)
  4. Java整体之JavaEE
  5. 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
  6. 数据结构14:队列(Queue),“先进先出”的数据结构
  7. arcgis10.0直连sde
  8. opencv第一课,安装配置
  9. python安装出现的证书问题
  10. maven 设置 编码 ,jdk 版本