hdu 4619 二分图最大匹配 ——最大独立集

时间:2023-03-09 23:00:39
hdu 4619 二分图最大匹配 ——最大独立集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#define maxn 1001
#define N 105
using namespace std; int map[N][N];
bool G[maxn][maxn];
int n,m;
int ans;
bool vis[maxn];
int link[maxn];
int rnum,lnum; bool match(int u){
for(int v=;v<rnum;v++){
if(G[u][v] && !vis[v]){
vis[v] = true;
if( !link[v] || match(link[v])){ //要么v没有匹配边,要么就递归找;
link[v] = u;
return true;
}
}
}
return false;
}
void solve(){
memset(link,,sizeof(link));
ans = ;
for(int i=;i<lnum;i++){
memset(vis,,sizeof(vis));
if(match(i)) ans++;
}
}
int main()
{
//if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} while(cin>>n>>m){
if(n== && m==) break;
memset(G,,sizeof(G));
memset(map,,sizeof(map));
lnum = ;
for(int i=;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
map[x][y] = map[x+][y] = lnum++; //
}
rnum = ;
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
if(map[x][y]){
G[map[x][y]][rnum] = true;
}
if(map[x][y+]){
G[map[x][y+]][rnum] = true;
}
if(map[x][y] || map[x][y+]) rnum++; }
solve();
printf("%d\n",n+m-ans);
}
}