poj2289Jamie's Contact Groups(好题)
来源:互联网 发布:环境破坏的资料数据 编辑:程序博客网 时间:2024/06/05 23:53
Jamie's Contact Groups
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 7026 Accepted: 2323
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 2John 0 1Rose 1Mary 15 4ACM 1 2 3ICPC 0 1Asian 0 2 3Regional 1 2ShangHai 0 20 0
Sample Output
22
Source
Shanghai 2004
本题显而易见,是二分答案加上二分图多重匹配,
需要注意的是读入
本题显而易见,是二分答案加上二分图多重匹配,
需要注意的是读入
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;const int maxn=1010;int linker[maxn][maxn];int g[maxn][maxn];int num[maxn];int used[maxn];int uN,vN;bool dfs(int u){ for(int v=1;v<=vN;v++){ if(g[u][v]&&!used[v]){ used[v]=1; if(linker[v][0]<num[v]){ linker[v][++linker[v][0]]=u; return true; } for(int i=1;i<=num[v];i++) if(dfs(linker[v][i])){ linker[v][i]=u; return true; } } } return false;}int hungary(){ int res=0; mem0(linker); for(int u=1;u<=uN;u++){ mem0(used); if(dfs(u)) res++; } return res;}int main(){ int n,m; int u; char s[100]; while(scanf("%d%d",&n,&m)!=EOF){ if(n==0&&m==0) break; mem0(g); for(int i=1;i<=n;i++){ scanf("%s",s); while(true){ //注意数字可能是连续的多个字符组成的 scanf("%d",&u); g[i][u+1]=1; if(getchar()=='\n') break; } } uN=n; vN=m; int high=1010,low=0; while(high-low>=0){ int mid=(low+high)>>1; for(int i=1;i<=m;i++) num[i]=mid; int ans=hungary(); if(ans>=n) high=mid-1; else low=mid+1; } printf("%d\n",low); } return 0;}
0 0
- poj2289Jamie's Contact Groups(好题)
- POJ2289Jamie's Contact Groups(二分图多重匹配+二分)
- Jamie's Contact Groups
- POJ2289--Jamie's Contact Groups
- Jamie's Contact Groups UVALive
- poj2289 Jamie's Contact Groups
- POJ 2289 Jamie's Contact Groups
- POJ-2289-Jamie's Contact Groups
- POJ 2289 Jamie's Contact Groups
- Jamie's Contact Groups POJ - 2289
- 2289 Jamie's Contact Groups //二分+二分图多重匹配
- POJ2289 Jamie's Contact Groups 二分+最大流匹配
- POJ 2289 Jamie's Contact Groups 多重匹配+二分
- POJ 2289 Jamie's Contact Groups (二分+dinic)
- poj 2289 Jamie's Contact Groups 二分图多重匹配
- poj 2289 Jamie's Contact Groups(二分+多重匹配)
- POJ 2289 - Jamie's Contact Groups 二分+最大流
- POJ 2289 Jamie's Contact Groups 二分+最大匹配
- hdu1162
- Codeforces Beta Round #3——B. Lorry
- Objective-C 基础知识之 (十五):KVC
- 实在小店在B2C平台具有一定的知名度
- R操作数据库里面的数据
- poj2289Jamie's Contact Groups(好题)
- ios的系统框架
- lua的table.sort 中出现 invalid order function for sorting
- 异常和异常处理
- SBT下载特别慢的问题解决
- 我自己的软弱和情绪
- 多线程详解
- android HAL接口的编码小结
- 集合框架List部分