您的位置:首页 > 其它

Codevs 3052 多米诺 (二分图染色+二分图最大匹配)

2016-11-16 10:22 337 查看
一个多米诺占用相邻的两个

那么把相邻的两个分开的话,就可以把一个多米诺看做一个成立的二分图匹配

于是把棋盘进行二分图染色,染成黑白棋牌的那种,即一个格子与它上下左右的格子颜色不同

所以我们就把每个格子与它上下左右的格子连边,连边的条件就是这两个格子都是可以放的

(因为只有二分图左右两边的都是可以放的格子才可以把匹配看做一张多米诺)

然后跑匈牙利算法就可以了=w=

const
walk:array[1..4,1..2] of longint=((1,0),(-1,0),(0,1),(0,-1));
var
n,m,k,a,b,ans :longint;
c,d :longint;
i,j,ll,l :longint;
co :array[0..2510] of longint;
vis :array[0..2510] of boolean;
link,last :array[0..2510] of longint;
pre,other :array[0..20010] of longint;
map :array[0..2510] of boolean;
procedure connect(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
end;

procedure dfs(x,t:longint);
var
p,q:longint;
begin
co[x]:=t;
t:=t xor 1;
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if (co[p]=-1) then dfs(p,t);
q:=pre[q];
end;
end;

function find(x:longint):boolean;
var
p,q:longint;
begin
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if not vis[p] then
begin
vis[p]:=true;
if (link[p]=0) or (find(link[p])) then
begin
link[p]:=x;exit(true);
end;
end;
q:=pre[q];
end;
exit(false);
end;

begin
read(n,m);read(k);
for i:=1 to k do
begin
read(a,b);
map[(a-1)*m+b]:=true;
end;
for i:=1 to n do
for j:=1 to m do
if not map[(i-1)*m+j] then
begin
for ll:=1 to 4 do
begin
a:=i+walk[ll,1];
b:=j+walk[ll,2];
if (a>=1) and (b>=1) and (a<=n) and (b<=m) and not map[(a-1)*m+b] then
begin
connect((i-1)*m+j,(a-1)*m+b);
connect((a-1)*m+b,(i-1)*m+j);
end;
end;
end;
//
for i:=1 to n*m do co[i]:=-1;
for i:=1 to n*m do
if (co[i]=-1) then dfs(i,0);
//
for i:=1 to n*m do
if co[i]=0 then
begin
fillchar(vis,sizeof(vis),false);
if find(i) then inc(ans);
end;
//
writeln(ans);
end.
——by Eirlys 
转载请注明出处=w=

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模板 二分图
相关文章推荐