题目大意:你的手机通讯录里有n个联系人,m个分组,其中,有的联系人在多个分组里。你的任务是在一些分组里删除一些联系人,使得每个联系人只在一个分组里并且使人数最多的那个分组人数最少。找出人数最多的那个分组中的人数。

题目分析:要求的是最小的最大值,二分枚举这个最小的最大人数x。增加源点s和汇点t,从s向每一个联系人连一条弧,容量为1,表示一个联系人只能在一个分组中;然后对于每个联系人向他所在的分组连一条弧,容量为1,表示在这个分组里最多保存一次该联系人;然后从每个分组向汇点连一条弧,容量为x,表示这个分组不能保存超过x个联系人。求最大流,如果源点s出发的每条边都满载,说明x可行。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<string>
# include<vector>
# include<list>
# include<set>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; # define LL long long
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b) const double inf=1e30;
const int INF=1<<30;
const int N=1505; struct Edge
{
int fr,to,cap,fw;
Edge(int _fr,int _to,int _cap,int _fw):fr(_fr),to(_to),cap(_cap),fw(_fw){}
};
vector<Edge>edges,tedges;
vector<int>G[N];
int cur[N],vis[N],d[N],mark[505]; void init(int n)
{
edges.clear();
REP(i,0,n) G[i].clear();
} void addEdge(int u,int v,int cap)
{
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
} bool BFS(int s,int t)
{
CL(vis,0);
vis[s]=1;
d[s]=0;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
REP(i,0,G[u].size()){
Edge &e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>e.fw){
d[e.to]=d[u]+1;
vis[e.to]=1;
q.push(e.to);
}
}
}
return vis[t];
} int DFS(int u,int t,int a)
{
if(u==t||a==0) return a;
int flow=0,f;
for(int &i=cur[u];i<G[u].size();++i){
Edge &e=edges[G[u][i]];
if(d[e.to]==d[u]+1&&(f=DFS(e.to,t,min(a,e.cap-e.fw)))>0){
e.fw+=f;
edges[G[u][i]^1].fw-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
} int Dinic(int s,int t)
{
int flow=0;
while(BFS(s,t)){
CL(cur,0);
flow+=DFS(s,t,INF);
}
return flow;
} bool read(int &x)
{
x=0;
char c;
while(c=getchar()){
if(c==' ') return true;
else if(c=='\n') return false;
else x=x*10+c-'0';
}
} bool judge()
{
REP(i,0,G[0].size())
if(edges[G[0][i]].cap>0&&edges[G[0][i]].cap!=edges[G[0][i]].fw) return false;
return true;
} int main()
{
int n,m;
char name[20];
while(scanf("%d%d",&n,&m)&&(n+m))
{
init(n+m+2);
CL(mark,0);
REP(i,1,n+1){
cin>>name;
addEdge(0,m+i,1);
int a;
getchar();
while(read(a)){
++mark[a];
addEdge(m+i,a+1,1);
}
addEdge(m+i,a+1,1);
++mark[a];
} int cnt=edges.size();
tedges.clear();
REP(i,0,cnt) tedges.push_back(edges[i]); int l=0,r=0;
REP(i,0,m) r=max(r,mark[i]);
while(l<r){
int mid=l+(r-l)/2;
init(n+m+2);
REP(i,0,cnt) addEdge(tedges[i].fr,tedges[i].to,tedges[i].cap);
REP(i,0,m) if(mark[i])
addEdge(i+1,n+m+1,min(mid,mark[i]));
Dinic(0,n+m+1);
if(judge()) r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}

  

UVALive-3268 Jamie's Contact Groups (最大流,网络流建模)的更多相关文章

  1. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

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

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 8567   Accepted: ...

  3. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

  4. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  5. POJ2289 Jamie's Contact Groups(二分图多重匹配)

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 7721   Accepted: ...

  6. POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: ...

  7. POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)

    Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/ ...

  8. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  9. Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)

    题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...

  10. 图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)

    Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very ...

随机推荐

  1. 解决Ubuntu14.04下vi编辑器不能使用方向键和退格键问题

    参考:http://blog.sina.com.cn/s/blog_7d0c2fed01010zbi.html 系统:Ubuntu14.04 使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多 ...

  2. Oracle HA 之 基于活动数据库复制配置oracle 11.2 dataguard

    规划:主库:db_name=dbking               db_unique_name=dbkingpri               备库:db_name=dbking          ...

  3. SUBSTRING_INDEX()

    http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index ) DEFAULT 'fru ...

  4. 20144306《网络对抗》MAL_后门原理与实践

    本期收获 1.了解后门的基本概念. 2.Netcat.socat.MSF meterpreter的使用(MSF meterpreter实在太好玩了) 3.后门软件的启动方式: Windows任务计划程 ...

  5. the source attachment does not contain the source for the file xxx.class无法关联到某个类

    问题描述: 按下列操作添加相应路径(这里是错误操作) 该问题仍旧无法解决: 注意:这里spring-webmvc-4.1.7.RELEASE.JAR中确实包含AnntationMethodHandle ...

  6. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

  7. Linux系统——Ansible批量管理工具

    批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...

  8. vgg_face人脸识别

    最近参考http://blog.csdn.net/hlx371240/article/details/51388022一文,用LFW数据集对vgg_face.caffemodel进行fine-tune ...

  9. VS2010/MFC编程入门之二十八(常用控件:列表视图控件List Control 上)

    前面一节中,鸡啄米讲了图片控件Picture Control,本节为大家详解列表视图控件List Control的使用.      列表视图控件简介 列表视图控件List Control同样比较常见, ...

  10. 工具推荐. 在线unix, 在线python/perl脚本测试环境

    在线python, perl, javascript, Lisp, Ruby等  http://melpon.org/wandbox/ 正则表达式在线测试工具 http://tools.jb51.ne ...