Description

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.

Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.

Input

* Line 1: Two integers N and K, separated by a single space. 
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

3 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

INPUT DETAILS: 
The following diagram represents the data, where "X" is an asteroid and "." is empty space: 
X.X 
.X. 
.X.

OUTPUT DETAILS: 
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).

Source

USACO 2005 November Gold
 
 
分别以n个行和n个列为结点,建立二分图,每一个星球对应一个连接其所在行和列的边,问题转化为求这个二分图的最小顶点覆盖。
在二分图中,有结论:最小顶点覆盖数=最大匹配数
自己yy了一个证明:
首先,我们证明不存在小于最大匹配数的最小顶点覆盖数。
这是显然的,因为这些顶点根本无法覆盖所有的匹配边。
接下来证明存在等于最大匹配数的最小顶点覆盖数。
首先证明,在最大匹配中,每条匹配边连接的两个顶点a,b最多只有一个与非匹配点有连边。
用反证法:假设a与c,b与d这件都有边,且c,d都不是匹配点,则可以去掉连接a,b的匹配边,加上连接a,c和连接b,d的匹配边,是匹配数+1,这与最大匹配矛盾。
这样,我们构造这样一个顶点集合:对于每条匹配边,选择其连接的两个点中的一个(如果两个点有与非匹配点有连边的点,则选那个点;否则随便选一个)。
这个集合中有最大匹配数个点。
我们证明:这个点集能覆盖所有的边。
若一条边是匹配边,则其显然被覆盖。
若一条边不是匹配边:
1)若其与某匹配顶点有连边,则该匹配顶点必在我们构造的点集中,所以该边被覆盖
2)若其连接着两个非匹配点,则可以增加这条边为匹配边,是匹配数+1,这与最大匹配矛盾,故此情况不成立
所以,这个点集能覆盖所有的边。
综上所述,在二分图中,最小顶点覆盖数=最大匹配数已得到证明。
 program rrr(input,output);
const
inf=;
type
etype=record
t,c,rev,next:longint;
end;
var
e:array[..]of etype;
a,cur,d:array[-..]of longint;
q:array[..]of longint;
n,m,i,x,y,h,t,cnt,ans:longint;
procedure ins(x,y,c:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].c:=c;e[cnt].next:=a[x];a[x]:=cnt;
end;
procedure add(x,y:longint);
begin
ins(x,y,);ins(y,x,);
e[cnt].rev:=cnt-;e[cnt-].rev:=cnt;
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure bfs;
begin
for i:=-n to n+ do d[i]:=-;
h:=;t:=;q[]:=;d[]:=;
while h<t do
begin
inc(h);
i:=a[q[h]];
while i<>inf do
begin
if (d[e[i].t]=-) and (e[i].c>) then
begin
d[e[i].t]:=d[q[h]]+;
inc(t);q[t]:=e[i].t;
end;
i:=e[i].next;
end;
end;
end;
function dfs(k,f:longint):longint;
var
ans,r,i:longint;
begin
if (k=n+) or (f=) then exit(f);
ans:=;i:=cur[k];
while i<>inf do
begin
if (e[i].c>) and (d[e[i].t]=d[k]+) then
begin
r:=dfs(e[i].t,min(f,e[i].c));
dec(e[i].c,r);inc(e[e[i].rev].c,r);
ans:=ans+r;f:=f-r;
if f= then break;
end;
i:=e[i].next;
cur[k]:=i;
end;
if f> then d[k]:=-;
exit(ans);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m);
cnt:=;for i:=-n to n+ do a[i]:=inf;
for i:= to n do begin add(,-i);add(i,n+); end;
for i:= to m do begin readln(x,y);add(-x,y); end;
ans:=;
while true do
begin
bfs;
if d[n+]=- then break;
for i:=-n to n+ do cur[i]:=a[i];
ans:=ans+dfs(,inf);
end;
write(ans);
close(input);close(output);
end.

poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)的更多相关文章

  1. UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design

    题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi  ...

  2. UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)

    恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...

  3. 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)

    二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...

  4. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  5. poj3041 二分图最小顶点覆盖

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17237   td>Accepted: 9375 ...

  6. POJ1325 Machine Schedule 【二分图最小顶点覆盖】

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11958   Accepted: 5094 ...

  7. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  8. [POJ3041] Asteroids(最小点覆盖-匈牙利算法)

    传送门 题意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍,最少要几次.   解析: 把每一行与每一列当做二分图两边的点. 某格子有障碍,则对应行与列连边. ...

  9. POJ 3041 Asteroids (对偶性,二分图匹配)

    题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...

随机推荐

  1. 20155320 2016-2017-2 《Java程序设计》第的四周学习总结

    20155320 2016-2017-2 <Java程序设计>第的四周学习总结 教材学习内容总结 继承与多态 继承就是子承父类,避免重复定义共同行为,会使用extends关键词,表示会扩展 ...

  2. LNMP的安装--详细版

    一.软件概述 [root@webserver ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@webserver ~]# u ...

  3. python3工作环境部署+spyder3+jupyter notebook

    1.python3安装 1)官网去下载python3.7版本,双击安装,只要注意勾选写到PATH就行,其它直接NEXT. 2)安装完成,CMD键入 python 回车,跳出python界面就是成功. ...

  4. spring源码-aop增强-5.2

    一.aop增强就是针对于不同的切面进行的相关增强,目的当然是更好的支持相关应用和解耦. 二.默认的aop增强类有AspectJMethodBeforeAdvice.AspectJMethodBefor ...

  5. Django视图层详细介绍

    1 视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...

  6. 四、利用EnterpriseFrameWork快速开发基于WCF为中间件的三层结构系统

    回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 本章内容与上一张<利用Enterprise ...

  7. 使用闭包的方式实现一个累加函数 addNum

    使用闭包的方式实现一个累加函数 addNum,参数为 number 类型,每次返回的结果 = 上一次计算的值 + 传入的值,如: addNum(10); //10 addNum(12); //22 a ...

  8. Appium 运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for (转)

    现象:Appium运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported f ...

  9. AtCoder Regular Contest 101 D - Median of Medians

    二分答案 然后前缀和+树状数组来判断这个答案是否大于等于数 如果我们对于一个查询,如果小于这个数令为1,大于这个数领为-1 将所有前缀和放在树状数组中,就可以查询所有sum_{l} < sum_ ...

  10. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...