uva 1345 Jamie's Contact Groups (最大流+二分)

uva 1345 Jamie’s Contact Groups

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

题目大意:给出一些人,和他们可能加入的组号。问每个组,最小的最大人数是多少。
解题思路:看到最小的最大值就想到二分,问题是二分什么。这题二分的是最大流的容量。设置一个超级源点,连向所有的人,容量为1。设置一个超级汇点,使所有的组连向超级汇点,二分的就是这里的容量(0~n)。然后根据题目给出的人和组的关系连接人和组,容量为1。二分时,若当前状态求出的最大流等于人数,则下界等于mid,若不等于,则上界等于mid。二分出的容量,就是组中的最小的最大人数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <sstream>
#include <iostream>
using namespace std;

const int N = 1010 + 510;
const int M = 1010 * 510 * 4;
const int PO = 1005;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, m, s, t, rec[PO][PO], cnt[PO];
int ec, head[N], first[N], que[N], lev[N];  
int Next[M], to[M], v[M];  

void init() {  
    ec = 0;  
    memset(first, -1, sizeof(first));  
}  

void addEdge(int a,int b,int c) {  
    to[ec] = b;  
    v[ec] = c;  
    Next[ec] = first[a];  
    first[a] = ec++;  

    to[ec] = a;  
    v[ec] = 0;  
    Next[ec] = first[b];  
    first[b] = ec++;  
}  

int BFS() {  
    int kid, now, f = 0, r = 1, i;  
    memset(lev, 0, sizeof(lev));  
    que[0] = s, lev[s] = 1;  
    while (f < r) {  
        now = que[f++];  
        for (i = first[now]; i != -1; i = Next[i]) {  
            kid = to[i];      
            if (!lev[kid] && v[i]) {  
                lev[kid] = lev[now] + 1;      
                if (kid == t) return 1;  
                que[r++] = kid;  
            }  
        }  
    }  
    return 0;  
}  

int DFS(int now, int sum) {  
    int kid, flow, rt = 0;  
    if (now == t) return sum;  
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {  
        head[now] = i;    
        kid = to[i];  
        if (lev[kid] == lev[now] + 1 && v[i]) {  
            flow = DFS(kid, min(sum - rt, v[i]));  
            if (flow) {  
                v[i] -= flow;  
                v[i^1] += flow;  
                rt += flow;  
            } else lev[kid] = -1;     
        }             
    }  
    return rt;  
}  

int dinic() {  
    int ans = 0;  
    while (BFS()) {  
        for (int i = 0; i <= t; i++) {  
            head[i] = first[i];  
        }             
        ans += DFS(s, INF);  
        if (ans >= n) return ans;
    }  
    return ans;  
}     

void input() {
    init();
    s = 0, t = (n + m) + 2;
    memset(cnt, 0 ,sizeof(cnt));
    char ch[100];
    int num;
    string ss;
    for (int i = 0; i < n; i++) {
        getline(cin, ss);
        stringstream stream(ss);
        stream >> ch;
        while (stream >> num) {
            addEdge(i + 1, num + 1 + n, 1); 
        }
    }
    for (int i = 1; i <= n; i++) {
        addEdge(s, i, 1);
    }
    for (int i = 1; i <= m; i++) {
        addEdge(i + n, t, 1);   
    }
}

int build(int x) {
    for (int i = 0; i < ec; i += 2) {
        if (to[i] == t) v[i] = x;
        else v[i] = 1;
        v[i^1] = 0; 
    }
    int temp = dinic();
    if (temp == n) return 1;
    else return 0;
}

int main() {
    while (scanf("%d %d\n", &n, &m) == 2, (n || m)) {
        input();
        int L = 0, R = n + 2;
        while (L < R) {
            int mid = (L + R) / 2;      
            if (build(mid)) R = mid;
            else L = mid + 1;
        }
        printf("%d\n", L);
    }   
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值