Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8193 | Accepted: 3358 |
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
特殊的最小路径覆盖,每个点可以被不同的路径重复经过。
用floyd判断两个点是否间接联通(传说这叫传递闭包),然后匈牙利算法找路径覆盖即可。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=510; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 13 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 14 return x*f; 15 } 16 int mp[mxn][mxn]; 17 int link[mxn]; 18 bool vis[mxn]; 19 int n,m; 20 bool DFS(int u){ 21 int i,j; 22 for(i=1;i<=n;i++){ 23 if(mp[u][i] && !vis[i]){ 24 vis[i]=1; 25 if(link[i]==-1 || DFS(link[i])){ 26 link[i]=u; 27 return 1; 28 } 29 } 30 } 31 return 0; 32 } 33 int solve(){ 34 int res=0; 35 for(int i=1;i<=n;i++){ 36 memset(vis,0,sizeof vis); 37 if(DFS(i))res++; 38 } 39 return res; 40 } 41 int main(){ 42 int i,j,u,v; 43 while(scanf("%d%d",&n,&m)){ 44 if(!n && !m)break; 45 memset(mp,0,sizeof mp); 46 memset(link,-1,sizeof link); 47 for(i=1;i<=m;i++){ 48 u=read();v=read(); 49 mp[u][v]=1; 50 } 51 for(int k=1;k<=n;k++) 52 for(i=1;i<=n;i++) 53 for(j=1;j<=n;j++){ 54 mp[i][j]|=mp[i][k]&mp[k][j]; 55 } 56 int res=solve(); 57 printf("%d\n",n-res); 58 } 59 return 0; 60 }