[Usaco2005 Nov]Asteroids
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
INPUT DETAILS:
The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.
Sample Output
2
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).
二分图匹配,对于每个点,它若被行清理过,就不必再次被列清理,反之亦然,然后就是最大匹配
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=5e2;
int path[N+10],pre[N*N+10],now[N+10],child[N*N+10];
int tot,n,m;
bool use[N+10];
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool check(int x){
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (use[son]) continue;
use[son]=1;
if (path[son]<0||check(path[son])){path[son]=x;return 1;}
}
return 0;
}
void work(){
memset(path,-1,sizeof(path));
int res=0;
for (int i=1;i<=n;i++){
memset(use,0,sizeof(use));
if (check(i)) res++;
}
printf("%d\n",res);
}
int main(){
n=read(),m=read();
for (int i=1;i<=m;i++){
int x=read(),y=read();
join(x,y);
}
work();
return 0;
}
[Usaco2005 Nov]Asteroids的更多相关文章
- 1741: [Usaco2005 nov]Asteroids 穿越小行星群
1741: [Usaco2005 nov]Asteroids 穿越小行星群 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 231 Solved: 166 ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案. 代码 #inc ...
- BZOJ 1741: [Usaco2005 nov]Asteroids 穿越小行星群
Description 贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所 ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群 最小点覆盖
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1741 思路 消除所有的小行星 每个点(x,y)只有选择x或者y才能被覆盖 二分图最小点覆盖= ...
- 【BZOJ】1741: [Usaco2005 nov]Asteroids 穿越小行星群
[题意]给定n*n网格,有k个物品,每次可以消灭一行或一列,求消灭掉所有物品的最少操作次数. [算法]二分图最小覆盖 [题解]此题是最小覆盖模型的出处. 将物品的x-y连边建立二分图. 最小覆盖:选择 ...
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证) #include<iostream> #include<cstdi ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
- BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )
dp... dp( l , r , k ) , 表示 吃了[ l , r ] 的草 , k = 1 表示最后在 r 处 , k = 0 表示最后在 l 处 . ------------------- ...
- 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 85 Solved: 40[S ...
随机推荐
- Knockout.js用jquery的val设置值不更新
用如下方法,加上change() .val("blah").change()
- Maven创建项目时出现Generating project in Interactive mode就一直卡住的解决方案
使用maven命令在创建项目的时候出现 Generating project in Interactive mode 然后就一直卡住 网上搜做了很多解决方案 有说各种方案的,最后找到了一种.实验成功 ...
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
A literal type is a type that represents exactly one value, e.g. one specific string or number. You ...
- VS2015 android 设计器不能可视化问题解决。
近期安装了VS2015,体验了一下android 的开发,按模板创建执行了个,试下效果非常不错.也能够可视化设计.但昨天再次打开或创建一个android程序后,设计界面直接不能显示,显示错误:(可能是 ...
- Deepin-安装node
点击下载:Linux x64 文件解压: 方式1$xz -d file.tar.xz $tar -xvf file.tar 方式2 $tar xvJf file.tar.xz 解压后,把它移动到:/u ...
- 微信小程序之 Classify(商品属性分类)
1.项目目录 2.逻辑层 broadcast.js // pages/broadcast/broadcast.js Page({ /** * 页面的初始数据 */ data: { firstIndex ...
- PHP 按位与或 (^ 、&)
今天朋友群里朋友问了下 按位与或的问题.. 按位于主要是对二进制数操作. <?php $a = 1; $b = 2; $c = $a^b; ?> 这里不是单纯的相加关系 十进制 1换算成二 ...
- 点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965981 1. 安装jdk,tomcat sudo apt-get instal ...
- 原生js 平滑滚动到页面的某个位置
window.scrollTo() 语法1: window.scrollTo(x-coord,y-coord) x-coord 是文档中的横轴坐标. y-coord 是文档中的纵轴坐标. 例子: w ...
- Python爬虫开发【第1篇】【爬虫案例】
案例一:网站模拟登录 # douban.py from selenium import webdriver from selenium.webdriver.common.keys import Key ...