hdu 4619 Warm up 2(最大独立点集,二分匹配,4级)
Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 449 Accepted Submission(s): 225
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
Source
Recommend
zhuyuanchen520
思路:矛盾的点加边,求个最大独立点集,就是所有点数减去最大匹配。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int mm=3009; 6 class node 7 { 8 public:int x,y; 9 }f[mm]; 10 int head[mm],edge; 11 class knode 12 { 13 public:int v,next; 14 }e[mm*mm]; 15 int n,m; 16 int Y[mm];bool T[mm]; 17 void data() 18 { 19 memset(head,-1,sizeof(head));edge=0; 20 } 21 void add(int u,int v) 22 { 23 e[edge].v=v;e[edge].next=head[u];head[u]=edge++; 24 } 25 bool check(int i,int j) 26 { 27 if(f[i].x==f[j].x&&f[i].y==f[j].y)return 1; 28 if(f[i].x+1==f[j].x&&f[i].y==f[j].y)return 1; 29 if(f[i].x==f[j].x&&f[i].y==f[j].y+1)return 1; 30 if(f[i].x+1==f[j].x&&f[i].y==f[j].y+1)return 1; 31 return 0; 32 } 33 bool dfs(int u) 34 { 35 int v; 36 for(int i=head[u];~i;i=e[i].next) 37 { 38 v=e[i].v; 39 if(T[v])continue;T[v]=1; 40 if(Y[v]==-1||dfs(Y[v])) 41 { 42 Y[v]=u;return 1; 43 } 44 } 45 return 0; 46 } 47 int getans() 48 { memset(Y,-1,sizeof(Y)); 49 int ret=0; 50 for(int i=1;i<=n;++i) 51 { 52 memset(T,0,sizeof(T)); 53 if(dfs(i)) 54 ++ret; 55 } 56 return ret; 57 } 58 int main() 59 { 60 while(~scanf("%d%d",&n,&m)) 61 { if(n==0&&m==0)break; 62 for(int i=1;i<=n+m;++i) 63 scanf("%d%d",&f[i].x,&f[i].y); 64 data(); 65 for(int i=1;i<=n;++i) 66 for(int j=n+1;j<=n+m;++j) 67 if(check(i,j)) 68 add(i,j); 69 int ans=n+m-getans(); 70 printf("%d\n",ans); 71 } 72 return 0; 73 }
The article write by nealgavin