TYVJ 1268 [CoVH之柯南开锁]

废话不多说,这个题目为二分图最大匹配:把n行每一行看成一个点,共有n个点;把m列每一列看成一个点,共有m个点。当格子为灰色时,把这个格子所属的行的点与列的点连边,这样就构成一个二分图。

 

VAR
 cover:array[1..10000]of boolean;
 link,v:array[1..10000]of longint;
 map:array[1..10000,1..100]of longint;
 i,m,n,maxp:longint;
Procedure init;
 var
 i,j:longint;
 s:string;
 begin
 fillchar(v,sizeof(v),0);
 readln(n,m);
 for i:=1 to n do
  begin
  readln(s);
  for j:=1 to m do
   if s[j]='1'then
    begin
    inc(v[i]);
    map[i,v[i]]:=n+j;
    inc(v[n+j]);
    map[n+j,v[n+j]]:=i;
    end;
  end;
 end;
Function find(i:longint):boolean;
 var
 q,k,m:longint;
 begin
 find:=true;
 for k:=1 to v[i] do
  begin
  m:=map[i,k];
  if not cover[m] then
   begin
   q:=link[m];link[m]:=i;cover[m]:=true;
   if (q=0)or find(q) then exit;
   link[m]:=q;
   end;
  end;
 exit(false);
 end;
Begin
 init;
 for i:=1 to m+n do
  begin
  fillchar(cover,sizeof(cover),0);
  find(i);
  end;
 maxp:=0;
 for i:=1 to m+n do if link[i]<>0 then inc(maxp);
 writeln(maxp div 2);
End.
posted @ 2011-10-26 20:31  NoRush  阅读(1173)  评论(0编辑  收藏  举报