Asteroids----------求二分图的最小顶点覆盖问题

                                                      Asteroids

时间限制(普通/Java):3000MS/10000MS          运行内存限制:65536KByte
总提交:34            测试通过:23

描述

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.

 

输入

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

 

输出

* Line 1: The integer representing the minimum number of times Bessie must shoot.

 

样例输入

 

3 4
1 1
1 3
2 2
3 2

 

样例输出

 

2
#include<iostream> #include<vector> using namespace std; int N,K; int a[501][501]; int pre[501]; int x,y; vector< vector<int> >adj; bool deal(int i,bool flag[]) { int j,temp,k; for(j=0;j<adj[i].size();j++) { k=adj[i][j]; if(!flag[k]) { flag[k]=true; temp=pre[k]; pre[k]=i; if(!temp||deal(temp,flag)) { return true; } pre[k]=temp; } } return false; } void hungray() { int i; int sum=0; bool flag[501]={0}; for(i=1;i<=N;i++) { memset(flag,0,sizeof(flag)); if(deal(i,flag)) sum++; } cout<<sum<<endl; } int main() { int i,j; cin>>N>>K; memset(pre,0,sizeof(pre)); adj.assign(N+1,vector<int>()); for(i=1;i<=K;i++) { cin>>x; cin>>y; adj[x].push_back(y); } hungray(); return 0; }

你可能感兴趣的:(Asteroids----------求二分图的最小顶点覆盖问题)