【HDOJ】1669 Jamie's Contact Groups
二分+二分图多重匹配。
/* 1669 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
const int maxm = ;
bool M[maxn][maxm];
bool visit[maxm];
int can[maxm][maxm];
int sz[maxm];
int n, gn, bound; bool dfs(int u) {
rep(i, , gn) {
if (M[u][i] && !visit[i]) {
visit[i] = true;
if (sz[i] < bound) {
can[i][sz[i]++] = u;
return true;
}
rep(j, , sz[i]) {
if (dfs(can[i][j])) {
can[i][j] = u;
return true;
}
}
}
} return false;
} bool judge(int bound_) {
bound = bound_;
memset(sz, , sizeof(sz));
rep(i, , n) {
memset(visit, false, sizeof(visit));
if (!dfs(i))
return false;
} return true;
} void solve() {
int l, r, mid;
int ans; l = ;
ans = r = n;
while (l <= r) {
mid = (l + r) >> ;
if (judge(mid)) {
ans = mid;
r = mid - ;
} else {
l = mid + ;
}
} printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif char name[];
int gid;
char ch; while (scanf("%d %d", &n, &gn)!=EOF && (n||gn)) {
memset(M, false, sizeof(M));
rep(i, , n) {
scanf("%s", name);
while () {
scanf("%d%c", &gid, &ch);
M[i][gid] = true;
if (ch == '\n')
break;
}
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
二分+网络流Dinic也可以解。
/* 1669 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int v, c, nxt;
} Edge_t; const int INF = 0x3f3f3f3f;
const int maxn = ;
const int maxm = ;
const int maxv = maxn + maxm;
const int maxe = maxn*maxm*+;
Edge_t E[maxe];
int F[maxe];
int head[maxv], head_[maxv];
int id[maxm];
int dis[maxv], Q[maxv];
int m;
int n, gn;
int s, t; void init() {
memset(head, -, sizeof(head));
m = ;
s = ;
t = n + gn + ;
} void addEdge(int u, int v, int c) {
E[m].v = v;
E[m].c = c;
E[m].nxt = head[u];
head[u] = m++; E[m].v = u;
E[m].c = ;
E[m].nxt = head[v];
head[v] = m++;
} bool bfs() {
int l = , r = ;
int u, v, k; Q[r++] = s;
memset(dis, , sizeof(dis));
dis[s] = ; while (l < r) {
u = Q[l++];
for (k=head[u]; k!=-; k=E[k].nxt) {
v = E[k].v;
if (!dis[v] && E[k].c>F[k]) {
dis[v] = dis[u] + ;
if (v == t)
return false;
Q[r++] = v;
}
}
} return true;
} int dfs(int u, int val) {
if (u==t || val==)
return val; int ret = ;
int tmp, v; for (int& k=head_[u]; k!=-; k=E[k].nxt) {
v = E[k].v;
if (dis[v]==dis[u]+ && E[k].c>F[k] && (tmp=dfs(v, min(val, E[k].c-F[k])))>) {
F[k] += tmp;
F[k^] -= tmp;
ret += tmp;
val -= tmp;
if (val == )
break;
}
} return ret;
} int Dinic() {
int ret = , tmp; while () {
if (bfs())
break; memcpy(head_, head, sizeof(head));
while () {
tmp = dfs(s, INF);
if (tmp == )
break;
ret += tmp;
}
}
#ifndef ONLINE_JUDGE
printf("Dinic = %d\n", ret);
#endif
return ret;
} bool judge(int bound) {
rep(i, , gn+)
E[id[i]].c = bound;
memset(F, , sizeof(F)); return Dinic()>=n;
} void solve() {
int l = , r = n, mid;
int ans = -; while (r >= l) {
mid = (l + r) >> ;
if (judge(mid)) {
ans = mid;
r = mid - ;
} else {
l = mid + ;
}
} printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif char name[];
char ch;
int gid; while (scanf("%d %d", &n, &gn)!=EOF && (n||gn)) {
init();
rep(i, , n+) {
scanf("%s", name);
while () {
scanf("%d%c", &gid, &ch);
addEdge(i, gid+n+, );
if (ch == '\n')
break;
}
}
rep(i, , n+)
addEdge(s, i, );
rep(i, , gn+) {
id[i] = m;
addEdge(n+i, t, );
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】1669 Jamie's Contact Groups的更多相关文章
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- 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 / 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 ...
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 7721 Accepted: ...
- 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: ...
- POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)
Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/ ...
- POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 6 ...
随机推荐
- Java validator整理
Java validator整理 因为想对方法的入参和出参作简单的非空或者非空字符做校验,所以找了下相关的@NotNull注解 类 | 说明 --- | --- javax.validation.co ...
- springmvc学习(四)
1.使用 @CookieValue 绑定请求中的 Cookie 值 例子: java @RequestMapping(value="/testCookieValue") publi ...
- mysql---左连接、右连接、内连接之间的区别与联系
现有两张表 第一张表为男生表,记录了男生的姓名和配偶的编号 第二张表为女生表,记录了女生的姓名和自己的编号 第一种情况:主持人请所有男生都上台,并且带上自己的配偶.这时不管男生有没有配偶都要上台,所以 ...
- 我爱工程化 之 gulp 使用(二)
上一篇 介绍了gulp的安装.环境等配置.基本使用,那么现在,我们快走进 速8,深入了解吧...... 一.各种安装.环境配置.插件安装(参考上一篇文章) 二.项目基本目录结构 三.编写 gulpf ...
- 关于WPF中Popup控件的小记
在wpf开发中,常需要在鼠标位置处弹出一个“提示框”(在此就以“提示框”代替吧),通过“提示框”进行信息提示或者数据操作,如果仅仅是提示作用,使用ToolTip控件已经足够,但是有些是需要在弹出的框中 ...
- 微信公众号-5秒内不回复测试并处理方案,顺便复习php 时间执行
在index.php中 file_put_contents('has_request.txt','请求时间:'.date('YmdHis')."\n",FILE_APPEND); ...
- PHP初学留神(三)
星期一进行面试结束后,意味着我的考研日子也结束了,以及我的2013.在好好总结之后还不能停止学习,心想着要把算法继续学下去,还有Linux.不过呢,始终都要记住尼采老师的这句当头棒喝:“不加选择的知识 ...
- centos不能挂在ntfs
root@s 下载]# mount /dev/sdb1 /mnt mount: unknown filesystem type 'ntfs' wget http://www.tuxera.com/co ...
- R语言和大数据
#安装R语言R3.3版本会出现各种so不存在的问题,退回去到R3.1版本时候就顺利安装.在安装R环境之前,先安装好中文(如果没有的话图表中显示汉字成框框了)和tcl/tk包(少了这个没法安装sqldf ...
- jdbc 连接Oracle informix Mysql
package com.basicSql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...