POJ2289-Jamie's Contact Groups-二分图多重匹配-ISAP
注意POJ数组越界可能返回TLE!!!
网络流的maxn大小要注意
其他没什么了 裸二分答案+isap乱搞
不过复杂度没搞懂 V=1e3 E = 1e5 那ISAP的O(V^2E)怎么算都不行啊
/*--------------------------------------------------------------------------------------*/ #include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
/*--------------------------------------------------------------------------------------*/
using namespace std; int N,M,T;
const int maxn = ;
const int maxm = ;
//map<int ,string > mp;
vector <int> save[maxn]; struct Edge{
int to,next,cap,flow;
}edge[maxm]; int head[maxn],tot;
int uN = ;
int gap[maxn],dep[maxn],pre[maxn],cur[maxn]; void init()
{
tot = ;
memset(head,-,sizeof head);
} void add_edge(int u,int v,int w,int rw = )
{
//printf("%d->%d %d\n",u,v,w);
edge[tot].to = v;edge[tot].cap = w;edge[tot].next = head[u];
edge[tot].flow = ;head[u] = tot++;
edge[tot].to = u;edge[tot].cap = rw;edge[tot].next = head[v];
edge[tot].flow = ;head[v] = tot++;
}
int Q[maxn];
void BFS(int st,int ed)
{
memset(dep,-,sizeof dep);
memset(gap,,sizeof gap);
gap[] = ;
int front = ,rear = ;
dep[ed] = ;
Q[rear++] = ed;
while(front != rear)
{
int u = Q[front++];
for(int i=head[u];~i;i=edge[i].next)
{
int v = edge[i].to;
if(dep[v] != -) continue;
Q[rear++] = v;
dep[v] = dep[u] + ;
gap[dep[v]] ++;
}
}
}
int S[maxn]; int sap(int st,int ed)
{
BFS(st,ed);
memcpy(cur,head,sizeof head);
int top = ;
int u = st; int ans = ;
while(dep[st] < uN)
{
if(u == ed)
{
int Min = INF;
int inser;
for(int i=;i<top;i++)
{
if(Min > edge[S[i]].cap - edge[S[i] ].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser=i;
}
}
for(int i=;i<top;i++)
{
edge[S[i]].flow += Min;
edge[S[i]^].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top]^].to;
continue;
}
bool flag = false;
int v;
for(int i=cur[u];~i;i=edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = uN;
for(int i=head[u];~i;i=edge[i].next)
{
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]] --;
if(!gap[dep[u]]) return ans;
dep[u] = Min + ;
gap[dep[u] ]++;
if(u != st) u = edge[S[--top]^].to;
}
return ans;
} int check(int mid)
{
uN = N+M+;
init();
for(int i=;i<N;i++)
{
add_edge(N+M,i,);
for(int j=;j<save[i].size();j++)
{
add_edge(i,save[i][j]+N,);
}
}
for(int i=;i<M;i++)
{
add_edge(N+i,N+M+,mid);
} int res = sap(N+M,N+M+);
//printf("mid:%d res:%d\n",mid,res);
if(res == N) return true;
else return false;
} int solve()
{
int L = ,R = *N,mid;
while(L <= R)
{
mid = (L+R)>>;
if(check(mid)) R = mid-;
else L = mid+;
}
//printf("%d %d\n",L,R);
return L;
} int main()
{
while(scanf("%d%d",&N,&M) && N+M)
{
char s[];
//mp.clear();
for(int i=;i<maxn;i++) save[i].clear();
for(int i=;i<N;i++)
{
scanf("%s",s);
char c = getchar();
if(c != ' ') continue;
int num = -;
while((c = getchar()) && c !='\n')
{
if(isdigit(c))
{
if(num == -) num = ;
num *= ;num += c-'';
}
else if(c == ' ')
{
save[i].push_back(num);
num = -;
}
}
if(num != -) save[i].push_back(num);
}
if(M == || N == ) printf("0\n");
else printf("%d\n",solve());
}
}
POJ2289-Jamie's Contact Groups-二分图多重匹配-ISAP的更多相关文章
- POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 6 ...
- 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——————【多重匹配、二分枚举匹配次数】
Jamie's Contact Groups Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ 2289 Jamie's Contact Groups(多重匹配+二分)
题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 7721 Accepted: ...
- Jamie's Contact Groups---hdu1669--poj2289(多重匹配+二分)
题目链接 题意:Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个 ...
- poj2289 Jamie's Contact Groups
思路: 二分+最大流.实现: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include ...
- POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)
Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/ ...
随机推荐
- 大家一起和snailren学java-(五)访问控制权限
“感觉中间断了一天,可是数数好像又没断……(-_^)” 这一天我们来再次细致讨论一下java的访控机制.java的访控机制其实在编程架构上非常实用的,也就是所谓的隐藏具体实现或者封装. 首先看看使用场 ...
- python sorted用法
python列表排序 python字典排序 sorted List的元素可以是各种东西,字符串,字典,自己定义的类等. sorted函数用法如下: sorted(data, cmp=None, key ...
- linux动态网络和静态网络和克隆后的网络配置
建议设置网卡NAT模式 动态网络配置:1.一定要开启本地DHCP服务 2.在虚拟网络编辑器中选择NAT模式选中DHCP项如下图 3.ifup eth0 静态网络配置 : 注释:ifcfg-eth0部分 ...
- 05_最长公共子序列问题(LCS)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题7: 问题描述:给两个子序列A和B,求长度最大的公共子序列.比如1,5,2,6,8,和2,3,5,6,9,8,4的最长公共子序 ...
- nginx 平滑升级tengine
wget http://tengine.taobao.org/download/tengine-1.5.1.tar.gz //下载Tengine1.5.1版本 tar zxvf tengine-1. ...
- Hbase step by step 完全分布式安装
Step1: download and extract the packages: http://mirror.bit.edu.cn/apache/hbase/stable/ Step2: set t ...
- ZooKeeper架构设计及其应用要点
问题导读: 1.ZooKeeper的数据模型是什么 ?2.ZooKeeper应用有哪些陷阱 ?3.每个节点(ZNode)中存储的是什么?4.一个ZNode维护了一个状态结构都包含了什么?5.ZNode ...
- 《TCP/IP详解 卷一》读书笔记-----DNS
1.DNS是一个分布式数据库系统用来提供主机名和IP地址之间的映射,之所以称为分布式原因的原因是因特网上没有一台主机知道这类映射的全部信息,当然也不可能做到,因为数据量实在太大了 2.应用程序通过一个 ...
- 怎样用ZBrush中复数对象进行工作
在ZBrush®中有两种方法可以使用复数对象即“多边形组”和“次工具”. 若有疑问可直接访问:http://www.zbrushcn.com/jichu/fushu-duixiang.html 什么是 ...
- 微博API使用
新浪微博的API开放平台: http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI IOS和Android都有SDK可以下载,ios的地址: https:// ...