题目链接:
HDU 4619 Warm up 2
题意:
有n个水平方向放置和m个竖直方向放置的骨牌,这些骨牌在各自的方向上不会重叠,但是水平向上和竖直方向上的骨牌可能互相重叠,重叠的骨牌只能保留一个,问最多可以留下多少骨牌?
分析:
把每个纸牌看成一个点,将水平方向和竖直方向重叠的纸牌建边使其相邻,题意即保留最多的点使得两两不相邻,这就是最大独立集啊.最大独立集 = 顶点数 - 最大匹配数.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 1010;int n, m, total;
int vis[MAX_N], match[MAX_N], head[MAX_N];struct Edge{int to,next;Edge() {}Edge(int _to, int _next) : to(_to), next(_next) {}
}edge[MAX_N * MAX_N];struct Domino{int x,y;
}hor[MAX_N], ver[MAX_N];inline void AddEdge(int from, int to)
{edge[total].to = to;edge[total]. next = head[from];head[from] = total++;
}inline bool dfs(int u)
{for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(!vis[v]){vis[v] = 1;if(match[v] == -1 || dfs(match[v])){match[v] = u;return true;}}}return false;
}inline int Hungary()
{memset(match, -1, sizeof(match));int res = 0;for(int i = 0; i < n; i++){memset(vis, 0, sizeof(vis));if(dfs(i)) res++;}return res;
}inline bool link(int i, int j)
{if((hor[i].x == ver[j].x || hor[i].x + 1 == ver[j]. x) && (hor[i].y == ver[j].y || hor[i].y == ver[j].y + 1))return true;else return false;
}int main()
{IOS;while(cin >> n >> m && (n || m)){for(int i = 0; i < n; i++){cin >> hor[i].x >> hor[i].y;}for(int i = 0; i < m; i++){cin >> ver[i].x >> ver[i].y;}memset(head, -1, sizeof(head));total = 0;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(link(i, j)){AddEdge(i, j);}}}int ans = Hungary();cout << n + m - ans << endl;}return 0;
}