Solution report
Question:
Given the nxn matrix, M points are obstacles
Only one row or column can be deleted at a time. The obstacle can be cleared at least once.
Ideas:
Think of rows and columns as two collection nodes, and view obstacles as the links of the collection nodes. This converts the rows and columns into the minimum vertices to eliminate the edge, that is, the minimum vertices overwrite.
In the bipartite graph: (n nodes with no isolated points)
Minimum vertex overwrite = maximum match
Maximum vertex independence = knots-maximum match
#include <queue>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#define N 510using namespace std;int mmap[N+N][N+N],n,k,vis[N+N],pre[N+N];int dfs(int x){ int i; for(i=n+1; i<=n+n; i++) { if(!vis[i]&&mmap[x][i]) { vis[i]=1; if(pre[i]==-1||dfs(pre[i])) { pre[i]=x; return 1; } } } return 0;}int main(){ int i,j,x,y; while(cin>>n>>k) { memset(mmap,0,sizeof(mmap)); memset(pre,-1,sizeof(pre)); for(i=0; i<k; i++) { cin>>x>>y; mmap[x][n+y]=1; } int ans=0; for(i=1; i<=n; i++) { memset(vis,0,sizeof(vis)); ans+=dfs(i); } cout<<ans<<endl; }}
Asteroids
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:14694 |
|
Accepted:8016 |
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 dimo-represents the data, where "X" is an asteroid and "." Is empty space:
X. x
. X.
. X.
Output details:
Bessie may fire your SS Row 1 to destroy the asteroids at () and (), and then she may fire down column 2 to destroy the asteroids at () and ).
Source
Usaco 2005 November gold