Codeforces 1027F Session in BSU - 并查集
题目传送门
题目大意
有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加一场考试,不能在同一个时间参加两场考试,问最后参加的考试最早的时间是什么。
这几天,我怎么做的都是水题Emm....
考虑先将$a_i, b_i$离散化。
对于每一门考试,在$a_i, b_i$间连一条无向边。
对于每个连通块,讨论:
- 如果边数大于点数,显然无解
- 如果边数等于点数,那么答案必须大于等于点权中的最大值。
- 如果边数小于点数,那么最大不可用的时候会产生若干个基环树,所以答案必须大于等于点权中的次大值。
并查集随便维护一下就行了。
Code
/**
* Codeforces
* Problem#1027F
* Accepted
* Time: 873ms
* Memory: 63000k
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
typedef bool boolean; template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} typedef class Input {
protected:
const static int limit = ;
FILE* file; int ss, st;
char buf[limit];
public: Input():file(NULL) { };
Input(FILE* file):file(file) { } void open(FILE *file) {
this->file = file;
} void open(const char* filename) {
file = fopen(filename, "r");
} char pick() {
if (ss == st)
st = fread(buf, , limit, file), ss = ;//, cerr << "str: " << buf << "ed " << st << endl;
return buf[ss++];
}
}Input; #define digit(_x) ((_x) >= '0' && (_x) <= '9') Input& operator >> (Input& in, unsigned& u) {
char x;
while (~(x = in.pick()) && !digit(x));
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
return in;
} Input& operator >> (Input& in, unsigned long long& u) {
char x;
while (~(x = in.pick()) && !digit(x));
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
return in;
} Input& operator >> (Input& in, int& u) {
char x;
while (~(x = in.pick()) && !digit(x) && x != '-');
int aflag = ((x == '-') ? (x = in.pick(), -) : ());
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
u *= aflag;
return in;
} Input& operator >> (Input& in, long long& u) {
char x;
while (~(x = in.pick()) && !digit(x) && x != '-');
int aflag = ((x == '-') ? (x = in.pick(), -) : ());
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
u *= aflag;
return in;
} Input in (stdin); #define pii pair<int, int> int n, m;
int *uf;
int *es;
int *mx, *smx;
pii* ps;
vector<int> br; int find(int x) {
return (uf[x] == x) ? (x) : (uf[x] = find(uf[x]));
} inline void init() {
in >> n;
ps = new pii[(n + )];
for (int i = ; i <= n; i++)
in >> ps[i].first >> ps[i].second;
} inline void descrete() {
for (int i = ; i <= n; i++) {
br.push_back(ps[i].first);
br.push_back(ps[i].second);
}
sort(br.begin(), br.end());
m = unique(br.begin(), br.end()) - br.begin();
for (int i = ; i <= n; i++) {
ps[i].first = lower_bound(br.begin(), br.begin() + m, ps[i].first) - br.begin();
ps[i].second = lower_bound(br.begin(), br.begin() + m, ps[i].second) - br.begin();
}
} void addEdge(int u, int v) {
if (find(u) != find(v)) {
u = find(u), v = find(v);
es[u] += es[v];
if (mx[u] < mx[v])
smx[u] = mx[u], mx[u] = mx[v];
else if (smx[u] < mx[v])
smx[u] = mx[v];
smx[u] = ((smx[u] > smx[v]) ? (smx[u]) : (smx[v]));
uf[v] = u;
}
es[find(u)]++;
} int res = -;
inline void solve() {
uf = new int[m];
es = new int[m];
mx = new int[m];
smx = new int[m]; for (int i = ; i < m; i++)
uf[i] = i, es[i] = -, mx[i] = i, smx[i] = -;
for (int i = ; i <= n; i++)
addEdge(ps[i].first, ps[i].second);
for (int i = ; i < m; i++)
if (find(i) == i) {
if (es[i] > ) {
res = -;
break;
}
if (!es[i])
res = max(res, mx[i]);
else
res = max(res, smx[i]);
} if (res == -)
puts("-1");
else
printf("%d\n", br[res]);
} int main() {
init();
descrete();
solve();
return ;
}
Codeforces 1027F Session in BSU - 并查集的更多相关文章
- Codeforces 1027F. Session in BSU
题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- cf1027F. Session in BSU(并查集 匈牙利)
题意 题目链接 $n$个人,每个人可以在第$a_i$天或第$b_i$,一天最多考一场试,问在最优的情况下,最晚什么时候结束 Sol 自己只能想到暴力匈牙利二分图匹配,然而还是被构造数据卡了.. 标算很 ...
- CF1027F Session in BSU (并查集+树上构造)
题目大意:你可以在第$ai$天或者第$bi$天进行第$i$场考试,每天最多进行一场考试,求把所有考试都考完的最早结束时间 由于天数可能很大,需要离散 把问题抽象成一棵树,每个点最多被"分配& ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
随机推荐
- iOS开发支付篇-内购(IAP)
一,前言 经典文章参考: . http://yimouleng.com/2015/12/17/ios-AppStore/ 内购流程 . http://www.jianshu.com/p/b199a46 ...
- react-native-Cocoapods-Swift-Project
https://reactnative.cn/docs/integration-with-existing-apps/ 1.创建一个xcode工程,single View就行,项目语言选择swift, ...
- h5跳转
<!doctype html> <html lang="en" style="width:100%; height:100%"> < ...
- Vue.js中集成summernote
首先引用summernote样式及js: <!--summernote css --> <link href="${ctxPath}/static/css/summerno ...
- luogu4643 [国家集训队]阿狸和桃子的游戏
题目链接:洛谷 这道题乍一看非常的难,而且题目标题上的标签让人很害怕. 但其实这道题并不难写(只要想到了...emm) 因为我们只需要知道两个人得分之差,所以我们可以对条件进行变换. 我们将边权平分到 ...
- Oracle 12.2报错ORA-15032、ORA-15410或ORA-15411解决
现象:在Oracle 12.2.0.1 RAC环境,在其ASM实例中,如果添加不同大小或者不同数量的LUN到failgroup中,会报错: ORA-15032: not all alterations ...
- 关于Oracle的44951事件
最近有同事遇到某客户数据库产生大量阻塞,等待事件为:enq HW - contention,最开始采用不断杀会话的方式,效果不好,问题一直高频反复.进一步确认SQL是大量的insert,且插入的表中含 ...
- 团队项目第一篇——NABCD
团队名称: 团队项目名称: 团队口号: N(Need)需求: 现如今数据越来越零碎化,繁杂化,身为在校大学生的我们也因此对于时间的利用率也相应减少,为了时间的充分利用,减少在冗杂的信息中耽误的时间,充 ...
- meta 标签汇总
HTML 里 meta常用标签: " class="mathcode" src="https://private.codecogs.com/gif.latex? ...
- 接口自动化测试持续集成--Soapui接口功能测试持续集成
Soapui接口功能测试持续集成,需要先安装好maven和jenkins,maven和jenkins安装和系统环境配置比较简单,在这里不做叙述. 1.Soapui保存的工程文件 soapui工程保 ...