Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22905   Accepted: 12421

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

题意:在一个n*n的格子中有k个物品。现在有一种炸弹可以炸掉一排或者一竖的物品,求最少需要几颗炸弹才能把物品全部炸掉。
思路:
对网格的x,y建立联系,如果xi,yi有物品,那么就建立联系。选最少的顶点炸掉所有的物品,就是一个最小顶点覆盖问题,即最少的顶点覆盖图的所有的边。最小顶点覆盖是个NP问题,没有高效的算法。不过对于二分图而言,最大匹配=最小顶点覆盖
证明:首先,最小顶点覆盖不能小于最大匹配。每条匹配边链接的两个匹配点a,b中。最多有1个点链接为匹配点,否者可以去掉当前匹配边,增加2条匹配边,不符合最大匹配。现在选取每个匹配边链接的匹配点中的一个(如果有连接未匹配点就选它),目前最大匹配=最小顶点覆盖,那么有没有边没有包含进去呢?如果有的话,那么这条边的两个顶点只能是未匹配点,这样的话又与最大匹配不符,所以在二分图中,最大匹配=最小顶点覆盖。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF= 1e13+;
priority_queue<P,vector<P>,greater<P> >q;
vector<int>G[maxn];
int vis[maxn],cy[maxn];
int dfs(int u)
{
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(vis[v]) continue;
vis[v]=true;
if(cy[v]==-||dfs(cy[v]))
{
cy[v]=u;
return true;
}
}
return false;
}
int solve(int n)
{
int ans=;
memset(cy,-,sizeof(cy));
for(int i=; i<=n; i++)
{
memset(vis,,sizeof(vis));
ans+=dfs(i);
}
return ans;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=; i<=k; i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
}
cout<<solve(n)<<endl;
return ;
}

二分图最小顶点覆盖

POJ 3041.Asteroids 最小顶点覆盖的更多相关文章

  1. poj 3041 Asteroids(最小点覆盖)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. POJ 3041 Asteroids 最小覆盖数

    http://poj.org/problem?id=3041 题目大意: 一辆宇宙飞船在一个小行星带中,你知道,这很危险.他有一种武器,可以清除掉一行或一列的小行星.问把小行星全部清除最少的武器使用次 ...

  3. poj 3041 Asteroids 最小点覆盖/最大匹配

    Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16242 Accepted: 8833 Descriptio ...

  4. POJ 3041 Asteroids (最小点覆盖集)

    题意 给出一个N*N的矩阵,有些格子上有障碍,要求每次消除一行或者一列的障碍,最少消除多少次可以全部清除障碍. 思路 把关键点取出来:一个障碍至少需要被它的行或者列中的一个消除. 也许是最近在做二分图 ...

  5. POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  6. [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配

    原题 本题为最小点覆盖,而最小点覆盖=最大二分图匹配 //最小点覆盖:用最少的点(左右两边集合的点)让每条边都至少和其中一个点关联. #include<cstdio> #include&l ...

  7. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

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

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

  9. poj 3041——Asteroids

    poj       3041——Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22604   Accep ...

随机推荐

  1. ABAP IMPORT&EXPORT的用法

    1含有事务码 1.1 不注入参数,直接调用 CALL TRANSACTION 'SUIM' AND SKIP FIRST SCREEN. 1.2 注入参数, SET PARAMETER ID: '屏幕 ...

  2. numpy中的数学

    1.dot,exp v = np.dot(arg1,arg2) #矩阵乘法 v2 = np.exp() # e的-x 次方

  3. I/O复用之epoll

    epoll 简介 epoll是为处理大批量句柄而作了改进的poll,它是在2.5.44内核中被引进的. 相关函数调用 int epoll_create(int size); 创建一个epoll的句柄. ...

  4. oracle 查询列表中选取其中一行

    select k.SAL from (select SAL,rownum rn from (select SAL from SCOTT.EMP where MGR = 7698 order by SA ...

  5. Android开发之getX,getRawX,getWidth,getTranslationX等的区别

    转载请注明出处:http://blog.csdn.net/dmk877/article/details/51550031      好久没写博客了,最近工作确实挺忙的,刚刚结束了一个TV项目的开发,对 ...

  6. openvpn-admin(openvpn web管理 )

    openvpn 两种认证简介: 1.key分发: 在服务器端生成秘钥,然后下载到本地,将服务器端的ca.crt xx.crt xx.key ta.key(如果服务器启用的话需要,未开启的话不需要,功能 ...

  7. centos7 mysql主从数据库同步

    主:192.168.2.222:从:192.168.2.223 一:安装mysql 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql!这里依旧以mysql为例进行展示 1.先检 ...

  8. javascript基础:函数参数与闭包问题

    今天在写东西的时候,对函数参数的概念有些模糊,查阅相关资料后,在博客上记点笔记,方便日后复习. 首先,在js中函数参数并没有强语言中那么要求严格,他不介意传递进来多少个参数,也不在乎传进来的参数是什么 ...

  9. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  10. PAT1018 (dijkstra+dfs)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...