题意

题目链接

$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(并查集 匈牙利)的更多相关文章

  1. CF1027F Session in BSU (并查集+树上构造)

    题目大意:你可以在第$ai$天或者第$bi$天进行第$i$场考试,每天最多进行一场考试,求把所有考试都考完的最早结束时间 由于天数可能很大,需要离散 把问题抽象成一棵树,每个点最多被"分配& ...

  2. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  3. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  4. bzoj 1191: [HNOI2006]超级英雄Hero 并查集 || 匈牙利算法

    1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1804  Solved: 850[Submit][S ...

  5. CF1027F Session in BSU

    link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...

  6. [CF1027F]Session in BSU[最小基环树森林]

    题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...

  7. Codeforces.1027F.Session in BSU(思路 并查集)

    题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...

  8. codeforces1027F. Session in BSU

    题目链接 codeforces1027F. Session in BSU 题解 二分图匹配就fst了....显然是过去的,不过tle test87估计也pp了,好坑 那么对于上面做匹配的这个二分图分情 ...

  9. CF 1027 F. Session in BSU

    F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...

随机推荐

  1. Words Gems

    所有的东西都来自抄袭.来自学习.不同的是用新的方法做其他公司做过的事情.很多公司做同样的事情,但只有一家公司最成功.你要发现一个有需求的服务,并做得比别人更好,而不是比别人更早.

  2. Jmeter提取响应数据的结果保存到本地的一个文件

    原文地址: https://www.cnblogs.com/whitewasher/p/9504728.html 当做性能压测时,可能会需要把响应数据的一些字段统计出来.这里简单介绍一下. 1.首先把 ...

  3. 安装pillow

    最近想学Python的图像操作.首要任务就是安装pillow.这个强大的图形处理工具. 但是我遇到了一个问题. Collecting pilow  Could not find a version t ...

  4. HDU-5980

    Find Small A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. 转:AppScan代理扫描app/H5安全测试

    1.首先设置AppScan代理,设置如下:

  6. 《SpringBoot揭秘 快速构建微服务体系》读后感(二)

    最简单的springBoot应用 package com.louis.test; import org.springframework.boot.SpringApplication; import o ...

  7. Do not have XXX handler in current page

    这种错误没有什么技术含量,也很容易解决. 一般就是wxml里面的button/form之类的,你用bindtap/bindsubmit给它绑了一个XXX函数,但是呢,你没有在相关js页面里面定义这个函 ...

  8. MongoDB官方C#驱动的AsQueryable踩到坑了

    collection.AsQueryable().Where()有4个重载,分别是: public static IQueryable<TSource> Where<TSource& ...

  9. AI决策算法 之 GOAP (一)

    http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...

  10. > 软件编程 > 安卓开发 > Unity编译时找不到AndroidSDK的问题:Unable to list target pla

    http://www.qingpingshan.com/rjbc/az/228769.html 现象 在用 Unity 编译 Android 平台的应用时,遇到 Unable to list targ ...