博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asteroids(二分图最大匹配模板题)
阅读量:5092 次
发布时间:2019-06-13

本文共 2260 字,大约阅读时间需要 7 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12323   Accepted: 6716

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 41 11 32 23 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).
1 #include
2 #include
3 int map[510][510]; 4 int link[510]; 5 int vis[510]; 6 int n; 7 bool dfs(int u) 8 { 9 int v;10 for(v = 1; v <= n; v++)11 {12 if(map[u][v] && !vis[v])13 {14 vis[v] = 1;15 if(link[v] == -1 || dfs(link[v]))16 {17 link[v] = u;18 return true;19 }20 }21 }22 return false;23 }24 int main()25 {26 27 int k,u,v,res;28 memset(map,0,sizeof(map));29 scanf("%d %d",&n,&k);30 while(k--)31 {32 scanf("%d %d",&u,&v);33 map[u][v] = 1;34 }35 memset(link,-1,sizeof(link));36 res = 0;37 for(u = 1; u <= n; u++)38 {39 memset(vis,0,sizeof(vis));40 if(dfs(u)) res++;41 }42 printf("%d\n",res);43 return 0;44 45 46 }
View Code

 

转载于:https://www.cnblogs.com/LK1994/p/3246755.html

你可能感兴趣的文章
为什么div设置其border无效?
查看>>
给博客园添加live2d看板娘(转)
查看>>
防抖 && 节流
查看>>
窗口实训1
查看>>
LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
查看>>
博客开通罗
查看>>
在vue中使用axios发送post请求,参数方式
查看>>
POJ 1026 Cipher
查看>>
Android解决异常apk on device '0292bea1': Unable to open sync connection!
查看>>
malloc分配的内存空间是连续的吗
查看>>
Rsync服务及搭建备份服务器
查看>>
网络、邻居、共享
查看>>
探索WebKit内核(一)------ 菜鸟起步
查看>>
chromium中的性能优化工具syzyProf
查看>>
标准文档流
查看>>
EL标签学习
查看>>
我的每日所学
查看>>
Thrift之代码生成器Compiler原理及源码详细解析2
查看>>
HTML列表
查看>>
THUSC2016 游记
查看>>