POJ 2289 Jamie's Contact Groups
二分答案+网络最大流
#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的更多相关文章
- 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 ...
- Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)
题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
- poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】
题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K ...
- POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】
Jamie's Contact Groups Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 6511 Accepted: ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
- POJ 2289 Jamie's Contact Groups (二分+最大流)
题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼. ...
- 图论--网络流--最大流 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 ...
- POJ 2289 Jamie's Contact Groups(多重匹配+二分)
题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...
随机推荐
- ural 1353. Milliard Vasya's Function(dp)
1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...
- 获取当前设备的IP地址
头文件: #import <ifaddrs.h> #import <arpa/inet.h> #import <net/if.h> 宏定义: #define IOS ...
- iOS学习笔记(02) - 关键字 __kindof
1.__kindof:表示当前类或它的子类. 2.__kindof书写格式:放在类型前面,表示修饰这个类型. 3.__kindof优点:在调用的时候,很清楚的知道返回类型. 直接举一个例子来形容这个问 ...
- 数组按时间(字符串->Date)排序
不说了 ,直接上代码.Talk is cheap , show me the code. NSArray *stortedArray = [wkSelf.dataArray sortedArrayUs ...
- VBS常用使用技巧
(一)VBS常用函数使用笔记: 1.Msgbox语法:msgbox "对话框内容", , "对话框的标题" 2.inputbox是VBS内建的函数,可以接受输入 ...
- Chapter 2 Open Book——19
He bent over and began scraping together a pile of the white mush. 他弯下腰,开始团起一个白色的雪球. "I'll see ...
- CSS样式与选择器
CSS构造块的样式: 1. h1{color:red;background-color:yellow} 其中:h1是选择器,花括号内是声明部分.多个声明之间用分号隔开. 2.为样式规则添加注释:/* ...
- flash跨域策略文件crossdomain.xml配置详解
来源:http://www.2cto.com/Article/201108/100008.html 0x01 简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了 ...
- .net文件上传,客户端用jquery file upload
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...
- karma+jasmine自动化测试
1.安装nodejs,进入项目目录 2.安装karma和相关插件 npm install karma --save-dev npm install karma-jasmine karma-chrome ...