Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12162   Accepted: 6620

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

  1. 3 4
  2. 1 1
  3. 1 3
  4. 2 2
  5. 3 2

Sample Output

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

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int n,k; //n矩阵规格,k星体数量
  5. int V1,V2; //二分图顶点集
  6. /*矩阵的行列分别属于二分图的两个顶点集V1、V2,其中行x∈V1,列y∈V2*/
  7. bool grid[501][501]; //存储数据方式:可达矩阵
  8. bool vis[501]; //记录V2的点每个点是否已被搜索过
  9. int link[501]; //记录 V2中的点y 在 V1中 所匹配的点x的编号
  10. int m; //最大匹配数
  11.  
  12. /*Hungary Algorithm*/
  13.  
  14. bool dfs(int x)
  15. {
  16. for(int y=1;y<=V2;y++)
  17. if(grid[x][y] && !vis[y]) //x到y相邻(有边) 且 节点y未被搜索
  18. {
  19. vis[y]=true; //标记节点y已被搜索
  20. if(link[y]==0 || dfs(link[y])) //link[y]==0 : 如果y不属于前一个匹配M
  21. { //find(link[y] : 如果被y匹配到的节点可以寻找到增广路
  22. link[y]=x; //那么可以更新匹配M'(用M替代M')
  23. return true; //返回匹配成功的标志
  24. }
  25. }
  26. return false; //继续查找V1下一个x的邻接节点
  27. }
  28.  
  29. void search(void)
  30. {
  31. for(int x=1;x<=V1;x++)
  32. {
  33. memset(vis,false,sizeof(vis)); //清空上次搜索时的标记
  34. if(dfs(x)) //从V1中的节点x开始寻找增广路
  35. m++;
  36. }
  37. return;
  38. }
  39.  
  40. int main(void)
  41. {
  42. cin>>n>>k;
  43. V1=V2=n;
  44.  
  45. int x,y; //坐标(临时变量)
  46. for(int i=1;i<=k;i++)
  47. {
  48. cin>>x>>y;
  49. grid[x][y]=true; //相邻节点标记
  50. }
  51.  
  52. /*增广轨搜索*/
  53.  
  54. search();
  55.  
  56. /*Output*/
  57.  
  58. cout<<m<<endl;
  59.  
  60. return 0;
  61. }

poj3041的更多相关文章

  1. poj3041,poj2226

    二分匹配的灵活运用 3041还是比较好想的,考虑到横排/竖排射一枪就能搞定这一行/一列的所有点, 我们以行数为点集x,列数为点集y,在目标点(xi,yi)之间连一条边 这样最小射击次数=最小点覆盖(边 ...

  2. POJ3041 二分图最大匹配

    问题:POJ3041 分析: 构造二分图:令A = B = { 1, 2, ... , n }, 分别代表行号集与列号集.假如第i行第j列有一颗行星,则连接Ai与Bj, 表示必须从Ai(即第i行),B ...

  3. POJ3041 Asteroids 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...

  4. poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配

    /** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...

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

    题目链接: https://vjudge.net/problem/POJ-3041 题目大意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍, 最少要几次. 解 ...

  6. 【题解】POJ3041 Asteroids - 图论 - 二分图匹配

    声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...

  7. POJ-3041

    思路:将n个行看作n个点{x_i}(i=1, ..., n),n个列也看作n个点{y_j}(j=1, ..., n).每个障碍看作一条无向边(x_i, y_j).则该问题能够归结为求二分图最小点覆盖数 ...

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

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

  9. POJ3041 Asteroids(二分图最大匹配)

    题目链接. 分析: 暂略. AC代码: #include <iostream> #include <cstdio> #include <cstring> #incl ...

随机推荐

  1. Android的计量单位px,in,mm,pt,dp,dip,sp

    android中dip.dp.px.sp和屏幕密度 1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持 ...

  2. UVa 10561 (SG函数 递推) Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  3. PHP适合做大型网站吗?

    1. 对递归的不良支持 递归是一种函数调用自身的机制.这是一种强大的特性可以把某些复杂的东西变得很简单.有一个使用递归的例子是快速排序(quicksort).不幸的是,PHP并不擅长递归.Zeev,一 ...

  4. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  5. Android自定义的webView——可实现的网页文本的复制

    package com.example.customlinearlayout.view; import android.app.ProgressDialog; import android.conte ...

  6. 反转链表 --剑指offer

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反正后链表的头结点. #include<stdio.h> #include<malloc.h> typedef str ...

  7. <转>DNS服务系列之二:DNS区域传送漏洞的安全案例

    DNS区域传送(DNS zone transfer)指的是一台备用服务器使用来自主服务器的数据刷新自己的域(zone)数据库.这为运行中的DNS服务提供了一定的冗余度,其目的是为了防止主的域名服务器因 ...

  8. 【原】Storm Local模式和生产环境中Topology运行配置

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  9. 使用MATLAB生成模糊控制的离线查询表

    1.打开模糊控制工具箱,编辑输入输出变量的隶属度函数和模糊控制规则,如下图所示,导出为fuzzy_control.fis文件. 2.打开Simulink模块,建立下图所示的系统框图,两输入,一输出,处 ...

  10. 内核源码分析之软中断(基于3.16-rc4)

    1.和软中断相关的数据结构: softing_vec数组(kernel/softirq.c) static struct softirq_action softirq_vec[NR_SOFTIRQS] ...