poj2289 Jamie's Contact Groups
思路:
二分+最大流。
实现:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream> #define N (1500 + 2)
#define M (N * N + 4 * N) typedef long long LL; using namespace std; struct edge
{
int v, cap, next;
};
edge e[M]; int head[N], level[N], cur[N];
int num_of_edges; /*
* When there are multiple test sets, you need to re-initialize before each
*/
void dinic_init(void)
{
num_of_edges = ;
memset(head, -, sizeof(head));
return;
} int add_edge(int u, int v, int c1, int c2)
{
int& i = num_of_edges; assert(c1 >= && c2 >= && c1 + c2 >= ); // check for possibility of overflow
e[i].v = v;
e[i].cap = c1;
e[i].next = head[u];
head[u] = i++; e[i].v = u;
e[i].cap = c2;
e[i].next = head[v];
head[v] = i++;
return i;
} void print_graph(int n)
{
for (int u = ; u < n; u++)
{
printf("%d: ", u);
for (int i = head[u]; i >= ; i = e[i].next)
{
printf("%d(%d)", e[i].v, e[i].cap);
}
printf("\n");
}
return;
} /*
* Find all augmentation paths in the current level graph
* This is the recursive version
*/
int dfs(int u, int t, int bn)
{
if (u == t) return bn;
int left = bn;
for (int &i = cur[u]; i >= ; i = e[i].next)
{
int v = e[i].v;
int c = e[i].cap;
if (c > && level[u] + == level[v])
{
int flow = dfs(v, t, min(left, c));
if (flow > )
{
e[i].cap -= flow;
e[i ^ ].cap += flow;
cur[u] = i;
left -= flow;
if (!left) break;
}
}
}
if (left > ) level[u] = ;
return bn - left;
} bool bfs(int s, int t)
{
memset(level, , sizeof(level));
level[s] = ;
queue<int> q;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == t) return true;
for (int i = head[u]; i >= ; i = e[i].next)
{
int v = e[i].v;
if (!level[v] && e[i].cap > )
{
level[v] = level[u] + ;
q.push(v);
}
}
}
return false;
} LL dinic(int s, int t)
{
LL max_flow = ; while (bfs(s, t))
{
memcpy(cur, head, sizeof(head));
max_flow += dfs(s, t, INT_MAX);
}
return max_flow;
} vector<int> v[N];
int n, m;
bool check(int x)
{
dinic_init();
for (int i = ; i <= n; i++)
{
for (int j = ; j < v[i].size(); j++)
{
add_edge(i, v[i][j] + n + , , );
}
}
for (int i = ; i <= n; i++)
add_edge(, i, , );
for (int j = n + ; j <= n + m; j++)
{
add_edge(j, n + m + , x, );
}
return dinic(, n + m + ) == n;
} int main()
{
while (cin >> n >> m, n || m)
{
getchar();
string s, name;
int group;
for (int i = ; i <= n; i++) v[i].clear();
for (int i = ; i <= n; i++)
{
getline(cin, s);
stringstream ss(s);
ss >> name;
while (ss >> group)
{
v[i].push_back(group);
}
}
int l = , r = n, ans = n;
while (l <= r)
{
int m = (l + r) >> ;
if (check(m))
{
r = m - ; ans = m;
}
else l = m + ;
}
cout << ans << endl;
}
return ;
}
poj2289 Jamie's Contact Groups的更多相关文章
- POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 6 ...
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 7721 Accepted: ...
- POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)
Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/ ...
- 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 ...
- Jamie's Contact Groups POJ - 2289(多重匹配 最大值最小化 最大流)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 8567 Accepted: ...
- 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【二分+最大流】【二分图多重匹配问题】
题目链接: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 (二分+二分图多重匹配)
题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
随机推荐
- fastjson过滤器简单记录
fastjson过滤器,该字段可以将转化的json字段遍历,方便实用 1 /** * 通用输出json * @param object * @return json字符串 */ public Stri ...
- 看我如何基于Python&Facepp打造智能监控系统
由于种种原因,最近想亲自做一个基于python&facepp打造的智能监控系统. 0×00:萌芽 1:暑假在家很无聊 想出去玩,找不到人.玩个lol(已卸载),老是坑人.实在是无聊至极,不过, ...
- Application特征
1.位置:服务器内存,执行速度快2.使用范围:整个应用程序3.类型:任意类型4.声明周期:应用程序开始创建到销毁
- bootstrap模态框出现或者消失的回调函数
当某一模态框出现的时候就触发函数: $(".modal").on('show.bs.modal',function(){ if(vueObj){...}else{//如果vue对象 ...
- Windows命令实现匿名邮件发送
在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...
- 开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行
开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行 内容收录,外链公布,流量点击.用户体验.这是SEO优化的几个核心和重点.也是SEO站长每天都在绞尽脑汁进行操作的SEO重心,影响着非常多人 ...
- TensorFlow的安装与CNN测试
0.说明 在Google开源该框架之后便使用真实K40m卡测试,由于生产环境是CentOS6.6的操作系统,但是该框架需要在Python2.7环境下执行,CentOS6.6下折腾了一天没搞定,后来换成 ...
- web 开发之js---js 多线程编程
AJAX 开发中的难题 试试多线程编程 想了解更多 有关作者 转载注明出处:http://www.infoq.com/cn/articles/js_multithread 虽然有越来越多的网站在应 ...
- python3 使用http.server模块 搭建一个简易的http服务器
from http.server import HTTPServer, BaseHTTPRequestHandler import json data = {'result': 'this is a ...
- Codeforces Round #311 (Div. 2)C. Arthur and Table
C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...