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).

 
 
 
大意:K个目标在N*N的方阵中,一次攻击可以消除同一行或同一列的目标,问最少几次攻击消除所有目标。
题解:这题是看题解写的,建一个二分图,图一侧的定点代表行,另一侧的定点代表列,如果(x,y)有目标,就连边x->y。
那么这题就转换为最少选多少个点,使所有边的两个定点至少有一个被选中,这就是经典模型最小点覆盖。
结论:最小点覆盖=最大匹配。
写的很好
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
const int maxn=1010;
int N,K,lin[maxn],len;
struct edge{
int y,next;
}e[10010];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
}
int tim,pretim,match[maxn],vis[maxn],ans;
bool hun(int x){
for(int i=lin[x];i;i=e[i].next)
if(vis[e[i].y]<=pretim){
vis[e[i].y]=++tim;
if(match[e[i].y]==0||hun(match[e[i].y])){
match[x]=e[i].y;
match[e[i].y]=x;
return 1;
}
}
return 0;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
N=read(),K=read();
for(int i=1;i<=K;i++){
int t1=read(),t2=read()+N;
insert(t1,t2);
}
for(int i=1;i<=N*2;i++)
if(!match[i]){
pretim=tim;
tim++;
if(hun(i))
ans++;
}
printf("%d\n",ans);
return 0;
}

  

poj3041——最小点覆盖的更多相关文章

  1. POJ3041 Asteroids(二分图最小点覆盖)

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

  2. 二分图变种之最小路径覆盖、最小点覆盖集【poj3041】【poj2060】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54859604 向大(hei)佬(e)势力学(di ...

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

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

  4. hdu1498 50 years, 50 colors --- 最小点覆盖

    给一个矩阵,里面有一些不同颜色的气球.每次能够消灭一行或一列中某一种颜色的气球,问你在k次及以内,有哪些颜色的气球是不管怎样也消不完的. 那么思路就是,对每一种颜色的气球求最小点覆盖.>k 则为 ...

  5. 二分图 最小点覆盖 poj 3041

    题目链接:Asteroids - POJ 3041 - Virtual Judge  https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...

  6. Asteroids POJ - 3041 二分图最小点覆盖

       Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in ...

  7. ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

    //匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...

  8. 【POJ 3041】Asteroids (最小点覆盖)

    每次选择清除一行或者一列上的小行星.最少选择几次. 将行和列抽象成点,第i行为节点i+n,第j列为节点j,每个行星则是一条边,连接了所在的行列. 于是问题转化成最小点覆盖.二分图的最小点覆盖==最大匹 ...

  9. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

随机推荐

  1. STL之set篇

    insert为插入.set_intersection求交集,set_union求并集,是属于algorithm里的函数. 例题有 PAT甲级1063 #include<iostream> ...

  2. mac中显示隐藏文件和.开头的文件

    在控制台中执行一下命令,即可在finder中看到此类文件: defaults write com.apple.Finder AppleShowAllFiles YES killall Finder

  3. XML——读与写

    XML写入 private static void writeXml() { using (XmlTextWriter xml = new XmlTextWriter(@"C:\Users\ ...

  4. 如何在Linuxt系统下运行maven项目

    如何在Linuxt系统下运行maven项目 我们知道现在利用MAVEN来管理JAVA项目是非常常见的.比如公司一般都有一个自己的MAVEN仓库,通过MAVEN仓库来解决我们的项目依赖,更加方便的构建项 ...

  5. [Windows Server 2012] 安装SQL Server 2012

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...

  6. 【YOLO】实时对象检测使用体验

    官网:https://pjreddie.com/darknet/yolo/ 以下全部在服务器上完成,服务器上是有opencv等. 1.安装Darknet git clone https://githu ...

  7. 011--c数组--排序--组成最大数

    数组--排序--组成最大数   组成最大数   任意输入一个自然数,输出该自然数的各位数字组成的最大数.例如,输入 1593 ,则输出为 9531 . 输入: 自然数 n 输出: 各位数字组成的最大数 ...

  8. IOS: Xcode报 Undecleared selector Warning错误的解决方法

    Undecleared selector  Warning 是编译器报的,特别是升级到IOS7 默认PROJ设定的时候,会出现这种问题,如果从代码上看语法完全没有问题,那么就可以在Xcode里面设置禁 ...

  9. discuz 微社区开通

    检测api: http://wsq.discuz.com/?a=apitest

  10. Keil MDK下如何设置非零初始化变量(复位后变量值不丢失)

    一些工控产品,当系统复位后(非上电复位),可能要求保持住复位前RAM中的数据,用来快速恢复现场,或者不至于因瞬间复位而重启现场设备.而keil mdk在默认情况下,任何形式的复位都会将RAM区的非初始 ...