Warm up 2
Time Limit: 3000/1000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 1754 accepted submission (s): 781
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.
Output for each test case, output the maximum number of remaining dominoes in a line.
Sample Input
2 30 00 30 11 11 34 50 10 23 12 20 01 02 04 13 20 0
Sample output
46
Source2013 multi-university training Contest 2
A domino card occupies two grids, some occupy two horizontal grids, some occupy two vertical grids, and some of these dominoes overlap. Now we need to move to overlap, so we can leave a few dominoes at most.
Idea: a domino card occupies two grids and connects one side. Find the maximum match. OK!
#include"stdio.h"#include"string.h"#include"queue"using namespace std;#define N 105#define M 4005int mark[M],link[M],g[N][N];int n,m,x,y,t;int head[M];int dir[4][2]={0,1,0,-1,-1,0,1,0};struct node{ int u,v,next;}map[M];int max(int a,int b){ return a>b?a:b;}void add(int u,int v){ map[t].u=u; map[t].v=v; map[t].next=head[u]; head[u]=t++;}int find(int k){ int i,v; for(i=head[k];i!=-1;i=map[i].next) { v=map[i].v; printf("%d ",v); if(!mark[v]) { mark[v]=1; if(link[v]==-1||find(link[v])) { link[v]=k; return 1; } } } return 0;}int main(){ int i,j,u,v,cnt; while(scanf("%d%d",&n,&m),n||m) { memset(g,-1,sizeof(g)); memset(map,0,sizeof(map)); memset(head,-1,sizeof(head)); t=x=y=0; cnt=0; for(i=0;i<n;i++) { scanf("%d%d",&u,&v); x=max(x,u+1);y=max(y,v); if(g[u][v]==-1) { g[u][v]=cnt++; } if(g[u+1][v]==-1) { g[u+1][v]=cnt++; } add(g[u][v],g[u+1][v]); add(g[u+1][v],g[u][v]); } for(i=0;i<m;i++) { scanf("%d%d",&u,&v); x=max(x,u);y=max(y,v+1); if(g[u][v]==-1) { g[u][v]=cnt++; } if(g[u][v+1]==-1) { g[u][v+1]=cnt++; } add(g[u][v],g[u][v+1]); add(g[u][v],g[u][v+1]); } x++;y++; int ans=0; for(i=0;i<cnt;i++) { memset(mark,0,sizeof(mark)); printf("%d :",i); ans+=find(i); puts(""); } printf("%d\n",ans/2); } return 0;}