Bzoj 1741: [usaco Nov] asteroids crossing the minor group

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.