版权声明:get busy living...or get busy dying https://blog.csdn.net/qq_41444888/article/details/89319367
http://poj.org/problem?id=2289
二分流入汇点的流量,保证最小化最大值,但是二分真是个很严重的问题,哎
直接把两个AC代码挂上吧,硬刚是刚不过了,会用就行了。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 50005;
int cnt, st, en;
int n, m;
int u, v, w;
struct node
{
int to, next, w;
}e[2000005];
int head[maxn], depth[maxn], cur[maxn];
void init()
{
st = 0, en = n + m + 1;
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w)
{
e[cnt].w = w;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
e[cnt].w = 0;
e[cnt].to = u;
e[cnt].next = head[v];
head[v] = cnt ++;
}
int dfs(int u, int flow)
{
if(u == en) return flow;
for(int& i = cur[u]; ~i; i = e[i].next)
{
if(depth[e[i].to] == depth[u] + 1 && (e[i].w != 0))
{
int zen = dfs(e[i].to, min(e[i].w, flow));
if(zen)
{
e[i].w -= zen;
e[i^1].w += zen;
return zen;
}
}
}
return 0;
}
int bfs()
{
queue<int> q;
while(! q.empty()) q.pop();
memset(depth, 0, sizeof(depth));
depth[st] = 1;
q.push(st);
while(! q.empty())
{
int h = q.front();
q.pop();
for(int i = head[h]; ~i; i = e[i].next)
{
if((! depth[e[i].to]) && e[i].w)
{
depth[e[i].to] = depth[h] + 1;
q.push(e[i].to);
}
}
}
if(depth[en]) return 1;
return 0;
}
int dinic()
{
int ans = 0;
while(bfs())
{
for(int i = 0; i <= n + m + 1; i ++)
cur[i] = head[i];
while(int d = dfs(st, inf))
ans += d;
}
return ans;
}
char tmp[20];
int tmp1;
vector<int> V[maxn];
int main()
{
while(scanf("%d%d", &n, &m) != EOF && (n + m))
{
for(int i = 1; i <= n; i ++)
{
V[i].clear();
scanf("%s", tmp);
while(getchar() != '\n')
{
scanf("%d", &tmp1);
V[i].push_back(tmp1+1);
}
}
int l = 1, r = n, mid;
//int ans = 0;
while(l < r)
{
init();
mid = (l + r) / 2;
for(int i = 1; i <= n; i ++)
addedge(0, i, 1);
for(int i = 1; i <= m; i ++)
addedge(i + n, n + m + 1, mid);
for(int i = 1; i <= n; i ++)
for(int j = 0; j < V[i].size(); j ++)
addedge(i, V[i][j] + n, 1);
if(dinic() == n)
{
r = mid;
}
else l = mid + 1;
}
printf("%d\n", l);
}
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 50005;
int cnt, st, en;
int n, m;
int u, v, w;
struct node
{
int to, next, w;
}e[2000005];
int head[maxn], depth[maxn], cur[maxn];
void init()
{
st = 0, en = n + m + 1;
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w)
{
e[cnt].w = w;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
e[cnt].w = 0;
e[cnt].to = u;
e[cnt].next = head[v];
head[v] = cnt ++;
}
int dfs(int u, int flow)
{
if(u == en) return flow;
for(int& i = cur[u]; ~i; i = e[i].next)
{
if(depth[e[i].to] == depth[u] + 1 && (e[i].w != 0))
{
int zen = dfs(e[i].to, min(e[i].w, flow));
if(zen)
{
e[i].w -= zen;
e[i^1].w += zen;
return zen;
}
}
}
return 0;
}
int bfs()
{
queue<int> q;
while(! q.empty()) q.pop();
memset(depth, 0, sizeof(depth));
depth[st] = 1;
q.push(st);
while(! q.empty())
{
int h = q.front();
q.pop();
for(int i = head[h]; ~i; i = e[i].next)
{
if((! depth[e[i].to]) && e[i].w)
{
depth[e[i].to] = depth[h] + 1;
q.push(e[i].to);
}
}
}
if(depth[en]) return 1;
return 0;
}
int dinic()
{
int ans = 0;
while(bfs())
{
for(int i = 0; i <= n + m + 1; i ++)
cur[i] = head[i];
while(int d = dfs(st, inf))
ans += d;
}
return ans;
}
char tmp[20];
int tmp1;
vector<int> V[maxn];
int main()
{
while(scanf("%d%d", &n, &m) != EOF && (n + m))
{
for(int i = 1; i <= n; i ++)
{
V[i].clear();
scanf("%s", tmp);
while(getchar() != '\n')
{
scanf("%d", &tmp1);
V[i].push_back(tmp1+1);
}
}
int l = 1, r = n, mid;
int ans = 0;
while(l <= r)
{
init();
mid = (l + r) / 2;
for(int i = 1; i <= n; i ++)
addedge(0, i, 1);
for(int i = 1; i <= m; i ++)
addedge(i + n, n + m + 1, mid);
for(int i = 1; i <= n; i ++)
for(int j = 0; j < V[i].size(); j ++)
addedge(i, V[i][j] + n, 1);
if(dinic() == n)
{
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}