题解: 本题中的所有顶点可以分成水平方向和竖直方向的攻击选择两类,每颗小行星所对应的边都分别与一个水平方向和一个竖直方向的顶点相连,所以是一个二分图,本题正好是求得二分图的最大匹配。在图论中,图中两两不含公共顶点的边的集合M称为匹配,儿元素最多的M称为最大匹配。
/***********************************************
* Author: fisty
* Created Time: 2015-08-16 21:23:21
* File Name : poj3041.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1
#define MAX_N 11000
int N, K;
int R[MAX_N], C[MAX_N];
int V;
vector<int> G[MAX_N];
int match[MAX_N];
bool used[MAX_N];
void add_edge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v){
used[v] = true;
for(int i = 0;i < G[v].size(); i++){
int u = G[v][i], w = match[u];
if(w < 0 || !used[w] && dfs(w)){
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching(){
int res = 0;
memset(match, -1, sizeof(match));
for(int v = 0;v < V; v++){
if(match[v] < 0){
memset(used, 0, sizeof(used));
if(dfs(v)){
res++;
}
}
}
return res;
}
void solve(){
V = N * 2;
for(int i = 0;i < K; i++){
add_edge(R[i]-1, N + C[i]-1);
}
printf("%d\n", bipartite_matching());
}
int main() {
//freopen("in.cpp", "r", stdin);
//cin.tie(0);
//ios::sync_with_stdio(false);
while(~scanf("%d%d", &N, &K)){
for(int i = 0;i < K; i++){
scanf("%d%d", &R[i], &C[i]);
}
solve();
}
return 0;
}