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?
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.
For each test of the input, print a line containing the least robots needed.
1 0 2 1 1 2 2 0 0 0
1 1 2
注意stl的vector在做查找时候比较慢,会超时 所以不妨就用二维map
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<cmath>
#define ff(i,x,y) for(int i=x;i<y;i++)
#define maxlen 500+7
using namespace std;
//vector<int> mmap[maxlen];
bool mmap[maxlen][maxlen];
bool check[maxlen];
int matching[maxlen];
int n,m;
bool dfs(int u)
{
for(int i=0;i<n;i++)
{
if(mmap[u][i]&&!check[i])
{
check[i]=1;
if(matching[i]==-1||dfs(matching[i])==1)
{
matching[i]=u;
return true;
}
}
}//for
return false;
}
int main()
{
//freopen("test.txt","r",stdin);
while(1)
{
//ini
//for(int i=0;i<maxlen;i++)
// mmap[i].resize(0);
memset(mmap,0,sizeof mmap);
//input
scanf("%d %d",&n,&m);
if(!n&&!m)
break;
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
x--;
y--;
mmap[x][y]=1;
//mmap[x].push_back(y);
}
//do
//先通过FLOYD将间接相邻的点直接相邻
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//if((find(mmap[i].begin(),mmap[i].end(),k)!=mmap[i].end())&&(find(mmap[k].begin(),mmap[k].end(),j)!=mmap[k].end()))
//mmap[i].push_back(j);
if(mmap[i][k]&&mmap[k][j])
mmap[i][j]=1;
}
}
}
memset(matching,-1,sizeof matching);
int ans=0;//最大匹配
for(int i=0;i<n;i++)//匈牙利
{
memset(check,0,sizeof check);
if(dfs(i)==1)
ans++;
}
printf("%d\n",n-ans);
}
return 0;
}