一 原题
F. Session in BSU
time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly nnexams.
For the each exam ii there are known two days: aiai — day of the first opportunity to pass the exam, bibi — day of the second opportunity to pass the exam (ai<biai<bi). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the nn exams.
Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the nn exams, or print -1 if he cannot pass all the exams at all.
Input
The first line of the input contains one integer nn (1≤n≤1061≤n≤106) — the number of exams.
The next nn lines contain two integers each: aiai and bibi (1≤ai<bi≤1091≤ai<bi≤109), where aiai is the number of day of the first passing the ii-th exam and bibi is the number of day of the second passing the ii-th exam.
Output
If Polycarp cannot pass all the nn exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that.
Examples
input
Copy
2 1 5 1 7
output
Copy
5
input
Copy
3 5 13 1 5 1 7
output
Copy
7
input
Copy
3 10 40 40 80 10 80
output
Copy
80
input
Copy
3 99 100 99 100 99 100
output
Copy
-1
二 分析
离散化之后问题描述如下:给你一个无向图,顶点标号1-n,要求从每条边的两个顶点中选出一个,点不能被重复选。要最小化这些点的标号最大值。
考虑一个连通分量(感觉这个操作就像高中做数论题时考虑一个数的素因子一样),顶点数nv和边数ne有三种情况:
1. ne = nv - 1 (树)
2. ne = nv (基环图)
3. ne > nv
分别讨论一下就好了。
三 代码
/*
AUTHOR: maxkibble
LANG: c++
PROB: 1027f
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
const int maxn = 2e6 + 5;
int n;
int e[maxn][2];
int es[maxn], cnt;
int fa[maxn], nv[maxn], ne[maxn], maxv[maxn][2];
int root(int x) {
return fa[x] == x? x: fa[x] = root(fa[x]);
}
inline void unite(int x, int y) {
x = root(x);
y = root(y);
ne[x]++;
if (x == y) return;
fa[y] = x;
nv[x] += nv[y];
ne[x] += ne[y];
if (maxv[x][0] > maxv[y][0]) {
maxv[x][1] = max(maxv[y][0], maxv[x][1]);
} else {
maxv[x][1] = max(maxv[x][0], maxv[y][1]);
maxv[x][0] = maxv[y][0];
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &e[i][j]);
es[cnt++] = e[i][j];
}
}
sort(es, es + cnt);
int m = unique(es, es + cnt) - es;
for (int i = 0; i < n; i++) {
e[i][0] = lower_bound(es, es + m, e[i][0]) - es;
e[i][1] = lower_bound(es, es + m, e[i][1]) - es;
}
for (int i = 0; i < m; i++) {
fa[i] = i;
nv[i] = 1;
maxv[i][0] = i;
maxv[i][1] = -1;
}
for (int i = 0; i < n; i++) {
unite(e[i][0], e[i][1]);
}
int ans = -1;
for (int i = 0; i < m; i++) {
if (root(i) != i) continue;
// printf("%d %d %d %d %d\n", i, nv[i], ne[i], maxv[i][0], maxv[i][1]);
if (nv[i] < ne[i]) {
puts("-1");
return 0;
}
if (nv[i] == ne[i])
ans = max(ans, maxv[i][0]);
else
ans = max(ans, maxv[i][1]);
}
printf("%d\n", es[ans]);
return 0;
}