Two-part graph matching
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <cmath>5 #defineMAXN 606 #defineMaxd 15007 using namespacestd;8 intV[maxd][maxd],vist[maxd],math[maxd];9 Ten intCOL[MAXN][MAXN],ROW[MAXN][MAXN]; One intk1,k2,n,m; A CharSTR[MAXN][MAXN]; - voidCreat_row () - { thek1=1; - for(intI=0; i<m;i++) - { - intflag=0; + for(intj=0; j<n;j++) - { + if(str[i][j]=='*') A { atrow[i][j]=K1; -flag=1; - } - if(str[i][j]=='#') - { -k1++; inflag=0; - } to } + if(flag) -k1++; the } * } $ voidCreat_col ()Panax Notoginseng { -K2=1; the for(intj=0; j<n;j++) + { A intflag=0; the for(intI=0; i<m;i++) + { - if(str[i][j]=='*') $ { $col[i][j]=K2; -flag=1; - } the if(str[i][j]=='#') - {Wuyik2++; theflag=0; - } Wu } - if(flag) Aboutk2++; $ } - } - intDfsintx) - { A for(intI=1; i<k2;i++) + { the if(!vist[i]&&V[x][i]) - { $vist[i]=1; the if(math[i]==-1||DFS (Math[i])) the { themath[i]=x; the return true; - } in the } the } About return false; the } the voidKM () the { + intcn1=0; - for(intI=1; i<k1;i++) the {Bayimemset (Vist,0,sizeof(vist)); the if(Dfs (i)) ans++; the } -printf"%d\n", ans); - } the intMain () the { the intT; thescanf"%d",&t); - while(t--) the { thescanf"%d%d",&m,&n); the for(intI=0; i<m; i++)94scanf"%s",&str[i]); the Creat_row (); the Creat_col (); the //printf ("%d%d==\n", K1,K2);98memset (V,0,sizeof(v)); Aboutmemset (math,-1,sizeof(math)); - for(intI=0; i<m; i++)101 {102 for(intj=0; j<n; J + +)103 {104 if(str[i][j]=='*') thev[row[i][j]][col[i][j]]=1;106 }107 108 }109 KM (); the }111 return 0; the}
HDU 5093 Battle ships