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最小是 ...
随机推荐
- SpringMVC的视图解析器
ViewResolver和View介绍 SpringMVC用于处理视图最重要的两个接口是ViewResolver和View.ViewResolver的主要作用是把一个逻辑上的视图名称解析为一个真正的视 ...
- call ,apply 和 bind的用法与区别
作用都是一样的,官方解释:"调用一个对象的一个方法,以另一个对象替换当前对象", 简单来说就是改变当前使用该方法的对象中的this指向: var xw = { name : &qu ...
- Mongodb 抛出异常:dbexit: really exiting now
删除 数据库文件夹下,的 _tmp 和 mongodb.lock 文件 , 重启即可.我的数据文件在 /data/mongo/data/ 下
- linux安装文件命令
tar -zxvf apache-tomcat.tar.gz -C /home/poka 注:安装tar.gz的安装包 设置系统自动启动tomcat 切换到root用户,执行命令 #chkconfig ...
- android开发技巧
1 Android去掉listView,gridView等系统自带阴影 当我们使用listView的时候,拉到顶,或是拉到底部的时候,我们会发现有系统自带的阴影效果出现,不同手机出现的颜色可能还会不一 ...
- C语言_愤怒的小鸟
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> ...
- Sublime Text: [Decode error - output not utf-8]
今天编译Python时, 输出窗口信息出现: [Decode error - output not utf-8][Decode error - output not utf-8] 发现是print ...
- 批处理 取得当前路径 %CD%
在DOS的批处理中,有时候需要知道当前的路径.在DOS中,有两个环境变量可以跟当前路径有关,一个是%cd%, 一个是%~dp0. 这两个变量的用法和代表的内容一般是不同的. 1. %cd% 可以用在批 ...
- Chapter 2 Open Book——15
The rest of the week was uneventful. I got used to the routine of my classes. 这周剩下的时间都是平淡无事的.我就是正常的上 ...
- js如何在指定页面跳转到另一指定页面
要实现从一个页面A跳到另一个页面B,js实现就在A的js代码加跳转代码 JS跳转大概有以下几种方式: 第一种:(跳转到b.html)<script language="javascri ...