Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1666 Accepted Submission(s): 747
Problem Description
Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
Input
There are multiple input cases.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.
Output
For each test case, output the maximum number of remaining dominoes in a line.
Sample Input
2 3 0 0 0 3 0 1 1 1 1 3 4 5 0 1 0 2 3 1 2 2 0 0 1 0 2 0 4 1 3 2 0 0
Sample Output
4 6
横竖建图,跑一遍二分匹配,最大独立集=点数-匹配数
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; #define clr(a,v) memset(a,v,sizeof a) #define rep(i,s,t) for(int i=s;i<t;i++) const int MAXN=1010; int map[MAXN][MAXN],cnt[MAXN]; int vis[MAXN],x[MAXN],y[MAXN]; int a[MAXN],b[MAXN]; int c[MAXN],d[MAXN]; int n,m; inline bool ok(int a,int b,int c,int d){ return (a==c&&b==d); } inline bool Cover(int i,int j){ int x_1=a[i],y_1=b[i]; int x_2=a[i]+1,y_2=b[i]; int x_3=c[j],y_3=d[j]; int x_4=c[j],y_4=d[j]+1; if(ok(x_1,y_1,x_3,y_3) || ok(x_1,y_1,x_4,y_4)) return 1; if(ok(x_2,y_2,x_3,y_3) || ok(x_2,y_2,x_4,y_4)) return 1; return 0; } inline bool dfs(int now){ rep(i,0,cnt[now]){ int v=map[now][i]; if(!vis[v]){ vis[v]=1; if(y[v]==-1 || dfs(y[v])){ x[now]=v; y[v]=now; return 1; } } } return 0; } inline int match(){ clr(x,-1);clr(y,-1); int ans=0; rep(i,0,n){ clr(vis,0); if(dfs(i)) ans++; } return ans; } int main(){ while(cin>>n>>m){ if(n==0 && m==0) break; rep(i,0,n) cin>>a[i]>>b[i]; rep(i,0,m) cin>>c[i]>>d[i]; clr(cnt,0); rep(i,0,n){ rep(j,0,m){ if(Cover(i,j)){ map[i][cnt[i]++]=j; } } } cout<<n+m-match()<<endl; } return 0; }