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个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
随机推荐
- JAVA初级复习知识点归纳
JDK的安装: 下载.安装 配置环境变量 a) path:.;%JAVA_HOME%\bin; b) JAVA_HOME:JDK的安装目录 c) classpath JDK和JRE和JVM: JAVA ...
- Servlet处理日期
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...
- tsdb import 相关
今天一直在做opentsdb 大量导入数据的工作. 中间遇到了一些值得记录的问题, 这里随手记一下 明天好好整理 1. 多进程logger python的logging模块不支持多进程,但我们可以用s ...
- maven bug之Maven:Non-resolvable parent POM: Failure to find错误
使用Maven编译淘宝的TimeTunnel项目时遇到如下错误: [INFO] Scanning for projects...[ERROR] The build could not read 1 p ...
- 学习swift从青铜到王者之字符串和运算符02
1 字符和字符串初步 var c :Character = "a" 2 构造字符串 let str1 = "hello" let str2 = " ...
- CCNA一些要点
考试范围: TK640-801(Cisco Certified Network Associate 640-801 ICND Course Notes). 信息单位: 1Byt ...
- [React] Use Prop Collections with Render Props
Sometimes you have common use cases that require common props to be applied to certain elements. You ...
- Kafka跨集群同步工具——MirrorMaker
MirrorMaker是为解决Kafka跨集群同步.创建镜像集群而存在的.下图展示了其工作原理.该工具消费源集群消息然后将数据又一次推送到目标集群. watermark/2/text/aHR0cDov ...
- ubuntu12.04通过Ganglia利用NVML模块进行GPU监控
1.安装Ganglia,这里安装的是3.1*版本,因为监控GPU的模块只支持3.1*版本系列的 apt-get install ganglia* 2.下载并安装PyNVML和NVML模块,下载地址ht ...
- HDU1052Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...