Jamie's Contact Groups POJ - 2289(多重匹配 最大值最小化 最大流)

时间:2024-01-07 11:26:38
Jamie's Contact Groups
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 8567   Accepted: 2900

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


二分最大值mid,源点连向n个联系*值为1,联系人连向它可以属于的分类,m个分类与汇点建边权值为mid(该分类最多mid个人)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int d[maxn], head[maxn], cur[maxn];
int n, m, s, t, ans;
int cnt = ;
vector<int> G[maxn];
struct node
{
int u, v, c, next;
} Node[maxn*]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = , V;
if(u == t || cap == )
return cap;
for(int &i=cur[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[e.u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
return ret;
} int dinic(int u)
{
ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
char str[maxn], ch;
while(~scanf("%d%d",&n,&m) && n+m)
{
for(int i=; i<=n; i++) G[i].clear();
int ret = ;
s = n+m+; t = n+m+;
int v;
for(int i=; i<=n; i++)
{
cin>> str;
while(scanf("%d%c", &v, &ch))
{
G[i].push_back(v);
if(ch == '\n')
break;
}
}
int l = , r = n, mid;
while(l <= r)
{
mem(head, -);
cnt = ;
mid = (l + r) / ;
for(int i=; i<=n; i++)
for(int j=; j<G[i].size(); j++)
add(m+i, G[i][j], );
for(int i=; i<=n; i++)
add(s, m+i, );
for(int i=; i<m; i++)
add(i, t, mid);
if(dinic(s) == n)
{
ret = mid;
r = mid - ;
}
else
l = mid + ;
}
cout<< ret <<endl; } }