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个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
随机推荐
- Ubuntu 16.04粘贴板增强工具Diodon
相比Parcellite(http://www.cnblogs.com/EasonJim/p/7119308.html),Diodon可以支持图片. 安装: sudo add-apt-reposito ...
- 条款五:对应的new和delete要采用相同的形式
string *stringarray = new string[100]; ... delete stringarray; 上述程序的运行情况将是不可预测的.至少,stringarray指向的100 ...
- JavaScript错误处理和堆栈追踪
转自:https://github.com/dwqs/blog/issues/49 有时我们会忽略错误处理和堆栈追踪的一些细节, 但是这些细节对于写与测试或错误处理相关的库来说是非常有用的. 例如这周 ...
- REST API 安全设计
REST API 安全设计 2017年04月27日 18:34:27 阅读数:1699 Rest API 的那些事儿 作者/ asterisk 在软件行业快速发展的今天,传统的软件授权已经不能足以 ...
- js 合并对象
对象的合并 需求:设有对象 o1 ,o2,需要得到对象 o3 var o1 = { a:'a' }, o2 = { b:'b' }; // 则 var o3 = { a:'a', b:'b' } 方法 ...
- 【Linux多线程】同步与互斥的区别
同步与互斥这两个概念经常被混淆,所以在这里说一下它们的区别. 一.同步与互斥的区别 1. 同步 同步,又称直接制约关系,是指多个线程(或进程)为了合作完成任务,必须严格按照规定的 某种先后次序来运行. ...
- php生成二维码2
<?php include "phpqrcode.php"; $value = "http://huizhongda.taobao.com/"; $err ...
- XML(一)语法
一.xml语法 1.文档声明 2.元素 3.属性 4.凝视 5.CDATA区.转义字符 6.处理指令 1.文档声明: 用来声明xml的基本属性,用来指挥解析引擎怎样去解析当前xml 通常一个xml都要 ...
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第五步--button控制主角Jump&Crouch
说明:这里是借鉴:晓风残月前辈的博客.他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...
- Ubuntu虚拟机安装遇到的各种坑
配置 13年Macbook Pro 虚拟机环境 Parallels Desktop Linux 版本 Ubuntu 16.04 1.分辨率问题 进入只有一种分辨率 终端输入 sudo xdiagnos ...