[二分匹配]Asteroids
阅读原文时间:2023年07月08日阅读:1

A

s

t

e

r

o

i

d

s

Asteroids

Asteroids

=================================================


题目描述

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

题目解析

首先我们看题,是求一个最小的次数。
然后不会网络流的我只好用二分匹配
这道题其实和最大匹配 人员分配一模一样,就只是换了个题面而已。


code

#include<stdio.h>

#include<iostream> 

#include<string.h>

using namespace std;

int n, m, s, q, tot, ans;
int head[10005], link[1005], vis[1005];

struct node
{
    int next,to;
}f[10005];

void add (int x, int y)
{
    f[ ++ tot].next = head[x];
    f[tot].to = y;
    head[x] = tot;
}

bool find (int x)
{
    for (int i = head[x]; i; i = f[i].next)
    {
        int j = f[i].to;
        if ( ! vis[j])
        {
            q = link[j];
            link[j]=x;
            vis[j]=1;
            if ( ! q || find(q)) return 1;
            link[j] = q;
        }

    }
    return 0;

}

int main ()
{
    scanf ("%d%d", &m, &n);
    for (int i=1; i<=n; ++i)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add (a,b);
    }
    for (int i=1; i<=n; ++i)
    {
        memset (vis, 0, sizeof(vis));
        ans += find(i);
    }
    printf ("%d", ans);
    return 0;
}