hdu 4619 二分图最大匹配 ——最大独立集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 #define maxn 1001 9 #define N 105 10 using namespace std; 11 12 int map[N][N]; 13 bool G[maxn][maxn]; 14 int n,m; 15 int ans; 16 bool vis[maxn]; 17 int link[maxn]; 18 int rnum,lnum; 19 20 bool match(int u){ 21 for(int v=1;v<rnum;v++){ 22 if(G[u][v] && !vis[v]){ 23 vis[v] = true; 24 if( !link[v] || match(link[v])){ //要么v没有匹配边,要么就递归找; 25 link[v] = u; 26 return true; 27 } 28 } 29 } 30 return false; 31 } 32 void solve(){ 33 memset(link,0,sizeof(link)); 34 ans = 0; 35 for(int i=1;i<lnum;i++){ 36 memset(vis,0,sizeof(vis)); 37 if(match(i)) ans++; 38 } 39 } 40 int main() 41 { 42 //if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 43 44 while(cin>>n>>m){ 45 if(n==0 && m==0) break; 46 memset(G,0,sizeof(G)); 47 memset(map,0,sizeof(map)); 48 lnum = 1; 49 for(int i=1;i<=n;i++){ 50 int x,y; 51 scanf("%d%d",&x,&y); 52 map[x][y] = map[x+1][y] = lnum++; // 53 } 54 rnum = 1; 55 for(int i=1;i<=m;i++){ 56 int x,y; 57 scanf("%d%d",&x,&y); 58 if(map[x][y]){ 59 G[map[x][y]][rnum] = true; 60 } 61 if(map[x][y+1]){ 62 G[map[x][y+1]][rnum] = true; 63 } 64 if(map[x][y] || map[x][y+1]) rnum++; 65 66 } 67 solve(); 68 printf("%d\n",n+m-ans); 69 } 70 }