题目传送门

  传送门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. Nginx(一)-windows下的安装配置

    第一步 下载 官网下载地址 因为只是测试这里选择最新版本1.13.9 下载完成得到zip压缩包 解压后得到如下目录 第二步  启动nginx 注意不要直接双击nginx.exe,这样会导致修改配置后重 ...

  2. 2018-2019-2 网络对抗技术 20165336 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165336 Exp4 恶意代码分析 1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或E ...

  3. Django---cookie和session

    Django的cookie和session 一.cookie 二.session 回到顶部 一.cookie 1.特点 1. cookie数据保存在客户端,以key-value存储 2. cookie ...

  4. 2019春第七周作业Compile Summarize

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能更加进一步的够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 指针对于基础题目的做法 参考文献与 ...

  5. Nginx配置不当可能导致的安全问题

    Nginx配置不当可能导致的安全问题 Auther: Spark1e目前很多网站使用了nginx或者tenginx(淘宝基于Nginx研发的web服务器)来做反向代理和静态服务器,ningx的配置文件 ...

  6. linux----------fedora 27 如何启用输入法

    1.安装完成以后是自带了输入法的,但是需要启用. 一定要放到第一个位置,然后注销或者重启.

  7. c# webapi上传、读取、删除图片

    public class FileAPIController : BaseController    {        private readonly string prefix = "t ...

  8. Install Sudo for Debian

    $ su $ apt-get install sudo $ vim /etc/sudoers 1 2 3 在文本中添加: "username" ALL=(ALL) ALL 1 保存 ...

  9. java-redis

    pom.xml添加如下配置: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  10. Catch That Cow (BFS广搜)

    问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...