admin管理员组

文章数量:1530018

Jamie’s Contact Groups(二分图匹配-多重匹配+二分)

source:Shanghai 2004
judge:vjudge
Time limit:7000 ms
Memory limit:65536 kB
OS:Linux

描述

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

题意

给你n个联系人以及每个联系人可被分到的组,让你把这些联系人分组并且把最大组的人数尽可能低。

利用二分逼近极限值,然后用匈牙利算法检验枚举值

第一次写多重匹配

代码

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cstdio>
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
#define maxn 1005
#define inf 0x3f3f3f3f
using namespace std;
struct poi {
	int cnt, g[maxn];
} link[maxn];
int used[maxn];
int n, m;
int p[maxn][maxn];
bool dfs(int u, int lim) {
	_rep(i, 1, m) {
		if (!used[i] && p[u][i]) {
			used[i] = 1;
			if (link[i].cnt < lim) {
				link[i].g[link[i].cnt++] = u;
				return 1;
			}
			_for(j, link[i].cnt) {
				if (dfs(link[i].g[j], lim)) {
					link[i].g[j] = u;
					return 1;
				}
			}
		}
	}
	return 0;
}
bool hungary(int lim) {
	memset(link, 0, sizeof(link));
	_rep(i, 1, n) {
		memset(used, 0, sizeof(used));
		if (!dfs(i, lim)) return 0;
	}
	return 1;
}
int main() {
	while (cin >> n >> m, getchar(), n || m) {
		memset(p, 0, sizeof(p));
		_rep(i, 1, n) {
			char name[50];
			cin >> name; getchar();
			while (1) {
				int gol;
				char ch;
				scanf("%d%c", &gol, &ch);
				p[i][gol + 1] = 1;
				if (ch == '\n') break;
			}
		}
		int l = 1, r = n, ans = n;
		while (l <= r) {
			int mid = (l + r) / 2;
			if (hungary(mid)) {
				r = mid - 1;
				ans = mid;
			}
			else l = mid + 1;
		}
		cout << ans << "\n";
	}
	return 0;
}

本文标签: JamieContactgroups