Treasure Exploration
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个点,一些点之间有单向边连接,现在有一种机器人能顺着这些边来访问点,两个不同机器人的道路可能包含一些相同的点。要求最少需要多少机器人,才能访问完所有的点。
思路:看到这题发现和我之前做的一道最小路径覆盖的题目很像,唯一不同的就是那题的点是不能重复的,但是这题的点是可以重复经过的,一组样例可以来解释区别
如果能重复经过的话就需要2个机器人,如果不能重复经过就需要3个。然后回到这里,怎么求可以重复经过的,比如有两条边一个是1-2,另一个是2-3,这样的话其实就可以重新建立一条1-3的边,因为他们之间是可以相互到达的,所以可以用flody跑一遍,把能到达的点相互之间都连边。最后求最大匹配数,需要最少机器人的个数就等于点的总数-最大匹配数。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 5010
#define ll long long
#define inf 0x3f3f3f3f
#define mod 998244353
int vis[maxn],match[maxn];
int mp[550][550];
int n,m;
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(mp[i][k]==1&&mp[k][j]==1)
mp[i][j]=1;
}
int dfs(int u)
{
for(int v=1; v<=n; v++)
{
if(!vis[v]&&mp[u][v])
{
vis[v]=1;
if(!match[v]||dfs(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0) break;
memset(match,0,sizeof(match));
memset(mp,0,sizeof(mp));
int x,y;
for(int i=0; i<m; i++)
{
scanf("%d %d",&x,&y);
mp[x][y]=1;
}
floyd();
int ans=0;
for(int i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",n-ans);
}
return 0;
}