您的位置:首页 > 其它

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

2013-07-26 00:23 309 查看
题目链接: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

;
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=1;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,0,sizeof(link));
ans = 0;
for(int i=1;i<lnum;i++){
memset(vis,0,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==0 && m==0) break;
memset(G,0,sizeof(G));
memset(map,0,sizeof(map));
lnum = 1;
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
map[x][y] = map[x+1][y] = lnum++; //
}
rnum = 1;
for(int i=1;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+1]){
G[map[x][y+1]][rnum] = true;
}
if(map[x][y] || map[x][y+1])    rnum++;

}
solve();
printf("%d\n",n+m-ans);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: