Treasure Exploration
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8570 | Accepted: 3500 |
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?
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
Source
POJ Monthly--2005.08.28,Li Haoyuan
题目大意:
给你一个有向图,让你找出最少的可重叠路径,使得它们能够覆盖全部点。
解题思路:
这题和POJ 1422很像,我最开始也把这道题当成和那道题解法一样了,不过仔细读题可以发现POJ 1422的路径是不能重叠的,也就是通常说的有向图最小路径覆盖问题。而这题不同路径之间是可以有重叠部分的,这就导致了两题的答案不同。
仔细思考就可以发现,造成答案不同的只有一种情况,就是两条路径开始部分和起点部分是相互独立的,但是中间有了一段公共部分,按照最小路径覆盖这样会把其中一条路径拆成两条。如果我们能够把那一段公共路径跳过去就可以用最小路径覆盖解决这题了,于是我们就可以利用传递闭包。
那么这题的解法就出来了,先对原图求传递闭包,然后在新图上跑有向图最小路径覆盖,得到的结果就是有向图最小可重叠路径覆盖。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
const int MAXV=500+3;
int V,E;
bool G[MAXV][MAXV];
int num_x;//左边顶点数
int match_x[MAXV],match_y[MAXV];//顶点匹配对象
int dis,dis_x[MAXV],dis_y[MAXV];//距离(用于多路增广)
bool used[MAXV];
bool searchP()//标记距离(用于多路增广)
{
queue<int> que;
dis=INF;
mem(dis_x,-1);
mem(dis_y,-1);
for(int i=0;i<num_x;++i)
if(match_x[i]==-1)
{
que.push(i);
dis_x[i]=0;
}
while(!que.empty())
{
int u=que.front(); que.pop();
if(dis_x[u]>dis)
break;
for(int v=0;v<V;++v)
if(G[u][v])
{
if(dis_y[v]==-1)
{
dis_y[v]=dis_x[u]+1;
if(match_y[v]==-1)
dis=dis_y[v];
else
{
dis_x[match_y[v]]=dis_y[v]+1;
que.push(match_y[v]);
}
}
}
}
return dis!=INF;
}
bool dfs(int u)//dfs增广
{
for(int v=0;v<V;++v)
if(G[u][v])
{
if(!used[v]&&dis_y[v]==dis_x[u]+1)
{
used[v]=true;
if(match_y[v]!=-1&&dis_y[v]==dis)
continue;
if(match_y[v]==-1||dfs(match_y[v]))
{
match_y[v]=u;
match_x[u]=v;
return true;
}
}
}
return false;
}
int hopcroft_carp()//二分图最大匹配
{
int res=0;
mem(match_x,-1);
mem(match_y,-1);
while(searchP())
{
mem(used,0);
for(int i=0;i<num_x;++i)
if(match_x[i]==-1&&dfs(i))
++res;
}
return res;
}
void init()//初始化
{
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
G[i][j]=false;
}
int main()
{
while(~scanf("%d%d",&V,&E)&&(V||E))
{
init();
for(int i=0;i<E;++i)
{
int u,v;
scanf("%d%d",&u,&v);
G[u-1][v-1]=true;
}
for(int k=0;k<V;++k)//floyd求传递闭包
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
G[i][j]|=G[i][k]&G[k][j];
num_x=V;
printf("%d\n",V-hopcroft_carp());
}
return 0;
}