UESTC Training for Graph Theory——B、Asteroids

Asteroids

Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 123 Tried: 470

Description

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.

Input

The first line contains a single integer t (1 <= t <= 11), the number of test cases.
Each case begins with two integers N and K, separated by a single space.Then K lines follows,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

For each case, print an integer representing the minimum number of times Bessie must shoot.

Sample Input

1
3 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.

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

Source

USACO 2005 November Gold

 

 

 

 

 

 

 

 

 

 

 

  

/*算法思想:
  给一个矩形,矩形中有一些陨石,每次扫射只能扫射一行或者一列,问最少需要多少次扫射
  这道题开始的时候没有半点思路,枚举不现实,必TLE,但是看着过的人那么多,就觉得一定
  是简单题,结果等老师们讲了之后才明白过来,原来是最小点覆盖,而且还有一个神奇的定理:
          最小点覆盖数=最大匹配数。
  这道题构图的方法也有点巧妙:假设(i,j)有一个陨石,那么左边集合中的第 i 个点和右边
  集合中的第 j 个点连一条边,表示能够匹配,这样的话,整个二分图就构成了,求这个二分图
  的最大匹配就是最终的答案了。求匹配的方法是用的匈牙利算法。找增广链,找到一条,匹配数
  就+1,要是找不到了,那么当前的匹配数就是最大匹配数。
*/

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 600
using namespace std;
struct data
{
    int st,en,next;
} edge[N*N];
int head[N],tot,n;
int joint[N];
bool use[N],fg[N];
void add_edge(int st,int en)  //加边
{
    edge[tot].st=st;
    edge[tot].en=en;
    edge[tot].next=head[st];
    head[st]=tot++;
}
bool find(int u)  //寻找增广链
{
    if(fg[u]) return false;
    fg[u]=true;
    for(int pos=head[u];pos!=-1;pos=edge[pos].next)
        if(!joint[edge[pos].en] || find(joint[edge[pos].en]))
        {
            joint[edge[pos].en]=u; //找到了,原路返回,并更新与右边点集相匹配的点
            return true;
        }
    return false;
}
int max_match()
{
    int ans=0;
    memset(use,0,sizeof(use));
    memset(joint,0,sizeof(joint));
    while(1)
    {
        memset(fg,0,sizeof(fg));
        bool flag=false;
        for(int i=1;i<=n;i++)
            if(!use[i] && find(i))  //能找到一条增广链,更新最大匹配 ans 的值
            {
                ans++;
                use[i]=true;
                flag=true;
                break;
            }
        if(!flag) return ans;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int k,r,c;
        memset(head,-1,sizeof(head));
        tot=0;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&r,&c);
            add_edge(r,c);
        }
        printf("%d\n",max_match());
    }
    return 0;
}


 

你可能感兴趣的:(UESTC Training for Graph Theory——B、Asteroids)