一般图最大匹配带花树:

建图后,计算最大匹配数.

假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点。后手不论怎么走,都必定走到一个被匹配的点上。先手就能够顺着这个交错路走下去,最后一定是后手没有路可走,由于假设还有路可走,这一条交错路,就是一个增广路,必定有更大的匹配.

Game


Time Limit: 1 Second      Memory Limit: 32768 KB


Fire and Lam are addicted to the game of Go recently. Go is one of the oldest board games. It is rich in strategy despite its simple rules. The game is played by two players who alternately
place black and white stones on the vacant intersections of a grid of 19*19 lines. Once placed on the board, stones cannot be moved elsewhere, unless they are surrounded and captured by the opponent's stones. The object of the game is to control (surround)
a larger portion of the board than the opponent.

Fire thinks it is too easy for him to beat Lam. So he thinks out a new game to play on the board. There are some stones on the board, and we don't need to care about the stones' color
in this new game. Fire and Lam take turns to remove one of the stones still on the board. But the Manhattan distance between the removed stone and the opponent's last removed stone must not be greater than L. And the one who can't remove any stone
loses the game.

The Manhattan distance between (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

To show the performance of grace, Fire lets Lam play first. In the beginning of the game, Lam can choose to remove any stone on the board.

Fire and Lam are clever, so they both use the best strategy to play this game. Now, Fire wants to know whether he can make sure to win the game.

Input

There are multiple cases (no more than 30).

In each case, the first line is a positive integer n (n <= 361) which indicates the number of stones left on the board. Following are n lines, each contains
a pair of integers x andy (0 <= xy <= 18), which indicate a stone's location. All pairs are distinct. The last line is an integer L (1 <= L <= 36).

There is a blank line between cases.

Ouput

If Fire can win the game, output "YES"; otherwise, just output "NO".

Sample Input

2
0 2
2 0
2 2
0 2
2 0
4

Sample Output

NO
YES

Author: LIN, Yue

Source: The 10th Zhejiang University Programming Contest

problemId=3726" style="color:blue; text-decoration:none">Submit    Status

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue> using namespace std; const int maxn=500; /*******************************************/ struct Edge
{
int to,next;
}edge[maxn*maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
} /*******************************************/ int n;
int Match[maxn];
int Start,Finish,NewBase;
int Father[maxn],Base[maxn];
bool InQueue[maxn],InPath[maxn],InBlossom[maxn];
int Count;
queue<int> q; int FindCommonAncestor(int u,int v)
{
memset(InPath,false,sizeof(InPath));
while(true)
{
u=Base[u];
InPath[u]=true;
if(u==Start) break;
u=Father[Match[u]];
}
while(true)
{
v=Base[v];
if(InPath[v]) break;
v=Father[Match[v]];
}
return v;
} void ResetTrace(int u)
{
int v;
while(Base[u]!=NewBase)
{
v=Match[u];
InBlossom[Base[u]]=InBlossom[Base[v]]=true;
u=Father[v];
if(Base[u]!=NewBase) Father[u]=v;
}
} void BlossomContract(int u,int v)
{
NewBase=FindCommonAncestor(u,v);
memset(InBlossom,false,sizeof(InBlossom));
ResetTrace(u); ResetTrace(v);
if(Base[u]!=NewBase) Father[u]=v;
if(Base[v]!=NewBase) Father[v]=u;
for(int tu=1;tu<=n;tu++)
{
if(InBlossom[Base[tu]])
{
Base[tu]=NewBase;
if(!InQueue[tu])
{
q.push(tu);
InQueue[tu]=true;
}
}
}
} void FindAugmentingPath()
{
memset(InQueue,false,sizeof(InQueue));
memset(Father,0,sizeof(Father));
for(int i=1;i<=n;i++) Base[i]=i;
while(!q.empty()) q.pop();
q.push(Start); InQueue[Start]=true;
Finish=0; while(!q.empty())
{
int u=q.front(); InQueue[u]=false;
q.pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(Base[u]!=Base[v]&&Match[u]!=v)
{
if(v==Start||(Match[v]>0&&Father[Match[v]]>0))
BlossomContract(u,v);
else if(Father[v]==0)
{
Father[v]=u;
if(Match[v]>0)
{
q.push(Match[v]);
InQueue[Match[v]]=true;
}
else
{
Finish=v;
return ;
}
}
}
}
}
} void AugmentPath()
{
int u,v,w;
u=Finish;
while(u>0)
{
v=Father[u];
w=Match[v];
Match[v]=u;
Match[u]=v;
u=w;
}
} void Edmonds()
{
memset(Match,0,sizeof(Match));
for(int u=1;u<=n;u++)
{
if(Match[u]==0)
{
Start=u;
FindAugmentingPath();
if(Finish>0) AugmentPath();
}
}
} struct Point
{
int x,y,id;
}p[maxn]; int L; int MHD(Point a,Point b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
p[i]=(Point){x,y,i};
}
scanf("%d",&L);
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(MHD(p[i],p[j])<=L)
{
add_edge(i,j);
add_edge(j,i);
}
}
}
Edmonds();
Count=0;
for(int i=1;i<=n;i++)
{
if(Match[i]) Count++;
else break;
}
//cout<<"--> "<<Count<<endl;
if(Count==n) puts("YES");
else puts("NO");
}
return 0;
}

最新文章

  1. form-line 样式 让 两个控件在同一个水平位置
  2. 1.4 云计算的SPI服务模型
  3. Java for LeetCode 221 Maximal Square
  4. Undefined symbols for architecture x86_64: ( linker command failed with exit code 1)
  5. UIEdgeInsetsMake, CGRectOffset等API参数详解
  6. bzoj 2744: [HEOI2012]朋友圈
  7. 收集Magento FAQ常见问题处理办法
  8. node.js 包教不包会 (Windows版详解)
  9. 关于pthread里面一些函数的使用心得!
  10. sharepoint2013小技巧
  11. FFMPEG:H264解码-SDL显示(RGB32、RGB24、YUV420P、YUV422)
  12. expdp之include参数——实现表级别的expdp操作
  13. Awvs、Snort的下载安装
  14. Linux 开机启动顺序_005
  15. Chart控件的用法
  16. UVa 127 - &amp;quot;Accordian&amp;quot; Patience POJ 1214 链表题解
  17. pycharm的基本使用
  18. ClassNotFoundException和NoClassDefFoundError的解决办法
  19. pageadmin CMS Sql Server2008 R2数据库安装教程
  20. [linux]linux下安装mysql

热门文章

  1. JavaEE-02 JSP数据交互01
  2. 记一次Linux系统被入侵的过程
  3. focus,focusin,blur,focusout区别
  4. 牛客OI赛制测试赛2 D 星光晚餐
  5. linux配置网桥
  6. 如何用纯 CSS 创作一个荧光脉冲 loader 特效
  7. php函数之数组
  8. 有关OEP脱壳
  9. Hibernate入门(1)——环境搭建
  10. AI学习笔记(02)