二分答案+网络最大流

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; int N,M;
const int maxn = + ;
const int INF = 0x7FFFFFFF;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int n, m, s, t;
int U[maxn*],V[maxn*];
int tot;
int T[maxn];
char ss[maxn]; void init()
{
for(int i = ; i < maxn; i++) G[i].clear();
edges.clear();
tot=;
memset(T,,sizeof T);
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
} bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
} int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} void Input()
{
int SS;
for(int i=;i<=N;i++)
{
gets(ss);
for(int ii=;ss[ii];ii++)
if(ss[ii]==' ')
{SS=ii;break;}
int num=;
for(int ii=SS+;;ii++)
{
if(ss[ii]>=''&&ss[ii]<='') num=num*+ss[ii]-'';
if(ss[ii]==' '||ss[ii]=='\0')
{
U[tot]=i;V[tot]=N+num+;tot++;
T[num]++;
num=;
}
if(ss[ii]=='\0') break;
}
}
} void Solve()
{
int Ma=-INF;
for(int i=;i<M;i++) if(T[i]>Ma) Ma=T[i];
int Min=,Max=Ma;
int Mid=(Min+Max)/;
s=;t=N+M+;
while()
{
for(int i = ; i < maxn; i++) G[i].clear();
edges.clear();
for(int i=;i<=N;i++) AddEdge(s,i,);
for(int i=;i<tot;i++) AddEdge(U[i],V[i],INF);
for(int i=N+;i<=N+M;i++) AddEdge(i,t,Mid);
if(dinic(s,t)<N)
{
Min=Mid+;
Mid=(Min+Max)/;
}
else
{
Max=Mid;
Mid=(Min+Max)/;
}
if(Min==Max) break;
}
printf("%d\n",Min);
} int main()
{
while(~scanf("%d%d",&N,&M))
{
if(!N&&!M) break;
scanf("\n");
init();
Input();
Solve();
}
return ;
}

POJ 2289 Jamie's Contact Groups的更多相关文章

  1. 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 ...

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

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

  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——————【多重匹配、二分枚举匹配次数】

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

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

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

  6. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

  7. POJ 2289 Jamie's Contact Groups (二分+最大流)

    题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼. ...

  8. 图论--网络流--最大流 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 ...

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

    题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...

随机推荐

  1. android Tweened Animations

    Android提供了两种类型的动画: 一类是Tween动画:提供了旋转.移动.伸展和淡出等效果: 第二类是Frame-by-frame动画:这一类Animations可以创建一个Drawable序列, ...

  2. 【分享】bootstrap学习笔记

    一.基础知识 1.整体架构 以响应式设计为理念,css组件.js插件+jquery.基础布局组件和12栅格系统搭建. 1.1响应式设计:结合media query查询,适应更多设备,自动适应用户的设备 ...

  3. laravel 邮箱改密功能

    参考网址: http://laravelacademy.org/post/1290.html

  4. JSON理解

    var txt = '{"employees":[' + //多段文字用'+'来组织在一起 '{"firstName":"Bill",&qu ...

  5. win7 xp 双系统安装记录

    原机win7 64 增加xp x86 win7在c盘,xp装h盘 1.老毛桃pe,雨林木风gho,蓝屏,0000007b 2.通用pe.雨林木风gho,蓝屏,00000007b 3.pe设置h盘为系统 ...

  6. HDU--1301--Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  7. HDU 5805 NanoApe Loves Sequence

    处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,10240000 ...

  8. mysql 中 SQL_CALC_FOUND_ROWS 功能

    mysql 数据库不符合sql标准的地方不少,比如TIMESTAMP列的处理,字符串比较默认大小写不敏感什么的.有时候这些问题会让你很郁闷,尤其是对从其它数据库转过来的人来说.但有些功能倒也蛮有趣. ...

  9. c语言-键盘扫描码

    定义: 键盘上的每一个键都有两个唯一的数值进行标志.为什么要用两个数值而不是一个数值呢?这是因为一个键可以被按下,也可以被释放.当一个键按下时,它们产生一个唯一的数值,当一个键被释放时,它也会产生一个 ...

  10. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...