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/ ...
随机推荐
- Memcache笔记03-php操作Memcached
通过php程序操作Memcached服务几种形式 Memcache 扩展 Memcached 扩展 Socket套接字操作 memcached-client.php(函数) 对于php扩展来说,dan ...
- Effective Java 71 Use lazy initialization judiciously
Lazy initialization - It decreases the cost of initializing a class or creating an instance, at the ...
- 一次由于字符集问题引发的MySQL主从同步不一致问题追查
近期业务准备上线一个新功能,灌入数据之后突然发现主从同步停止,报错如下: Error 'Duplicate entry '66310984-2014-04-18 00:00:00--122815.sh ...
- Java基础の乱弹琴二:break关键字
Java中的break一般用于 跳出一个switch或者循环. 跳出switch基本不用赘述. break跳出循环一般是跳出当前一层循环. 如若需要跳出多层循环可以在break后加标签,然后把标签标注 ...
- Centos 部署Keepalive高可用软件
Keepalive安装部署 一.环境介绍 1)Centos6.4 2) keepalived-1.2.12 3) 主备机的ip Master:172.31.100.5 Slave: 172.31. ...
- Ubuntu进阶学习,指令迅速查询,Bug迅速查询(Ctrl+F)
There is some notes while I am learning Ubuntu Operate System! (Ask Ubuntu) 1-- Hard link : ln comma ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
- 去哪儿网2017校招在线笔试(前端工程师)编程题及JavaScript代码
编程题很简单.整个试卷结构为: 一.问答题: 对前端的理解,了解哪些框架库? 二.在线编程题:身份证分组 如下第一道:身份证分组 三.在线编程题:身份证分组.统计字符.酒店价格(三选二) 如下第二三四 ...
- js开发工具箱
昨天看到一位大牛的博客,里面有一篇文章“web前端开发分享-目录”,文章中提到的一个给前端er用的一个js开发工具箱.自己使用了一下,非常好用,代码压缩,代码美化,加密,解密之类基本功能都有,生成二维 ...
- selenium依次点击页面的删除按钮
需要依次点击页面的删除按钮,如下图: @Test public static void FaBu() { TestMenuJump.jumpExam(driver); TestMenuJump.jum ...