Description
Bessie wants to drive her ship through a dangerous minor group. the Group is an nxn grid (1 ≤ n ≤ 500). There are K planets in the grid (1 ≤ k ≤ 10000 ). fortunately, Bessie has a very powerful weapon that can eliminate all the minor planets in one row or column at a time. This weapon is very expensive, so she wants to use as little as possible. the positions of all the minor planets are given, and the minimum number of shooting attempts by Bessie can be taken to eliminate all the minor planets. input
Row 1st: two integers, N and K, separated by a space.
2nd rows to k + 1 rows: each row has two spaces separated integer r, C (1 ≤ r, c ≤ n), representing the row and column of the minor respectively. Output
An integer indicates the minimum number of fires required by Bessie, which can eliminate all the minor planets.
Question:
A bipartite graph with two column points representing rows and columns respectively.
For a vertex (x, y) from X to Y.
The minimum coverage is the answer.
Code:
#include<cstdio>#include<algorithm>#include<cstring>//by zrt//problem:using namespace std;typedef long long ll;const double eps=1e-9;int H[505],P[10005],X[10005],tot;int n,k;inline void add(int x,int y){ P[++tot]=y;X[tot]=H[x];H[x]=tot;}bool cover[505];int link[505];int ans;bool find(int k){ for(int i=H[k];i;i=X[i]){ if(!cover[P[i]]){ int q=link[P[i]]; link[P[i]]=k; cover[P[i]]=1; if(q==-1){ ans++;return true; } if(find(q)) return true; link[P[i]]=q; } } return false;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%d%d",&n,&k); for(int i=0,x,y;i<k;i++){ scanf("%d%d",&x,&y); add(x,y); } memset(link,-1,sizeof link); for(int i=1;i<=n;i++){ memset(cover,0,sizeof cover); find(i); } printf("%d\n",ans); return 0;}
Bzoj 1741: [usaco Nov] asteroids crossing the minor group