一般图最大匹配带花树:

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

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

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;
}

ZOJ 3316 Game 一般图最大匹配带花树的更多相关文章

  1. HDOJ 4687 Boke and Tsukkomi 一般图最大匹配带花树+暴力

    一般图最大匹配带花树+暴力: 先算最大匹配 C1 在枚举每一条边,去掉和这条边两个端点有关的边.....再跑Edmonds得到匹配C2 假设C2+2==C1则这条边再某个最大匹配中 Boke and ...

  2. 【learning】一般图最大匹配——带花树

    问题描述 ​ 对于一个图\(G(V,E)\),当点对集\(S\)满足任意\((u,v)\in S\),均有\(u,v\in V,(u,v)\in E\),且\(S\)中没有点重复出现,我们称\(S\) ...

  3. UOJ #79 一般图最大匹配 带花树

    http://uoj.ac/problem/79 一般图和二分图的区别就是有奇环,带花树是在匈牙利算法的基础上对奇环进行缩点操作,复杂度似乎是O(mn)和匈牙利一样. 具体操作是一个一个点做类似匈牙利 ...

  4. 【UOJ 79】 一般图最大匹配 (✿带花树开花)

    从前一个和谐的班级,所有人都是搞OI的.有 n 个是男生,有 0 个是女生.男生编号分别为 1,…,n. 现在老师想把他们分成若干个两人小组写动态仙人掌,一个人负责搬砖另一个人负责吐槽.每个人至多属于 ...

  5. 【UOJ #79】一般图最大匹配 带花树模板

    http://uoj.ac/problem/79 带花树模板,做法详见cyb的论文或fhq的博客. 带花树每次对一个未盖点bfs增广,遇到奇环就用并查集缩环变成花(一个点),同时记录每个点的Next( ...

  6. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  7. URAL 1099. Work Scheduling (一般图匹配带花树)

    1099. Work Scheduling Time limit: 0.5 secondMemory limit: 64 MB There is certain amount of night gua ...

  8. HDU 4687 Boke and Tsukkomi (一般图匹配带花树)

    Boke and Tsukkomi Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Othe ...

  9. URAL1099 Work Scheduling —— 一般图匹配带花树

    题目链接:https://vjudge.net/problem/URAL-1099 1099. Work Scheduling Time limit: 0.5 secondMemory limit: ...

随机推荐

  1. Markdown的安装和语法

    步骤: 1.打开webstorm,File-->Setting-->输入plugin-->Install JetBrains plugin-->输入markdown--> ...

  2. QT_1

    QT概述 1.1 QT 是一个跨平台的C++图形用户界面应用程序框架 1.2 发展史: 1991奇趣科技 1.3 QT 版本:商业版.开源版 1.4 优点: 1.4.1 跨平台 1.4.2 接口简单 ...

  3. ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0

    找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...

  4. <Spring Cloud>入门五 hystrix

    1.服务熔断 1.1引入坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artif ...

  5. 如何用纯 CSS 创作文本滑动特效的 UI 界面

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/QrxxaW 可交互视频教 ...

  6. 关于一个多个.cpp文件的项目中,函数出现未定义引用错误

    编译的话,必须把全部的c文件都要编译的啊,只编译一个c算怎么个逻辑呢?编译实际上是2个过程,编译和链接.编译过程只检查所有的符号(变量,函数)有没有声明,即只需要h文件生命就够了.但是链接时候,需要找 ...

  7. Linux 命令大全 - 管理文件和目录的命令

    1.pwd 显示当前目录 该命令的英文解释为print working directory(打印工作目录).输入pwd命令,Linux会输出当前目录. 2.cd 命令用来改变所在目录 cd / 转到根 ...

  8. Mac 10.10 配置apache

    配置php 命令行工具:http://blog.csdn.net/evane1890/article/details/38759073 自从系统从OS X Mavericks 10.9升级到OS X  ...

  9. css position是前端的你了解多少?

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...

  10. python selenium xpath定位方式

    作者刚开始写博客,格式和语言方面难免存在问题,请大家海涵,如果有问题也请直接指出 xpath是一种在xm文档中定位的语言,详细简介,请自行参照百度百科,本文主要总结一下xpath的使用方法,个人看法, ...