Asteroids

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 16242 Accepted: 8833

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

题意

给你一个n*n的方阵,然后有一些东西在一些坐标上,你需要用最少的炸弹把这些东西都炸掉,然后问你,最少炸弹数量是多少

题解

我们建立一个二分图,然后把每行当成一个点,把每列当成一个点,如果这一行这一列有炸弹的话,我们就建一条边。那么这道题,就转化成了,最小点覆盖问题,直接跑一大最大匹配就好了

总而言之,言而总之,我也是套版做的= =

代码

#define REP(i, n) for (int i=0;i<n;++i)
#define RD(n) scanf("%d",&n)
int V;
vector<int> G[maxn];
int match[maxn];
int used[maxn]; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int v)
{
used[v]=1;
for(int i=0;i<G[v].size();i++)
{
int u=G[v][i],w=match[u];
if(w<0||!used[w]&&dfs(w))
{
match[v]=u;
match[u]=v;
return 1;
}
}
return 0;
} int bipartite_matching()
{
int res=0;
memset(match,-1,sizeof(match));
for(int v=0;v<V;v++)
{
if(match[v]<0)
{
memset(used,0,sizeof(used));
if(dfs(v))
res++;
}
}
return res;
} int n,k;
int R[maxn],C[maxn];
void solve()
{
V=n*2;
for(int i=0;i<k;i++)
{
add_edge(R[i]-1,n+C[i]-1);
}
printf("%d\n",bipartite_matching());
} int main()
{
scanf("%d%d",&n,&k);
REP(i,k)
{
RD(R[i]);
RD(C[i]);
}
solve();
return 0;
}

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 (最小点覆盖集)

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

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

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

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

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

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

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

  6. Asteroids POJ - 3041 【最小点覆盖集】

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  7. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  8. POJ 3041 Asteroids 最小覆盖数

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

  9. POJ 3041(最小点覆盖)

    题意: 假如你如今正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物,你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭所有障碍物 输入为:     N K 接下来有K行,每行包括 ...

随机推荐

  1. 【Linux技术】ubuntu常用命令【转】

    转自:http://www.cnblogs.com/lcw/p/3159462.html 查看软件xxx安装内容:dpkg -L xxx查找软件库中的软件:apt-cache search 正则表达式 ...

  2. csv导入mysql提示错误[Error Code] 1290 - The MySQL server is running with the --secure-file-priv option 解决方法【转】

    解决方法: 1.进入mysql查看secure_file_prive的值 mysql>SHOW VARIABLES LIKE "secure_file_priv"; secu ...

  3. Codeforces 946D Timetable(预处理+分组背包)

    题目链接:http://codeforces.com/problemset/problem/946/D 题目大意:有n个字符串,代表n天的课表,1表示这个时间要上课,0表示不要上课,一天在学校时间为第 ...

  4. JAVA 解析TXT文本

    package file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  5. sublime text配置make工具

    sublime text配置make工具 Linux下许多项目是用makefile来管理的,是用gcc+make等方式来编译和运行. 在只有tty的场合或年代,使用vim或emacs是不二选择:但在L ...

  6. appium----adb shell输入中文/Emoji表情符(ADBKeyBoard)

    前序 “adb shell input textyoyo“ 可以通过adb 输入英文的文本,由于不支持unicode编码,所以无法输入中文,github上有个国外的大神写了个ADBKeyBoard输入 ...

  7. 浅谈C#中的值类型和引用类型

    在C#中,值类型和引用类型是相当重要的两个概念,必须在设计类型的时候就决定类型实例的行为.如果在编写代码时不能理解引用类型和值类型的区别,那么将会给代码带来不必要的异常.很多人就是因为没有弄清楚这两个 ...

  8. Ansible实战:部署分布式日志系统

    本节内容: 背景 分布式日志系统架构图 创建和使用roles JDK 7 role JDK 8 role Zookeeper role Kafka role Elasticsearch role My ...

  9. .NET异步多线程,Thread,ThreadPool,Task,Parallel,异常处理,线程取消

    今天记录一下异步多线程的进阶历史,以及简单的使用方法 主要还是以Task,Parallel为主,毕竟用的比较多的现在就是这些了,再往前去的,除非是老项目,不然真的应该是挺少了,大概有个概念,就当了解一 ...

  10. Loadrunner参数化逗号报错解决方法

    Loadrunner参数化逗号报错解决方法     介绍Loadrunner参数化时,参数中包含有逗号时出错的解决方法. 在Loadrunner进行参数化时,参数中如果含有逗号,编辑保存后会报错: 此 ...