Question: http://poj.org/problem? Id = 2594
Note: use Floyd to connect an I point to a point that can be reached by I, and then overwrite the minimum path.
Source code:
#include <stdio.h>#include <string.h>#define MAXN 505int map[MAXN][MAXN];int vx[MAXN],vy[MAXN],v[MAXN];int cas,nx,ny,n,m;int uu,vv;int dfs(int x){ v[x]=1; for(int y=1;y<=ny;y++) if(map[x][y]==1) { if(vy[y]==-1||(v[vy[y]]==0&&dfs(vy[y]))) { vx[x]=y; vy[y]=x; return 1; } } return 0;}int MaxMatch(){ int ans=0; for(int i=1;i<=nx;i++) vx[i]=-1; for(int i=1;i<=ny;i++) vy[i]=-1; for(int i=1;i<=nx;i++) { for(int j=1;j<=nx;j++) v[j]=0; if(dfs(i)) ans++; } return ans;}int main(){ //freopen("F:\\a.txt","r",stdin); while(1) { scanf("%d %d",&n,&m); if(!n&&!m) break; memset(map,0,sizeof(map)); for(int i=0;i<m;i++) { scanf("%d %d",&uu,&vv); map[uu][vv]=1; } nx=ny=n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) if(map[i][j]&&map[j][k]) map[i][k]=1; printf("%d\n",n-MaxMatch()); } return 0;}