Atcoder Beginner Contest 309——D-F讲解

news2024/9/24 3:21:58

前言

由于最近期末考试,所以之前几场都没打,给大家带了不便,非常抱歉。

这个暑假,我将会持续更新,并给大家带了更好理解的题解!希望大家多多支持。

由于, A ∼ C A\sim C AC 题比较简单,所以就不写了,如果大家有不会的,可以私信问我。


D - Add One Edge

1. Description

Problem Statement

We have an undirected graph with ( N 1 + N 2 ) (N_1+N_2) (N1+N2) vertices and M M M edges. For i = 1 , 2 , … , M i=1,2,\ldots,M i=1,2,,M, the i i i-th edge connects vertex a i a_i ai and vertex b i b_i bi.

The following properties are guaranteed:
Vertex u u u and vertex v v v are connected, for all integers u u u and v v v with 1 ≤ u , v ≤ N 1 1 \leq u,v \leq N_1 1u,vN1.
Vertex u u u and vertex v v v are connected, for all integers u u u and v v v with N 1 + 1 ≤ u , v ≤ N 1 + N 2 N_1+1 \leq u,v \leq N_1+N_2 N1+1u,vN1+N2.
Vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1+N2) are disconnected.
Consider performing the following operation exactly once:
choose an integer u u u with 1 ≤ u ≤ N 1 1 \leq u \leq N_1 1uN1 and an integer v v v with N 1 + 1 ≤ v ≤ N 1 + N 2 N_1+1 \leq v \leq N_1+N_2 N1+1vN1+N2, and add an edge connecting vertex u u u and vertex v v v.
We can show that vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1+N2) are always connected in the resulting graph; so let d d d be the minimum length (number of edges) of a path between vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1+N2).
Find the maximum possible d d d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices $u$ and $v$ of an undirected graph are said to be connected if and only if there is a path between vertex $u$ and vertex $v$. ### Constraints

1 ≤ N 1 , N 2 ≤ 1.5 × 1 0 5 1 \leq N_1,N_2 \leq 1.5 \times 10^5 1N1,N21.5×105
0 ≤ M ≤ 3 × 1 0 5 0 \leq M \leq 3 \times 10^5 0M3×105
1 ≤ a i ≤ b i ≤ N 1 + N 2 1 \leq a_i \leq b_i \leq N_1+N_2 1aibiN1+N2
( a i , b i ) ≠ ( a j , b j ) (a_i,b_i) \neq (a_j,b_j) (ai,bi)=(aj,bj) if i ≠ j i \neq j i=j.
Vertex u u u and vertex v v v are connected for all integers u u u and v v v such that 1 ≤ u , v ≤ N 1 1 \leq u,v \leq N_1 1u,vN1.
Vertex u u u and vertex v v v are connected for all integers u u u and v v v such that N 1 + 1 ≤ u , v ≤ N 1 + N 2 N_1+1 \leq u,v \leq N_1+N_2 N1+1u,vN1+N2.
Vertex 1 1 1 and vertex ( N 1 + N 2 ) (N_1+N_2) (N1+N2) are disconnected.
All input values are integers.

Input

The input is given from Standard Input in the following format:

N 1 N_1 N1 N 2 N_2 N2 M M M
a 1 a_1 a1 b 1 b_1 b1
⋮ \vdots
a M a_M aM b M b_M bM

Output

Print the answer.

Sample Input 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

Sample Output 1

5

If we set u = 2 u=2 u=2 and v = 5 v=5 v=5, the operation yields d = 5 d=5 d=5, which is the maximum possible.

Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4

2. Solution


根据这个图以及题目大意,可以看出题目中所给的无向图一定是分成了两个连通块,并且起点 1 1 1,终点 N 1 + N 2 N_1+N_2 N1+N2一定不再同一个连通块内部

(1)对于连通块 1 1 1,我们只需计算起点 1 1 1 到连通块内各个顶点的最短距离,取出最长的距离 r e s 1 res_1 res1
(2)对于连通块 2 2 2,我们只需计算终点 N 1 + N 2 N_1+N_2 N1+N2 到连通块内各个顶点的最短距离,取出最长的距离 r e s 2 res_2 res2

最后的答案就是 r e s 1 + r e s 2 + 1 res_1+res_2+1 res1+res2+1,因为我们中间还要加一条边,所以距离还会多 1 1 1


3.Code

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

const int N = 3e5 +10;

int n1, n2, m;
int u, v;
vector<int> g[N];
int dist[N], st[N];

void bfs(int start) //模版(不多说了,不会的可以问我)
{
	memset(dist, 0x3f, sizeof dist);
	memset(st, 0, sizeof st);
	queue<int> q;
	q.push(start);
	dist[start] = 0;
	
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		
		if (st[t]) continue;
		st[t] = 1;
		
		for (auto c : g[t])
			q.push(c), dist[c] = min(dist[c], dist[t] + 1);
	}
}

int main()
{
	cin >> n1 >> n2 >> m;
	
	while (m --)
	{
		cin >> u >> v;
		g[u].push_back(v), g[v].push_back(u);
	}
	
	bfs(1); //求连通块1内点1到其他点的最短距离
	int res1 = 0;
	for (int i = 1; i <= n1; i ++)
		res1 = max(res1, dist[i]);//取出到所有点中最短距离中最长的
		
	bfs(n1 + n2);//求连通块2内点N1+N2到其他点的最短距离
	int res2 = 0;
	for (int i = n1 + 1; i <= n1 + n2; i ++)
		res2 = max(res2, dist[i]); //取出到所有点中最短距离中最长的
		
	cout << res1 + res2 + 1 << endl;
	
	return 0;
}

4. Time Complexity: O ( N 1 + N 2 ) O(N_1+N_2) O(N1+N2)


E - Family and Insurance

1.Description

Problem Statement

There is a family consisting of person 1 1 1, person 2 2 2, … \ldots , and person N N N. For i ≥ 2 i\geq 2 i2, person i i i’s parent is person p i p_i pi.
They bought insurance M M M times. For i = 1 , 2 , … , M i=1,2,\ldots,M i=1,2,,M, person x i x_i xi bought the i i i-th insurance, which covers that person and their descendants in the next y i y_i yi generations.
How many people are covered by at least one insurance?

Constraints

2 ≤ N ≤ 3 × 1 0 5 2 \leq N \leq 3 \times 10^5 2N3×105
1 ≤ M ≤ 3 × 1 0 5 1 \leq M \leq 3 \times 10^5 1M3×105
1 ≤ p i ≤ i − 1 1 \leq p_i \leq i-1 1pii1
1 ≤ x i ≤ N 1 \leq x_i \leq N 1xiN
1 ≤ y i ≤ 3 × 1 0 5 1 \leq y_i \leq 3 \times 10^5 1yi3×105
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M
p 2 p_2 p2 … \ldots p N p_N pN
x 1 x_1 x1 y 1 y_1 y1
⋮ \vdots
x M x_M xM y M y_M yM

Output

Print the answer.

Sample Input 1

7 3
1 2 1 3 3 3
1 1
1 2
4 3

Sample Output 1

4

The 1 1 1-st insurance covers people 1 1 1, 2 2 2, and 4 4 4, because person 1 1 1’s 1 1 1-st generation descendants are people 2 2 2 and 4 4 4.

The 2 2 2-nd insurance covers people 1 1 1, 2 2 2, 3 3 3, and 4 4 4, because person 1 1 1’s 1 1 1-st generation descendants are people 2 2 2 and 4 4 4, and person 1 1 1’s 2 2 2-nd generation descendant is person 3 3 3.

The 3 3 3-rd insurance covers person 4 4 4, because person 4 4 4 has no 1 1 1-st, 2 2 2-nd, or 3 3 3-rd descendants.
Therefore, four people, people 1 1 1, 2 2 2, 3 3 3, and 4 4 4, are covered by at least one insurance.

Sample Input 2

10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1

Sample Output 2

10

2.Solution

在这里插入图片描述
这是样例中所对应的树。

这道题可以运用动态规划
f i f_i fi 表示 i i i 号节点的保险还能延续到多少代。

那么转移就非常简单了:
f i = max ⁡ ( f i , f p i − 1 ) f_i = \max (f_i, f_{p_i} - 1) fi=max(fi,fpi1) (注: p i p_i pi 即题目中所说的含义)。

之后,就进行对每一个点判断:
如果 f i ≥ 0 f_i\ge0 fi0,那么 r e s + + res++ res++

最后的答案就是 r e s res res。(不太明白的话,可以看看代码~~~)


3.Code

#include <iostream>
#include <cstring>

using namespace std;

const int N = 3e5 + 10;

int n, m;
int f[N], p[N];

int main()
{
	cin >> n >> m;
	
	p[1] = 1;
	for (int i = 2; i <= n; i ++)
		cin >> p[i];
	
	memset(f, -1, sizeof f);
		
	while (m --)
	{
		int x, y;
		
		cin >> x >> y;
		
		f[x] = max(f[x], y); //进行最初的赋值,表示x节点可以延续y代
	}
	
	for (int i = 1; i <= n; i ++)
		f[i] = max(f[i], f[p[i]] - 1);
		
	int res = 0;
	for (int i = 1; i <= n; i ++)
		if (f[i] >= 0)
			res ++;
			
	cout << res << endl;
}

4.Time Complexity: O ( N ) O(N) O(N)


F - Box in Box

1. Description

Problem Statement

There are N N N boxes. The i i i-th box has a shape of a rectangular cuboid whose height, width, and depth are h i , w i h_i,w_i hi,wi, and d i d_i di, respectively.
Determine if there are two boxes such that one’s height, width, and depth are strictly greater than those of the other after rotating them if necessary.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2N2×105
1 ≤ h i , w i , d i ≤ 1 0 9 1 \leq h_i,w_i,d_i \leq 10^9 1hi,wi,di109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
h 1 h_1 h1 w 1 w_1 w1 d 1 d_1 d1
⋮ \vdots
h N h_N hN w N w_N wN d N d_N dN

Output

Print Yes if there are two boxes such that one’s height, width, and depth are strictly greater than those of the other after rotating them if necessary; print No otherwise.

Sample Input 1

3
19 8 22
10 24 12
15 25 11

Sample Output 1

Yes

If you rotate the 2 2 2-nd box to swap its height and depth, the 3 3 3-rd box will have greater height, depth, and width.

Sample Input 2

3
19 8 22
10 25 12
15 24 11

Sample Output 2

No

Sample Input 3

2
1 1 2
1 2 2

Sample Output 3

No

2. Solution

都放在视频里拉~~~,不懂得话可以再来问我。

ABC 309 F题

线段树没有学的同学可以点这里!


3. Code

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2e5 + 10;

int n;
struct Node1
{
	int h, w, d;
	void change() //保证h, w, d升序
	{
		if (h > w) swap(h, w);
		if (w > d) swap(w, d);
		if (h > w) swap(h, w);
	}
	bool operator< (const Node1 &t)const //重载小于号
	{
		if (t.h == h)
			return w > t.w;
		return h < t.h;
	}
}cube[N];
vector<int> discrete;
struct Node2
{
	int l, r;
	int mn;
}seg[8 * N]; //*8的原因是我们后面插入的时候为了离散化的方便,将Wi - 1也做成了节点

int find(int x) //离散化
{
	return lower_bound(discrete.begin(), discrete.end(), x) - discrete.begin() + 1;
}

void pushup(int u)
{
	seg[u].mn = min(seg[u << 1].mn, seg[u << 1 | 1].mn);
}

void build(int u, int l, int r) //建树
{
	if (l == r)
		seg[u] = {l, l, 0x3f3f3f3f};
	else
	{
		seg[u] = {l, r};
		int mid = l + r >> 1;
		build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
		pushup(u);
	}
}

void modify(int u, int x, int d) //单点修改
{
	if (seg[u].l == x && seg[u].r == x)
		seg[u].mn = min(seg[u].mn, d);
	else
	{
		int mid = seg[u].l + seg[u].r >> 1;
		if (x <= mid) modify(u << 1, x, d);
		else modify(u << 1 | 1, x, d);
		pushup(u);
	}
}

int query(int u, int l, int r) //区间查询
{
	if (seg[u].l >= l && seg[u].r <= r)
		return seg[u].mn;
		
	int mid = seg[u].l + seg[u].r >> 1, res = 0x3f3f3f3f;
	if (l <= mid) res = query(u << 1, l, r);
	if (r > mid) res = min(res, query(u << 1 | 1, l, r));
	
	return res;	
}

int main()
{
	cin >> n;
	
	for (int i = 1; i <= n; i ++)
		cin >> cube[i].h >> cube[i].w >> cube[i].d, cube[i].change();
		
	sort(cube + 1, cube + 1 + n);
	
	for (int i = 1; i <= n; i ++) discrete.push_back(cube[i].w), discrete.push_back(cube[i].w - 1);
	sort(discrete.begin(), discrete.end());
	discrete.erase(unique(discrete.begin(), discrete.end()), discrete.end());
	
	build(1, 0, discrete.size());
	
	for (int i = 1; i <= n; i ++)
	{
		int ans = query(1, 0, find(cube[i].w - 1));
		
		if (ans < cube[i].d)
		{
			cout << "Yes" << endl;
			return 0;
		}
		
		modify(1, find(cube[i].w), cube[i].d);
	}
	
	cout << "No" << endl;
}
东望苍苍,西望茫茫,叶落花凋泪满裳

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/735232.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

现代C++新特性 扩展的聚合类型(C++17 C++20)(PC浏览效果更佳)

文字版PDF文档链接&#xff1a;现代C新特性(文字版)-C文档类资源-CSDN下载 1.聚合类型的新定义 C17标准对聚合类型的定义做出了大幅修改&#xff0c;即从基类公开且非虚继承的类也可能是一个聚合。同时聚合类型还需要满足常规条件。 1&#xff0e;没有用户提供的构造函数。…

用C语言写一个压缩文件的程序

本篇目录 数据在计算机中的表现形式huffman 编码将文件的二进制每4位划分&#xff0c;统计其值在文件中出现的次数构建二叉树搜索二叉树的叶子节点运行并输出新的编码文件写入部分写入文件首部写入数据部分压缩运行调试解压缩部分解压缩测试为可执行文件配置环境变量总结完整代…

23数字图像置乱技术(matlab程序)

1.简述 一、引言 所谓“置乱”&#xff0c;就是将图像的信息次序打乱&#xff0c;a像素移动到b像素位置上&#xff0c;b像素移动到c像素位置上&#xff0c;……&#xff0c;使其变换成杂乱无章难以辨认的图片。数字图像置乱技术属于加密技术&#xff0c;是指发送发借助数学或者…

Python实现PSO粒子群优化算法优化Catboost分类模型(CatBoostClassifier算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 PSO是粒子群优化算法&#xff08;Particle Swarm Optimization&#xff09;的英文缩写&#xff0c;是一…

《低代码指南》——轻流5.0发布,无代码引擎矩阵全面升级

7月6日,由轻流主办「无代码无边界 202376Day|轻流无代码探索者大会」于上海顺利举行。轻流也在会上重磅发布了更加开放、灵活、低门槛的轻流5.0,和全面升级的专有轻流。 轻流5.0全面迭代升级了轻流的无代码引擎矩阵(表单引擎、流程引擎、报表引擎、门户引擎、数据引擎)。…

软件测试项目实战,电商项目测试实例 - 业务测试(重点)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 支付功能怎么测试…

pytest自动化测试实战之执行参数

上一篇介绍了如何运行pytest代码&#xff0c;以及用例的一些执行规则&#xff0c;执行用例发现我们中间print输出的内容&#xff0c;结果没有给我们展示出来&#xff0c;那是因为pytest执行时&#xff0c;后面需要带上一些参数。 参数内容 我们可以在cmd中通过输入 pytest -h…

域名捡漏的好方法,希望能够帮到你:域霸扫描器 V0.44 绿色免费版,供大家学习研究参考

高速扫描域名的工具&#xff0c;一均程序每小时五万条。 扫描域名是否注册&#xff0c;注册商是谁&#xff0c;域名的注册日期与过期日期。 供大家学习研究参考&#xff01; 下载&#xff1a;https://download.csdn.net/download/weixin_43097956/88025564

【SpringBoot——Error记录】

IDEA正常安装后&#xff0c;运行按钮为灰色解决方法尝试 解决方法一&#xff08;本人适用&#xff09;解决方法二 解决方法一&#xff08;本人适用&#xff09; 检查创建项目时JDK是否添加&#xff0c;版本是否正确。 解决方法二 点击左下角的Structure 参考链接&#xff1…

回归预测 | MATLAB实现WOA-CNN-LSTM鲸鱼算法优化卷积长短期记忆神经网络多输入单输出回归预测

回归预测 | MATLAB实现WOA-CNN-LSTM鲸鱼算法优化卷积长短期记忆神经网络多输入单输出回归预测 目录 回归预测 | MATLAB实现WOA-CNN-LSTM鲸鱼算法优化卷积长短期记忆神经网络多输入单输出回归预测预测效果基本介绍模型描述程序设计学习总结参考资料 预测效果 基本介绍 回归预测 …

node中的数据持久化之mongoDB

一、什么是mongoDB MongoDB是一种开源的非关系型数据库&#xff0c;正如它的名字所表示的&#xff0c;MongoDB支持的数据结构非常松散&#xff0c;是一种以bson格式&#xff08;一种json的存储形式&#xff09;的文档存储方式为主&#xff0c;支持的数据结构类型更加丰富的NoS…

mysql多表查询练习题

创建表及插入数据 create table if not exists dept3( deptno varchar(20) primary key , -- 部门号 name varchar(20) -- 部门名字 ); -- 创建员工表 create table if not exists emp3( eid varchar(20) primary key , -- 员工编号 ename varchar(20), -- 员工名字 age int, -…

换零钱——最小钱币张数(贪心算法)

贪心算法&#xff1a;根据给定钱币面值列表&#xff0c;输出给定钱币金额的最小张数。 (本笔记适合学完python基本数据结构&#xff0c;初通 Python 的 coder 翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff1a;https://www.python.org/ Free&#xff1a;大咖免费“圣…

CS EXE上线主机+文件下载上传键盘记录

前言 书接上文&#xff0c;CobaltStrike_1_部署教程及CS制作office宏文档钓鱼教程&#xff0c;该篇介绍【使用CS生成对应exe木马&#xff0c;上线主机&#xff1b;对上线主机进行&#xff0c;文件下载&#xff0c;文件上传&#xff0c;键盘记录】。 PS&#xff1a;文章仅供学习…

unseping

代码审计 <?php highlight_file(__FILE__);class ease{private $method;private $args;function __construct($method, $args) {$this->method $method;$this->args $args;}function __destruct(){if (in_array($this->method, array("ping"))) {call…

关于 colab Tutorial的介绍

&#xff08;一&#xff09;常用的快捷键 (二) 网上环境的配置 按照官网上所给的提示一步一步操作即可 注意&#xff1a;此平台需要科学的上网

word因导入mathtype不能使用复制粘贴快捷键的解决方法

1. 我们安装完mathtype后&#xff0c;有时会有两个mathtype显示&#xff0c;其中一个是属于office文件夹下的&#xff0c;另一个是win文件夹下的。如图&#xff1a; 2. 如果word中的复制粘贴快捷键&#xff08;CTRLC和CTRLV&#xff09;不能用&#xff0c;通常是因为office路径…

Arduino STM32F103C8+ST7735 1.8‘‘3D矢量图形demo

Arduino STM32F103C8ST7735 1.8’3D矢量图形demo &#x1f4cc;开源项目地址&#xff1a;https://github.com/cbm80amiga/ST7735_3d_filled_vector&#x1f527;所需库&#xff1a;https://github.com/cbm80amiga/Arduino_ST7735_STM&#x1f516;本开源工程基于Arduino开发平台…

JavaWeb JSP基础语法和指令

1. JSP语法 JSP是Java技术的一种应用&#xff0c;对Java所有的语法都支持&#xff0c;除此之外&#xff0c;还有一些扩充的语法。 1&#xff09;输出变量 <% new java.util.Date()%> <% name %> 2) 执行java代码 <% int a 0, b 1, t; for(int i0;i<10;i)…

day61_SSM+自定义注解实现日志记录

SSM自定义注解AOP实现日志记录 1 需求 工作中,经常会遇到记录日志的动作,以前是使用日志框架来实现,现在可以使用注解来实现,使用起来更方便,随用随加~ 今天我们演示在SSM的基础上,对普通的方法加上自定义注解,注解中写上该方法的日志信息,然后将日志信息记录到数据库中. 编…