hdu 1281
二分图,简单的模板题,不过题目比较难懂;
其中important chess就是删掉它不能够完美匹配,所以就枚举每一个可能删的棋子;
代码:
1 #include <cstdio> 2 #include <cstring> 3 int map[101][101],v[101]; 4 int match[101],n,m; 5 int dfs(int k) 6 { 7 int i; 8 for(i=0; i<m; i++) 9 { 10 if(map[k][i]&&!v[i]) 11 { 12 v[i]=1; 13 if(match[i]==-1||dfs(match[i])) 14 { 15 match[i]=k; 16 return 1; 17 } 18 } 19 } 20 return 0; 21 } 22 int main() 23 { 24 int i,t,ans,k,p,q,c,l,g; 25 t=0; 26 while(scanf("%ld%ld%ld",&n,&m,&k)==3) 27 { 28 memset(map,0,sizeof(map)); 29 memset(match,-1,sizeof(match)); 30 for(i=0; i<k; i++) 31 { 32 scanf("%d%d",&p,&q); 33 map[p-1][q-1]=1; 34 } 35 l=0; 36 for(i=0; i<n; i++) 37 { 38 memset(v,0,sizeof(v)); 39 if(dfs(i)) 40 l++; 41 } 42 c=0; 43 for(i=0; i<n; i++) 44 { 45 g=match[i]; 46 match[i]=-1; 47 map[g][i]=0; 48 memset(v,0,sizeof(v)); 49 if(!dfs(g)) 50 { 51 match[i]=g; 52 c++; 53 } 54 map[g][i]=1; 55 } 56 printf("Board %d have %d important blanks for %ld chessmen.\n",++t,c,l); 57 } 58 return 0; 59 }