[Usaco2005 Nov]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 4
1 1
1 3
2 2
3 2

INPUT DETAILS:
The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.
Sample Output
2
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).

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
 
int n,k,tot,ans,now[501],prep[10001],son[10001],match[501];
bool v[501];
 
void add(int,int);
void add(int x,int y)
{
     tot++;
     prep[tot]=now[x];
     now[x]=tot;
     son[tot]=y;
}
 
bool find(int);
bool find(int x)
{
     int p,so;
     p=now[x];
     while (p!=0)
     {
             so=son[p];
             if (v[so]==false)
             {
                  v[so]=true;
                  if (match[so]==0||find(match[so]))
                  {
                          match[so]=x;
                          return true;
                  }
             }
             p=prep[p];
     }
     return false;
}
 
int main()
{
    int a,b;
    scanf("%d%d",&n,&k);
    memset(now,0,sizeof(now));
    tot=0;
    for (int i=1;i<=k;i++)
    {
          scanf("%d%d",&a,&b);
          add(a,b);
    }
    ans=0;
    for (int i=1;i<=n;i++)
    {
          memset(v,0,sizeof(v));
          if (find(i))
          {
                 ans++;
          }
    }
    printf("%d
",ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/cutemush/p/12738259.html