cf1027F. Session in BSU(并查集 匈牙利)
题意
$n$个人,每个人可以在第$a_i$天或第$b_i$,一天最多考一场试,问在最优的情况下,最晚什么时候结束
Sol
自己只能想到暴力匈牙利二分图匹配,然而还是被构造数据卡了。。
标算很神奇。
同样考虑把题目中给出的模型建成二分图,左侧代表每个人,右侧代表每一天的考试
然后我们把右侧每个人能选择的两个点之间连边
这样模型就由二分图转化成了一条链上的问题。
分情况讨论一下:
考虑当前的联通块:
1、边数大于点数:因为每个条边都必须与一个点匹配,因此这样肯定无解
2、边数 = 点数:很显然是一棵基环树,输出最大值即可
3、边数 < 点数:这是一棵树,输出次大值即可
这样我们用并查集维护一下最大值,次大值。就做完了。。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define LL long long
using namespace std;
const int MAXN = 1e6 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
int a[MAXN], b[MAXN], date[MAXN << ], cnt, vis[MAXN], link[MAXN], atk[MAXN];
vector<int> v[MAXN];
int Aug(int x, int tim) {
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(vis[to] == tim) continue;
vis[to] = tim;
if(!link[to] || Aug(link[to], tim))
{
link[to] = x;
return ;
}
}
return ;
}
main() {
N = read();
for(int i = ; i <= N; i++) a[i] = read(), b[i] = read(), date[++cnt] = a[i], date[++cnt] = b[i];
sort(date + , date + cnt + );
cnt = unique(date + , date + cnt + ) - date - ;
for(int i = ; i <= N; i++) {
a[i] = lower_bound(date + , date + cnt + , a[i]) - date,
b[i] = lower_bound(date + , date + cnt + , b[i]) - date;
v[a[i]].push_back(i);
v[b[i]].push_back(i);
}
int ans = , tot = ;
for(int i = ; i <= cnt; i++) {
if(Aug(i, i))
tot++;
if(tot == N) {
printf("%d", date[i]); return ;
}
}
puts("-1");
return ;
}
/*
3
23 27
22 26
27 30
*/
暴力匈牙利TLE ON TEST 56
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = * 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, a[MAXN], b[MAXN], date[MAXN << ], cnt = , fa[MAXN], tag[MAXN], mx[MAXN], smx[MAXN];
int find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = find(fa[x]);
}
int main() {
N = read();
for(int i = ; i <= N; i++) a[i] = read(), b[i] = read(), date[++cnt] = a[i], date[++cnt] = b[i];
sort(date + , date + cnt + );
cnt = unique(date + , date + cnt + ) - date - ;
for(int i = ; i <= N; i++)
a[i] = lower_bound(date + , date + cnt + , a[i]) - date,
b[i] = lower_bound(date + , date + cnt + , b[i]) - date;
for(int i = ; i <= cnt; i++) fa[i] = i, mx[i] = i; for(int i = ; i <= N; i++) {
int x = a[i], y = b[i];
int fx = find(x), fy = find(y);
if(find(x) != find(y)) {
fa[fy] = fx; tag[fx] |= tag[fy];
if(mx[fy] > mx[fx]) smx[fx] = max(smx[fx], max(smx[fy], mx[fx])), mx[fx] = mx[fy];
else smx[fx] = max(smx[fx], mx[fy]);
}
else if(!tag[fx]) tag[fx] = ;
else {puts("-1"); return ;}
}
int ans = ;
for(int i = ; i <= cnt; i++) {
if(fa[i] == i)
if(tag[i]) ans = max(ans, mx[i]);
else ans = max(ans, smx[i]);
}
printf("%d\n", date[ans]);
return ;
}
cf1027F. Session in BSU(并查集 匈牙利)的更多相关文章
- CF1027F Session in BSU (并查集+树上构造)
题目大意:你可以在第$ai$天或者第$bi$天进行第$i$场考试,每天最多进行一场考试,求把所有考试都考完的最早结束时间 由于天数可能很大,需要离散 把问题抽象成一棵树,每个点最多被"分配& ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- bzoj 1191: [HNOI2006]超级英雄Hero 并查集 || 匈牙利算法
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1804 Solved: 850[Submit][S ...
- CF1027F Session in BSU
link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...
- [CF1027F]Session in BSU[最小基环树森林]
题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- codeforces1027F. Session in BSU
题目链接 codeforces1027F. Session in BSU 题解 二分图匹配就fst了....显然是过去的,不过tle test87估计也pp了,好坑 那么对于上面做匹配的这个二分图分情 ...
- CF 1027 F. Session in BSU
F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...
随机推荐
- SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题
SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. P ...
- Springboot学习七 spring的一些注解
一 事务控制 @Service public class CityServiceImpl implements CityService { @Autowired private CityMapper ...
- iterator与iterable的区别和联系
iterator与iterable 用Iterator模式实现遍历集合Iterator模式是用于遍历集合类的标准访问方法.它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内 ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
2017-03-16 11:23:29.601 1238 ERROR nova.compute.manager [instance: 3f195047-250a-4eb5-8da0-63bea6e26 ...
- YOLO3训练widerface数据集
因为YOLO3速度精度都很棒,所以想训练一下人脸模型,废话不多,进入正题 1写所有的配置文件 1.1 YOLO3-face.cfg 个人感觉YOLO的配置文件骑士和caffe差不多 在cfg/YOLO ...
- JS判断上传文件类型
/* * 判断图片类型 */ function checkImgType(ths){ if (ths.value == "") { ...
- C# 写 LeetCode easy #1 Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- 网站特效离不开脚本,javascript是最常用的脚本语言,我们归纳一下常用的基础函数和语法:
转载自网络,非原创 1.输出语句:document.write(""); 2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head ...
- 无法加载MainifestResourceTransformer
Cannot load implementation hint 'org.apache.maven.plugins.shade.resource.MainifestResourceTransforme ...
- OVN学习(三)
部署OVN实验环境 同OVN学习(一) 网关 在L3网络基础上部署网关 添加L3网关 ### Central节点 # ovn-sbctl show Chassis "8bd09faf-5ba ...