题目传送门

  传送门I

  传送门II

  传送门III

题目大意

  有$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 - 并查集的更多相关文章

  1. Codeforces 1027F. Session in BSU

    题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...

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

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

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

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

  4. cf1027F. Session in BSU(并查集 匈牙利)

    题意 题目链接 $n$个人,每个人可以在第$a_i$天或第$b_i$,一天最多考一场试,问在最优的情况下,最晚什么时候结束 Sol 自己只能想到暴力匈牙利二分图匹配,然而还是被构造数据卡了.. 标算很 ...

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

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

  6. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  7. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  8. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  9. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

随机推荐

  1. Web开发(调试方法 F12)

    参考: 参考:MDN 调试HTML 参考:什么是浏览器开发者工具? 参考:检查和编辑页面与样式 工具参考:标记验证服务 工具参考:直接输入验证(直接输入HTML源码进行在线检查) 目录: 1.相关快捷 ...

  2. 2018年年度总结 & 2019年计划

      2018关键词 「探索」 引用以前作文最爱写的开头,时间如白驹过隙,回想上次写17年年度总结,仿佛也就过了几日光景.   首先回顾一下17年定下的目标, 18年我将关键字设为探索,目的有两个,一是 ...

  3. 用web技术写APP

    _ 听说会写web的人也能写APP, 抱着尝试的心态踏上路体验一番 http://www.ionic.wang/start-index.html 0x00 框架环境介绍 ionic是什么? https ...

  4. 一对多关联模型,BELONGS_TO

    先分别创建三张表:test_user   test_message test_user 表里有id.name字段 test_message 表里有id.content.uid字段 然后建立一个Mode ...

  5. numpy(五)

    排序: x=np.array([2,5,6,2,3,5]) np.sort(x)  不改变原数组 x.sort() 改变原数组 i=np.argsort(x) 返回排序好的索引值 x[i] 使用花哨索 ...

  6. Install Superset from Python3.6

    本文安装Superset大致分为以下部分: 在操作系统中安装相关依赖,我所用的操作系统为Centos6.5 安装Python3.6.6 安装Superset 详细步骤如下: 相关依赖的安装 yum i ...

  7. 常见MQTT服务器搭建与试用

    常见MQTT服务器搭建与试用   简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠 ...

  8. ADB——应用交互

    使用ADB与手机应用交互 应用交互的操作包括:启动应用 / 调起Activity.调起Services.停止Service.发送广播.强行停止应用 基本命令 am <command> 常用 ...

  9. StringBuufer与StringBulder线程的区别

    StringBulder的append方法 不是synchronized修饰的   线程不安全     效率相对很快StringBuufer的append方法  是 synchronized修饰的  ...

  10. AMBER: CPPTRAJ Tutorial C0

    CPPTRAJ作为PTRAJ的继任者,拥有比PTRAJ更强大的功能,本教程会简要的介绍CPPTRAJ的用法及注意事项. 需要的文件: trpzip2.gb.nc trpzip2.ff10.mbondi ...