cf 1027f Session in BSU

本文介绍了一个算法问题,即如何帮助学生尽可能早地完成所有必修考试。通过将问题转化为图论中的选择问题,并利用离散化的技巧进行求解。文章详细展示了从问题描述到最终解决方案的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 原题

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;
}

 

### BSU 计算机网络考试真题相关资源 在查找与BSU(假设为特定学校或课程)计算机网络相关的考试真题时,通常需要关注以下几个方面:学校的官方资源、过往学生的分享以及公开的学术资料。以下是一些可能的途径和内容: #### 1. 官方资源 许多大学会通过其学习管理系统(如Blackboard、Moodle等)提供过往的考试题目或复习材料[^1]。学生可以通过登录学校的在线平台获取这些资源。此外,部分学校可能会发布公开的考试样例,供学生参考。 #### 2. 学生社区 一些学生社区或论坛(例如Reddit、Stack Exchange、GitHub等)上可能存在由学生分享的考试真题或笔记。这些资源通常是非官方的,但可以作为学习的补充材料[^2]。搜索关键词时,可以尝试使用“BSU Computer Networks past exam papers”或类似表述。 #### 3. 开放教育资源 开放教育资源(OER)网站也可能包含与计算机网络相关的考试题目。例如,MIT OpenCourseWare 提供了多门计算机网络课程的讲义和习题[^3]。虽然这些题目可能不完全适用于BSU的具体课程,但它们可以帮助学生理解考试的核心知识点。 #### 4. 教材配套习题 许多计算机网络课程基于经典教材,如《Computer Networking: A Top-Down Approach》或《Computer Networks》(Andrew S. Tanenbaum著)。这些教材通常附带大量的练习题和解答,能够帮助学生熟悉考试题型[^4]。 #### 5. 示例代码与实验题 计算机网络课程中常见的考试题型包括但不限于: - **理论题**:OSI模型、TCP/IP协议栈、路由算法等。 - **计算题**:带宽延迟积、吞吐量计算等。 - **编程题**:Socket编程、HTTP请求解析等。 以下是一个简单的Socket编程示例,可能出现在考试中: ```python import socket # 创建一个TCP/IP套接字 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定套接字到地址和端口 server_address = ('localhost', 10000) sock.bind(server_address) # 监听传入连接 sock.listen(1) while True: # 等待连接 connection, client_address = sock.accept() try: # 接收数据 data = connection.recv(1024) print(f"Received {data}") if data: connection.sendall(data.upper()) finally: connection.close() ``` 此代码展示了如何创建一个简单的TCP服务器,并处理客户端发送的数据[^5]。 ### 注意事项 在查找和使用考试真题时,请确保遵守学校的学术诚信政策。未经授权使用或传播考试题目可能违反相关规定[^6]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值