Warm up 2Time
limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 1895 Accepted Submission (s): 862
Problem Description Some 1x2 dominoes is placed on a plane. Each dominoe is placed either horizontally or vertically. It ' s guaranteed the dominoes in the same direction is not overlapped, but horizontal and vertical dominoes may overlap WI Th each of the other. You task is to remove some dominoes, so, the remaining dominoes does not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
Input there is multiple input cases.
The first line of all case is 2 integers:n (1 <= n <=), m (1 <= m <=), indicating the number of Hor Izontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <=) and y (0 <= y <=), indicating the POS Ition of a horizontal Dominoe. The Dominoe occupies the grids of (x, y) and (x + 1, y).
Then M lines follow contains 2 integers x (0 <= x <=) and y (0 <= y <=), indicating the POS Ition 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
Authorsysu
Source2013 multi-university Training Contest 2
Test instructions: Put n horizontal sticks, m vertically placed sticks in a grid, each stick occupies a 1x2 lattice. Each grid can only be used once and cannot be reused. How many sticks can I put up?
Problem solving: Because each stick occupies only 2 blocks, all the squares are divided into odd and even two parts, X, y two, and the sides represent each stick. This makes it possible to match or use the maximum flow (plus edge <s, X, 1>, <y, T, 1>, maximum capacity of 1 per edge) after the diagram has been built.
The following is the maximum flow solution.
#include <stdio.h> #include <string.h> #include <vector>using namespace std;const int maxn = 10010;// The maximum value of a const int MAXM = 400010;//The maximum value of the number of sides of a const int INF = 0x3f3f3f3f;struct edge{int to,next,cap;} edge[maxm];//Note is maxmint tol;int head[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];void init () {tol = 0; memset (head,-1,sizeof (Head));} Add edge, unidirectional figure three parameters, bidirectional figure four parameters void Addedg (int u,int v,int w,int rw=0) {edge[tol].to = V;edge[tol].cap = W;edge[tol].next = Head[u ]; Head[u] = tol++; edge[tol].to = U;edge[tol].cap = Rw;edge[tol].next = Head[v]; head[v]=tol++;} Input parameters: The number of the start, end point, number of points//points is not affected, as long as the total number of input points int sap (int start,int end,int N) {memset (gap,0,sizeof (GAP)); memset (dep,0,sizeof (DEP)); memcpy (cur,head,sizeof (head)); int u = start; Pre[u] =-1; Gap[0] = N; int ans = 0; while (Dep[start] < N) {if (U = = end) {for (int i = pre[u];i! =-1; i = pre[edge[i^1].to]) {Edge[i].cap-= 1; EDge[i^1].cap + = 1; } u = start; Ans + = 1; Continue } bool flag = FALSE; int V; for (int i = cur[u]; I! = -1;i = edge[i].next) {v = edge[i].to; if (edge[i].cap && dep[v]+1 = = Dep[u]) {flag = true; Cur[u] = pre[v] = i; Break }} if (flag) {u = V; Continue } int Min = N; for (int i = head[u]; I! = -1;i = Edge[i].next) if (Edge[i].cap && dep[edge[i].to] < Min) { Min = dep[edge[i].to]; Cur[u] = i; } gap[dep[u]]--; if (!gap[dep[u]]) return ans; Dep[u] = min+1; gap[dep[u]]++; if (u! = start) u = edge[pre[u]^1].to; } return ans; int main () {int n,m,mapt[105][105],id; int x,y,tx,ty; vector<int>mp[105][105]; while (scanf ("%d%d", &n,&m) {>0) {if (n==0&&m==0) break; memset (mapt,0,sizeof (MAPT)); for (int i=0, i<=102; i++) for (int j=0; j<=102; j + +) Mp[i][j].clear (); Id=0; while (n--) {scanf ("%d%d", &x,&y); if (mapt[x][y]==0) Mapt[x][y]=++id; tx=x+1; Ty=y; if (mapt[tx][ty]==0) Mapt[tx][ty]=++id; if (((x+y) &1) ==0) {mp[x][y].push_back (mapt[tx][ty]); } else Mp[tx][ty].push_back (Mapt[x][y]); } while (m--) {scanf ("%d%d", &x,&y); if (mapt[x][y]==0) Mapt[x][y]=++id; Tx=x; ty=y+1; if (mapt[tx][ty]==0) Mapt[tx][ty]=++id; if (((x+y) &1) ==0) {mp[x][y].push_back (mapt[tx][ty]); } else Mp[tx][ty].push_back (Mapt[x][y]); } init (); int s=0, t=id+1; for (int i=0, i<=102; i++) for (int j=0; j<=102; J + +) if (mapt[i][j]&& ((i+j) &1) ==0) { ADDEDG (S, Mapt[i][j], 1); for (int e=0; e<mp[i][j].size (); e++) {int v=mp[i][j][e]; ADDEDG (mapt[i][j],v,1); }} else if (Mapt[i][j]) addedg (mapt[i][j],t,1); printf ("%d\n", SAP (S,T,T+1)); }}
HDU 4619 Warm up 2 (maximum flow or binary match)