Description
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
Output
Sample Input
1 0 2 1 1 2 2 0 0 0
Sample Output
1 1 2
//
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=600;//N不能太大 否则超时
int cap[N][N];//初始化要清零
int _link[N];
bool used[N];
int nx,ny;//1->nx
bool _find(int t)
{
for(int i=1;i<=ny;i++)
if(!used[i]&&cap[t][i]==1)
{
used[i]=true;
if(_link[i]==-1||_find(_link[i]))
{
_link[i]=t;
return true;
}
}
return false;
}
int MaxMatch()
{
int num=0;
memset(_link,-1,sizeof(_link));
for(int i=1;i<=nx;i++)
{
memset(used,false,sizeof(used));
if(_find(i)) num++;
}
return num;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2&&n)
{
nx=ny=n;
memset(cap,0,sizeof(cap));
for(int i=0;i<m;i++)
{
int x,y;scanf("%d%d",&x,&y);
cap[x][y]=1;
}
//此题中每个点能走多次,所以要先FLOYD
for (int k=1;k<=n;k++)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (cap[i][k] && cap[k][j]) //顶点i和顶点j可到达
{
cap[i][j]=1;
}
}
}
}
int ans=MaxMatch();
printf("%d\n",n-ans);
}
return 0;
}