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/ ...
随机推荐
- [VMware]设置VM虚拟机随系统自动启动
设置步骤: 1.找到VM的安装路径,右键vmware发送到桌面快捷方式 2.右键桌面快捷方式的属性,看到目标的属性框 3.找到需要自启动的虚拟机路径,如: D:\QC_VM\Clone of Wind ...
- wait方法和sleep方法的区别
一.概念.原理.区别 Java中的多线程是一种抢占式的机制而不是分时机制.线程主要有以下几种状态:可运行,运行,阻塞,死亡.抢占式机制指的是有多个线程处于可运行状态,但是只有一个线程在运行. ...
- jQuery form插件的使用--用 formData 参数校验表单,验证后提交(简单验证).
Form Plugin API 里提供了很多有用的方法可以让你轻松的处理表单里的数据和表单的提交过程. 测试环境:部署到Tomcat中的web项目. 一.引入依赖js <script src=& ...
- Redis系列(三)—— 订阅/发布
Redis 订阅/发布 参考:http://www.cnblogs.com/mushroom/p/4470006.html,http://www.tuicool.com/articles/ABry2a ...
- nyoj 139 我排第几个--康拓展开
我排第几个 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在有"abcdefghijkl”12个字符,将其所有的排列中按字典序排列,给出任意一种排列,说 ...
- 3D俄罗斯方块设计
发布在博客:http://blog.sina.com.cn/s/blog_d38e811c0101cpis.html
- NOI2016 山西省省选 第二题序列
给出一个n(n<=10^18)然后把n拆成若干个数之和(3=1+2=2+1 是两种情况) 然后把这写数字当作斐波那契数列的下标相乘再相加 例如: 3=1+1+1=1+2=2+1=3 所以结果就是 ...
- CSS3属性选择器与(:not)选择器
一:css3属性选择器: img[alt]{ border:2px dashed #000; } 这个选择器会匹配页面标签中任何一个含有alt属性的图片标签. 还可以通过设定属性值来缩小匹配范围: ...
- 在同一个机器中安装LoadRunner与QTP
若你计划在测试机上安装LoadRunner并且测试机上已经安装了QTP,类似这样的情况可能会出现一些冲突现象,若QTP与LR必须并存在同一测试机上,那么请确保先安装Loadrunner以及所有的LR补 ...
- MySQL的重装问题解决方法
最近在工作上遇到了在Windows环境中将MySQL重装的问题,今天记录一下我的解决过程. 首先因为某些原因,我不得不把mysql卸载,然后重装,不论我用控制面板的卸载删除程序方式还是安全卫士的卸载, ...