hdu3041

链接:hdu3041

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

  • 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

    Sample Output

    2

    题解

  • 二分图最小点覆盖
  • 其实就等于二分图最大匹配
  • 数组忘了清零,WA一发,数组开大十倍,TLE一发
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e5+7;
struct node{
int v,nxt;
inline void init(int a,int b){v=a; nxt=b;}
}e[N];
int head[N],cnt=1,vis[N],mark[N],n,m,ans;
template <typename T>inline void read(T &x){
char c;int sign=1;x=0;
do{c=getchar(); if(c=='-')sign=-1;}while(c<'0'||c>'9');
do{x=x*10+c-'0'; c=getchar();}while(c>='0'&&c<='9');
x*=sign;
}

void reade(int a,int b){e[cnt].init(b,head[a]); head[a]=cnt++;}

int find(int u){
for(int i=head[u];i;i=e[i].nxt)if(!vis[e[i].v]){
vis[e[i].v]=1;
if(!mark[e[i].v]||find(mark[e[i].v])){
mark[e[i].v]=u;
return 1;
}
}
return 0;
}

int main(){
while(~scanf("%d%d",&n,&m)){
ans=0;
memset(head,0,sizeof(head));
memset(mark,0,sizeof(mark));
rep(i,1,m){
int x,y;
read(x);read(y);
reade(x,y);
}
rep(i,1,n){
memset(vis,0,sizeof(vis));
ans+=find(i);
}
printf("%d\n",ans);
}
return 0;
}