1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #define N 505 6 using namespace std; 7 8 int g[N][N]; 9 int n, m; 10 int vis[N], linker[N]; 11 bool dfs(int u){ 12 for(int i=1; i<=n; ++i) 13 if(g[u][i] && !vis[i]){ 14 vis[i]=1; 15 if(!linker[i] || dfs(linker[i])){ 16 linker[i]=u; 17 return true; 18 } 19 } 20 return false; 21 } 22 23 int main(){ 24 while(scanf("%d%d", &n, &m) && (n||m)){ 25 memset(g, 0, sizeof(g)); 26 while(m--){ 27 int u, v; 28 scanf("%d%d", &u, &v); 29 g[u][v]=1; 30 } 31 for(int k=1; k<=n; ++k) 32 for(int i=1; i<=n; ++i) 33 for(int j=1; j<=n; ++j) 34 if(!g[i][j]) 35 g[i][j]=g[i][k]&&g[k][j]; 36 memset(linker, 0, sizeof(linker)); 37 int ans=0; 38 for(int i=1; i<=n; ++i){ 39 memset(vis, 0, sizeof(vis)); 40 if(dfs(i)) ++ans; 41 } 42 printf("%d\n", n-ans); 43 } 44 return 0; 45 }
poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)
时间: 2024-10-31 15:24:06