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. BZOJ2039_employ人员雇佣_KEY

    题目传送门 网络流,求最小割. 设tot为所有盈利的和,即所有人(不花钱)雇佣. 对于S->i建一条容量为c[i]的边,i->j建一条S[i][j]*2的边,之所以这样建是因为如果不选这个 ...

  2. 【LG5020】[NOIP2018]货币系统

    [LG5020][NOIP2018]货币系统 题面 洛谷 题解 考场上第一眼还不会233 可以发现只要可以被其他的货币通过一些奇奇怪怪的方式表示出来的货币就\(ban\)掉即可 就是个完全背包 我是统 ...

  3. 通过java反射机制获取该类的所有属性类型、值

    转自:http://blog.csdn.net/sd4000784/article/details/7448221 方法使用了这俩个包下的 field 和method import Java.lang ...

  4. python与其他语言的区别

    C 和 Python.Java.C#等 C语言: 代码编译得到 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工作 其他语言: 代码编译得到 字节码 ,虚拟机执行字节码并转换成机器码再后在处 ...

  5. Kettle数据源连接配置

    说明: 通过(图3.1)我们可以看到创建数据源时需要配置相应的参数: Connection Name(必填):配置数据源使用名称,如:Rot_Source Host Name(必填):数据库主机IP地 ...

  6. javaweb(二十)——JavaBean总结

    一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...

  7. 安装文件报错error while loading shared libraries: libssl.so.6

    http://www.openssl.org/source/  这里下载http://www.openssl.org/source/openssl-1.0.0r.tar.gz 安装命令为:tar -z ...

  8. 2.1 Oracle之DML的SQL语句之单表查询以及函数

    1.SQL简介 对于不同的数据库来说,SQL语句是相通的,关系型数据库都以SQL语句为操作的标准,只是相应的数据库对应的函数不相同. SQL(Structured Query Language,结构化 ...

  9. Spring学习(3):IOC基础(转载)

    一. IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  10. Fulfilling Work: The Shippers More entrepreneurs hire 'fulfillment' outfits to store and ship their products

    By Stu Woo June 23, 2011 Brett Teper faced a logistical problem when he and a partner founded ModPro ...