POJ 2289 Jamie's Contact Groups 多重匹配+二分

一、内容

Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only one of those groups and the size of the largest group is minimized. 

Input

There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the last test case, there is a single line `0 0' that terminates the input. 

Output

For each test case, output a line containing a single integer, the size of the largest contact group. 

Sample Input

3 2
John 0 1
Rose 1
Mary 1
5 4
ACM 1 2 3
ICPC 0 1
Asian 0 2 3
Regional 1 2
ShangHai 0 2
0 0

Sample Output

2
2

二、思路

  • 首先答案显然是符合单调性的。 那么可以用二分来枚举每个组的人数。
  • 多重匹配: 右边的组可以匹配多个左边的人。当每组的人数不够mid的话直接匹配就行。 若已经达到mid,那么看是是否还能寻找到增广路,若能找到就进行替换。

三、代码

#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1005, M = 2e6 + 5;
struct E {
	int v, next;
} e[M];
struct Node {
	int len, my[N];
} mat[N];
int n, m, u, v, len, h[N];
char s[N], c;
bool vis[N]; 
void add(int u, int v) {
	e[++len].v = v; e[len].next = h[u]; h[u] = len;
}
bool dfs(int u, int mid) {
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (vis[v]) continue; vis[v] = true;
		if (mat[v].len < mid) { //如果还有长度的话 直接匹配 
			mat[v].my[++mat[v].len] = u;
			return true;
		}
		//否则替换其中某一个匹配
		for (int i = 1; i <= mat[v].len; i++) {
			if (dfs(mat[v].my[i], mid)) {
				mat[v].my[i] = u; //替换i那个匹配 
				return true;
			}
		} 
	}
	return false;
}  
bool ok(int mid) {
	for (int i = 0; i < m; i++) mat[i].len = 0;
	//memset(mat, 0, sizeof(mat));
	for (int i = 1; i <= n; i++) {
		memset(vis, false, sizeof(vis));
		if (!dfs(i, mid)) return false;
	}
	return true;  
}
int main() {
	while (scanf("%d%d", &n, &m), n) {
		memset(h, 0, sizeof(h)); len = 0;
 		for (int u = 1; u <= n; u++) {
			scanf("%s", &s);
			while (scanf("%c", &c), c != '\n') {
				scanf("%d", &v);                                                              
				add(u, v);
			}
		}	
		//二分答案 求多重匹配
		int l = 0, r = n; 
		while (l < r) {
			int mid = (l + r) >> 1;
			if (ok(mid)) {
				r = mid;
			} else {
				l = mid + 1;
			}
		} 	
		printf("%d\n", l);
	}
	return 0;
} 
发布了471 篇原创文章 · 获赞 481 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104541322
今日推荐