hdu 4619 Warm up 2

本文从WordPress迁移而来, 查看全部WordPress迁移文章

二分图 最大独立集

题意:给多个1*2的多骨诺派,有些横着放有些竖着放,横着的牌不会相互覆盖,竖着的也不会,但是横的和竖的则可能会。选出最多的牌,相互不覆盖,最多能选几个

把横着的牌看做X部,竖着的牌看住Y部,横牌和竖牌有重叠的连一条边,可知X,Y内部是不会有边的,所以得到了一个二分图模型。选择最多的牌不重叠也就是最大独立集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;
#define N 1010
#define M 1000010
#define pii pair<int,int>
#define cl(xx,yy) memset((xx),(yy),sizeof((xx)))

int n,m,tot,head[N];
struct edge{
int v,next;
}e[M];
struct dom{
int x,y;
}p[N],q[N];
int match[N];
bool vis[N];

inline void add(int u,int v){
e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;
}

inline bool check(int i,int j){
pii a,b,c,d;
a.first = p[i].x ; a.second = p[i].y;
b.first = p[i].x + 1 ; b.second = p[i].y;
c.first = q[j].x; c.second = q[j].y;
d.first = q[j].x; d.second = q[j].y + 1;
if(a == c || a == d || b == c || b == d)
return true;
return false;
}

int dfs(int u){
for(int k = head[u]; k != -1; k = e[k].next){
int v = e[k].v;
if(!vis[v]){
vis[v] = true;
if(match[v] == -1 || dfs(match[v])){
match[v] = u; return 1;
}
}
}
return 0;
}

void MaxMatch(){
int ans = 0;
cl(match,-1);
for(int i = 1; i <= n; i++){
cl(vis,false);
ans += dfs(i);
}
printf("%d\n",n+m-ans);
}

int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(!n && !m) break;
for(int i = 1; i <= n; i++)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i = 1; i <= m; i++)
scanf("%d%d",&q[i].x,&q[i].y);
tot = 0; cl(head,-1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(check(i,j))
add(i,j);
MaxMatch();
}
return 0;
}