登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

z55250825

一只蒟蒻

 
 
 

日志

 
 

【网络流24题】【骑士共存问题】  

2014-02-15 15:50:10|  分类: AC题目 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
   我们把每一个可以放棋子的点拆成两个点,然后对于两点如果可以一步走到则连边..显然就是二分图的最大独立集问题,然后类似24题之前的做即可。最近由于在YY WC上FFT(HZA)大神的带花树算法 所以喜欢上了做匹配问题,而本题做的是二分图匹配,所以这次用二分图匹配来写写算了,这样网络流24题就算大致完结了....
   (注:第九个点匈牙利得手写人工栈,这里人懒...)
====================================

program flow24;
const maxn=500000;
type edge=record
     v,pre:longint;
     end;

var e:array[0..maxn]of edge;
    last,match:array[0..maxn]of longint;
    v:array[0..maxn]of boolean;
    n,m,ans,tot:longint;
    g:array[0..210,0..210]of longint;

function cal(i,j:longint):longint;
begin
  exit((i-1)*n+j);
end;

procedure addedge(u,v:longint);
begin
  inc(tot);
  e[tot].v:=v;
  e[tot].pre:=last[u];
  last[u]:=tot;
  inc(tot);
  e[tot].v:=u;
  e[tot].pre:=last[v];
  last[v]:=tot;
end;

function dfs(u:longint):boolean;
var tmp:longint;
begin
  tmp:=last[u];
  while tmp<>0 do
   begin
     if not v[e[tmp].v] then
      begin
        v[e[tmp].v]:=true;
        if (match[e[tmp].v]=0)or(dfs(match[e[tmp].v])) then
          begin
            match[e[tmp].v]:=u;
            exit(true);
          end;
      end;
     tmp:=e[tmp].pre;
   end;
  exit(false);
end;

procedure init;
var i,j,k:longint;
begin
  read(n,m);
  fillchar(g,sizeof(g),0);
  for i:=1 to m do
   begin
     read(j,k);
     g[j][k]:=1;
   end;
  tot:=0;
  fillchar(last,sizeof(last),0);
  fillchar(match,sizeof(match),0);
  for i:=1 to n do
   for j:=1 to n do
    if ((i+j)and 1=0)and(g[i][j]=0) then
     begin
       if (i-1>0)and(j-2>0)and(g[i-1][j-2]=0) then
         addedge(cal(i,j),cal(i-1,j-2));
       if (i-1>0)and(j+2<=n)and(g[i-1][j+2]=0) then
         addedge(cal(i,j),cal(i-1,j+2));
       if (i-2>0)and(j-1>0)and(g[i-2][j-1]=0) then
         addedge(cal(i,j),cal(i-2,j-1));
       if (i-2>0)and(j+1<=n)and(g[i-2][j+1]=0) then
         addedge(cal(i,j),cal(i-2,j+1));
       if (i+1<=n)and(j-2>0)and(g[i+1][j-2]=0) then
         addedge(cal(i,j),cal(i+1,j-2));
       if (i+1<=n)and(j+2<=n)and(g[i+1][j+2]=0) then
         addedge(cal(i,j),cal(i+1,j+2));
       if (i+2<=n)and(j-1>0)and(g[i+2][j-1]=0) then
         addedge(cal(i,j),cal(i+2,j-1));
       if (i+2<=n)and(j+1<=n)and(g[i+2][j+1]=0) then
         addedge(cal(i,j),cal(i+2,j+1));
     end;
  ans:=0;
  for i:=1 to n do
   for j:=1 to n do
    if ((i+j)and 1=0)and(g[i][j]=0) then
     begin
       fillchar(v,sizeof(v),false);
       v[cal(i,j)]:=true;
       if dfs(cal(i,j)) then inc(ans);
     end;
  write(n*n-m-ans);
end;

begin
  //assign(input,'f.in');reset(input);
  init;
  //close(input);
end.

  评论这张
 
阅读(103)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018