代码如下:
1 #include <cstdio> 2 #include <cstring> 3 4 int n,m,k; 5 int g[110][110]; 6 int link[110]; 7 int a[10000],b[10000]; 8 int vis[110]; 9 10 bool can(int x) 11 { 12 for(int i = 1;i <= m;i ++) 13 { 14 if(g[x][i] && !vis[i]) 15 { 16 vis[i] = 1; 17 if(link[i] == -1 || can(link[i])) 18 { 19 link[i] = x; 20 return true; 21 } 22 } 23 } 24 25 return false; 26 } 27 28 int maxmatch() 29 { 30 int num = 0; 31 for(int i = 1;i <= n;i ++) 32 { 33 memset(vis,0,sizeof(vis)); 34 if(can(i)) 35 num ++; 36 } 37 return num; 38 } 39 40 int main() 41 { 42 int cas = 1; 43 while(scanf("%d%d%d",&n,&m,&k) == 3) 44 { 45 memset(link,-1,sizeof(link)); 46 memset(g,0,sizeof(g)); 47 for(int i = 0;i < k;i ++) 48 { 49 scanf("%d%d",&a[i],&b[i]); 50 g[a[i]][b[i]] = 1; 51 } 52 53 int ans = maxmatch(); 54 55 int ansk = 0; 56 for(int i = 0;i < k;i ++) 57 { 58 g[a[i]][b[i]] = 0; 59 memset(link,-1,sizeof(link)); 60 if(maxmatch() != ans) 61 ansk ++; 62 g[a[i]][b[i]] = 1; 63 } 64 printf("Board %d have %d important blanks for %d chessmen.\n",cas ++,ansk,ans); 65 } 66 67 return 0; 68 }
模仿别人写的,就是找到最大匹配数后,再看空点如果不是空点的最大匹配数会不会变