POJ - 2289 Jamie's Contact Groups

Jamie's Contact Groups

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 8667   Accepted: 2941

Description

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

Source

Shanghai 2004

扫描二维码关注公众号,回复: 2647880 查看本文章

       这是一道二分图多重匹配的模板题+二分,总体来说多重二分匹配就是有着多于1但是有着某一上限的可连接点的二分图匹配。所以只要在匹配时候判断是否超过数量限制或者能够有增广路就好了,注意的是我用的vector储存右方(分类)被选中的左方端点(人),所以找到增广路的时候注意是要替换而不是push_down;

        另外注意一下二分;

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n, m; char c; string name;
vector<int>use[505];
bool vis[505];
vector<int>G[1005];
int find(int x, int limit)
{
	int sz = G[x].size();
	for (int s = 0; s < sz; s++) {
		int v = G[x][s];
		if (!vis[v]) {
			vis[v] = 1; int vz = use[v].size();
			if (vz < limit) {
				use[v].push_back(x); return 1;
			}
			for (int w = 0; w < vz; w++) {
				if (find(use[v][w], limit)) {
					use[v][w] = x; return 1;
				}
			}
		}
	}
	return 0;
}
int check(int mid) {
	for (int s = 0; s<m; s++)
		use[s].clear();
	for (int s = 0; s < n; s++) {
		memset(vis, 0, sizeof(vis));
		if (!find(s, mid)) {
			return 0;
		}
	}
	return 1;
}
int main()
{
	ios::sync_with_stdio(0);
	while (~scanf("%d%d", &n, &m) && n + m) {
		for (int s = 0; s <= n; s++) {
			G[s].clear();
		}
		for (int s = 0; s < n; s++) {
			cin >> name;
			int t;
			while (~scanf("%d", &t)) {
				G[s].push_back(t);
				scanf("%c", &c);
				if (c == '\n') break;
			}
		}
		int li = 0, ri = n;
		while (li < ri) {
			int mid = li + ri >> 1;
			if (check(mid)) {
				ri = mid;
			}
			else {
				li = mid + 1;
			}
		}
		cout << ri << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/81449804
今日推荐