HDU 4619 (二分图匹配 最大独立集)
来源:互联网 发布:大数据与信息安全论文 编辑:程序博客网 时间:2024/05/22 17:08
Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2052 Accepted Submission(s): 938
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.
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 30 00 30 11 11 34 50 10 23 12 20 01 02 04 13 20 0
Sample Output
46
题意是给你n个横向的骨牌和m个纵向的骨牌,保证各自方向的骨牌不相交,问最少拿掉多少张骨牌使得所有的骨牌没有相交的情况.骨牌的长度是1.
用到了二分图的一个性质,二分图的最大独立集=所有的节点数-二分图最大匹配.
正好横向纵向两类点,如果相交就连边,然后就求个最大匹配就好了.
#include <bits/stdc++.h>using namespace std;#define maxn 2111#define maxm 2111111int n, m;int a[maxn][2], b[maxn][2];struct node { int from, to, next;}edge[maxm];int head[maxn];int cnt;void add_edge (int from , int to) { edge[cnt].from = from, edge[cnt].to = to, edge[cnt].next = head[from], head[from] = cnt++;}bool jiao (int i, int j) { if (a[i][0] == b[j][0] && a[i][1] == b[j][1]) return 1; if (a[i][0]+1 == b[j][0] && a[i][1] == b[j][1]) return 1; if (a[i][0] == b[j][0] && a[i][1] == b[j][1]+1) return 1; if (a[i][0]+1 == b[j][0] && a[i][1] == b[j][1]+1) return 1; return 0;}int pre[maxn], vis[maxn];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 (pre[v] == -1 || dfs (pre[v])) { pre[v] = u; return 1; } } } return 0;}int hungry () { int ans = 0; memset (pre, -1, sizeof pre); for (int i = 1; i <= n; i++) { memset (vis, 0, sizeof vis); if (dfs (i)) ans++; } return ans;}int main () { //freopen ("in", "r", stdin); ios::sync_with_stdio (0); while (cin >> n >> m && n+m) { cnt = 0; memset (head, -1, sizeof head); for (int i = 1; i <= n; i++) { cin >> a[i][0] >> a[i][1]; } for (int j = 1+n; j <= n+m; j++) { cin >> b[j][0] >> b[j][1]; for (int i = 1; i <= n; i++) { if (jiao (i, j)) { add_edge (i, j); add_edge (j, i); } } } cout << n+m-hungry () << endl; } return 0;}
0 0
- HDU 4619 (二分图匹配 最大独立集)
- hdu 1068 极大独立集 二分图最大匹配
- 二分图最大匹配 & 最大独立集
- poj3020 二分图匹配 最大独立集
- 二分图匹配 --- 最大独立集
- hdu 2768 Cat vs. Dog(二分图最大匹配--最大独立点集)
- HDU 3829 Cat VS Dog(最大独立集|二分图最大匹配)
- HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】
- hdu 1068 Girls and Boys(最大独立集,二分匹配)
- hdu 2768 (二分匹配,最大独立集)
- hdu 3829 二分图最大独立集
- hdu 3829 二分图最大独立集
- hdu 1068(二分图最大独立集)
- hdu 1068 二分图 最大独立集
- 最大独立集,二分图最大匹配 poj 1466
- 二分图:最大独立集&最大匹配&最小顶点覆盖
- HDU2768Cat vs. Dog二分图 最大独立集(最大匹配)
- hdu 4619—二分图求最大独立集
- Linux内核的信号机制——怪胎
- LeetCode 145 Binary Tree Postorder Traversal
- IGMP Snooping
- 安卓Android控件ListView获取item中EditText值
- C++继承总结
- HDU 4619 (二分图匹配 最大独立集)
- PHP自带邮件发送配置
- MyBatis 学习笔记一
- Github添加SSH Key
- POJ 1797 Heavy Transportation
- group by having where
- hdu 2639 Bone Collector II 01背包 第k优解
- mysql数据库学习
- C 读写文件