HDU4619--Warm up 2

Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 602 Accepted Submission(s): 306


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 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

求不交叉最大的数量,求最大独立集合=N+M-最大匹配。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int g[1001][1001];
 9 int link[1001][1001];
10 int cx[1001];
11 int cy[1001];
12 int mk[1001];
13 int n,m;
14 
15 void init()
16 {
17     memset(cx,0xff,sizeof(cx));
18     memset(cy,0xff,sizeof(cy));
19     memset(link,0,sizeof(link));
20     memset(g,0,sizeof(g));
21 }
22 
23 int path(int u)
24 {
25     int v;
26     for(v=0;v<=m;v++)
27     {
28         if(!mk[v]&&link[u][v])
29         {
30             mk[v]=1;
31             if(cy[v]==-1||path(cy[v]))
32             {
33                 cx[u]=v;
34                 cy[v]=u;
35                 return 1;
36             }
37         }
38     }
39     return 0;
40 }
41 
42 int maxmatch()
43 {
44     int i;
45     int sum=0;
46     for(i=0;i<=n;i++)
47     {
48         if(cx[i]==-1)
49         {
50             memset(mk,0,sizeof(mk));
51             sum+=path(i);
52         }
53     }
54     return sum;
55 }
56 
57 int main()
58 {
59     while(scanf("%d%d",&n,&m)!=EOF)
60     {
61         init();
62         if(!n&&!m)break;
63         int i;
64         for(i=1;i<=n;i++)
65         {
66             int s,e;
67             scanf("%d%d",&s,&e);
68             g[s][e]=-i;
69             g[s+1][e]=-i;
70         }
71         for(i=1;i<=m;i++)
72         {
73             int s,e;
74             scanf("%d%d",&s,&e);
75             if(g[s][e]<0)
76             {
77                 g[s][e]=-g[s][e];
78                 link[g[s][e]][i]=1;
79             }
80             if(g[s][e+1]<0)
81             {
82                 g[s][e+1]=-g[s][e+1];
83                 link[g[s][e+1]][i]=1;
84             }
85             g[s][e]=i;
86             g[s][e+1]=i;
87         }
88         int ans=maxmatch();
89         printf("%d
",n+m-ans);
90     }
91     return 0;
92 }
View Code
原文地址:https://www.cnblogs.com/zafuacm/p/3217997.html