Codeforces Round 947 (Div. 1 + Div. 2) D. Paint the Tree 题解 DFS

Paint the Tree

题目描述

378QAQ has a tree with n n n vertices. Initially, all vertices are white.

There are two chess pieces called P A P_A PA and P B P_B PB on the tree. P A P_A PA and P B P_B PB are initially located on vertices a a a and b b b respectively. In one step, 378QAQ will do the following in order:

  1. Move P A P_A PA to a neighboring vertex. If the target vertex is white, this vertex will be painted red.
  2. Move P B P_B PB to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.

Initially, the vertex a a a is painted red. If a = b a=b a=b, the vertex a a a is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.

378QAQ wants to know the minimum number of steps to paint all vertices blue.

输入描述

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1\leq t\leq 10^4 1t104). The description of the test cases follows.

The first line of each test case contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\leq n\leq 2\cdot 10^5 1n2105).

The second line of each test case contains two integers a a a and b b b ( 1 ≤ a , b ≤ n 1\leq a,b\leq n 1a,bn).

Then n − 1 n - 1 n1 lines follow, each line contains two integers x i x_i xi and y i y_i yi ( 1 ≤ x i , y i ≤ n 1 \le x_i,y_i \le n 1xi,yin), indicating an edge between vertices x i x_i xi and y i y_i yi. It is guaranteed that these edges form a tree.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

输出描述

For each test case, output the minimum number of steps to paint all vertices blue.

样例输入#1

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

样例输出 #1

2
8
13

原题

CF——传送门
洛谷——传送门

思路

先让棋子A与棋子B汇合,再让它们用最少的步数一起走过所有结点。这样的步数一定是最少的,因为将顶点涂成蓝色的前提是两棋子都经过某一点,而让两棋子相互靠近直到第一个结点被涂成蓝色便是最优的。值得注意的是,棋子A,B之间的相对位置存在两种情况:同步和异步。同步代表着两棋子可以在同一时间位于同一个结点上,使该结点变为蓝色;而异步代表着某一步中A棋子位于结点X上,只有在下一步时B棋子才有可能走到结点X上,并使该结点变为蓝色。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;const int maxn = 2e5 + 6;
vector<int> e[maxn]; // e[i]存储以i为起点的所有有向边void add(int x, int y) // 加无向边
{e[x].push_back(y);e[y].push_back(x);
}int a, b;
deque<int> path; // 记录两颗棋子汇合的路径
int st = -1;     // 两颗棋子最快汇合后的点(也是dfs求max_dis的起点)
int step = 0;    // 汇合所需步数
int extra = 0;   // 两个棋子不可能位于同一个结点的情况(即两棋子异步),此时需要补充1步
void dfs1(int pos, int fa, int depth, int tag)
{if (st != -1) // 如果已找到答案,快速返回return;for (int i = 0; i < e[pos].size(); i++){int to = e[pos][i];if (to == tag){step = (depth + 1) / 2;if (path.size() % 2 == 0) // 两棋子异步(即两棋子初始时距离为奇数,也就是最短路径上的结点为偶数个的情况){extra = 1;          // 补充1步path.push_front(a); // 特殊照顾一下两棋子初始时距离为1的情况,因为这种情况下st为a,但是path路径在dfs时并没有保存a结点st = path[step];}else // 两棋子同步(即两棋子初始时距离为偶数,也就是最短路径上的结点为奇数个的情况){st = path[step - 1];}return;}if (to == fa)continue;path.push_back(to);dfs1(to, pos, depth + 1, tag);path.pop_back(); // 回溯}
}int max_dis = 0; // 起点st到其他点的所有距离中的最远距离
void dfs2(int pos, int fa, int depth)
{max_dis = max(max_dis, depth); // 维护最远距离即可for (int i = 0; i < e[pos].size(); i++){int to = e[pos][i];if (to == fa)continue;dfs2(to, pos, depth + 1);}
}void solve()
{int n;cin >> n;cin >> a >> b;int x, y;for (int i = 1; i <= n - 1; i++){cin >> x >> y;add(x, y);}if (a != b)dfs1(a, 0, 0, b);elsest = a;dfs2(st, 0, 0);// 2 * (n - 1) - max_dis是用到了一个结论:从树中的一个结点开始走,走过树上所有结点至少需要2 * (n - 1) - max_dis步cout << step + 2 * (n - 1) - max_dis + extra << '\n';// 初始化,为了处理下一组测试数据for (int i = 1; i <= n; i++){e[i].clear();}path.clear();st = -1;dis = 0;max_dis = 0;extra = 0;
}int main()
{ios::sync_with_stdio(0);cin.tie(0);int t;cin >> t;while (t--)solve();return 0;
}

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

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

相关文章

Spring Boot与MongoDB集成指南

1. 引言 在当今快速发展的软件开发领域&#xff0c;选择合适的技术栈对于构建高效、可扩展的应用程序至关重要。随着微服务架构和云原生应用的兴起&#xff0c;开发人员需要更灵活、更快速的解决方案来满足不断变化的业务需求。Spring Boot和MongoDB的结合正是这一需求的完美答…

运维开发.MySQL.范式与反范式化

运维开发 MySQL.三大范式 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/qq_28550263/artic…

jest测试

文章目录 测试testexpecttoThrow asyncPromiseAsync/Await 作用域一次性设置 beforeAll and afterAll重复设置 beforeEach 和 afterEachdescribe 块 mockcli配置覆盖率测试报告 测试 test test(name, () > {function} }expect () > {expect(期望value).toBe(匹配value…

python海龟绘图(你的作业说不定就在这里)

三角形 from turtle import * for i in range(3):forward(100)left(120)基本绘图​​​​​ 让海龟前进 100 步: forward(100)你应该会看到&#xff08;最可能的情况&#xff0c;是在你的显示器的一个新窗口中&#xff09;海龟画出一条线段&#xff0c;方向朝东。 改变海龟的…

spdlog日志库源码:线程池thread_pool

线程池 线程池本质上一组事先创建的子线程&#xff0c;用于并发完成特定任务的机制&#xff0c;避免运行过程中频繁创建、销毁线程&#xff0c;从而降低程序运行效率。通常&#xff0c;线程池主要涉及到以下几个方面问题&#xff1a; 如何创建线程池&#xff1f;线程池如何执…

Ubuntu22.04之解决:登录计算机的密码与登录密钥环里的密码不再匹配(二百三十四)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

微信网页版登录插件v1.1.1

说到如今的微信客户端&#xff0c;大家肯定会有很多提不完的意见或者建议。比如这几年体积越来越大&#xff0c;如果使用频率比较高&#xff0c;那占用空间就更离谱了。系统迷见过很多人电脑C盘空间爆满&#xff0c;都是由于微信PC版造成的。 而且&#xff0c;它还加了很多乱七…

15、Spring系统-AOP

ProxyFactory选择cglib或jdk动态代理原理 ProxyFactory在生成代理对象之前需要决定到底是使用JDK动态代理还是CGLIB技术&#xff1a; 代理对象创建过程 JdkDynamicAopProxy 在构造JdkDynamicAopProxy对象时&#xff0c;会先拿到被代理对象自己所实现的接口&#xff0c;并且…

VSCODE终端输出中文乱码 菱形问号?

问题现象 VSCODE终端输出中文乱码 菱形问号&#xff1f; 解决方法 方法一 设置系统环境变量 变量名&#xff1a;PYTHONIOENCODING 值&#xff1a;utf8 方法二 安装插件Code Runner插件在设置中搜索 code-runner.executorMap&#xff0c;再点击在setting.json中编辑&#x…

达梦数据库

达梦数据库 达梦Docker部署 达梦Docker部署 1、下载链接 https://pan.baidu.com/s/1RI3Lg0ppRhCgUsThjWV6zQ?pwdjc62 2、docker启动命令 docker run -d -p 5236:5236 \ --restartalways \ --name dm8 \ -e LD_LIBRARY_PATH/app/dm8/bin \ -e LENGTH_IN_CHAR1 \ -e CASE_SENS…

powershell 配合aria2实现简单的图片爬取

powershell 配合aria2实现简单的图片爬取 01 前言 现如今&#xff0c;提到爬虫&#xff0c;令人不得不提到Python&#xff0c;确实简单&#xff0c;也强大&#xff0c;到处都可以找到教程。故而今天换换口味&#xff0c;用powershell来实现&#xff0c;配合aria2的强大下载功…

目标检测 | R-CNN、Fast R-CNN与Faster R-CNN理论讲解

☀️教程&#xff1a;霹雳吧啦Wz ☀️链接&#xff1a;https://www.bilibili.com/video/BV1af4y1m7iL?p1&vd_sourcec7e390079ff3e10b79e23fb333bea49d 一、R-CNN R-CNN&#xff08;Region with CNN feature&#xff09;是由Ross Girshick在2014年提出的&#xff0c;在PAS…

leetcode 684.冗余连接

思路&#xff1a;并查集 这里的图比较像一种特殊的数据结构&#xff0c;其实也是图论的一种东西&#xff0c;就是基环树&#xff0c;但是这里并不是有向图&#xff0c;而是无向图&#xff0c;所以并不能用那种剪枝操作然后找基环。 看到连通量&#xff0c;我们应该能想到两种…

Qt 配置Eigen矩阵库 - 并简单测试

Qt 配置Eigen矩阵库 - 并简单测试 引言一、在Qt中配置Eigen二、低通Demo源码三、参考链接以及其他 引言 Eigen是一个开源的C模板库&#xff0c;提供了线性代数和矩阵运算的功能。它被设计为一个高性能、可扩展和易用的库&#xff0c;可以用于科学计算、机器学习和计算机图形学等…

HCIA-HarmonyOS Device Developer 课程大纲

一&#xff1a;OpenHarmony 介绍 - &#xff08; 3 课时&#xff09; - OpenHarmony 简介&#xff1b;OpenHarmony 设计理念&#xff1b;OpenHarmony 设计理念概述&#xff1b; - OpenHarmony 试图解决的问题&#xff1b;应用生态割裂问题&#xff1b;用户数据割裂问题&#…

服务器感染了. rmallox勒索病毒,如何确保数据文件完整恢复?

导言&#xff1a; 近年来&#xff0c;随着信息技术的飞速发展&#xff0c;网络安全问题日益凸显。其中&#xff0c;勒索病毒作为一种严重的网络威胁&#xff0c;对个人和企业数据造成了巨大的威胁。本文将重点介绍.rmallox勒索病毒的特点、传播途径以及应对策略&#xff0c;旨…

【LeetCode算法】第94题:二叉树的中序遍历

目录 一、题目描述 二、初次解答 三、官方解法 四、总结 一、题目描述 二、初次解答 1. 思路&#xff1a;二叉树的中序遍历。访问二叉树的左子树&#xff0c;再访问二叉树的根节点&#xff0c;最后访问二叉树的右叉树。 2. 代码&#xff1a; void order(struct TreeNode* r…

文心智能体平台丨创建你的四六级学习小助手

引言 在人工智能飞速发展的今天&#xff0c;我们迎来了文心智能体平台。该平台集成了最先进的人工智能技术&#xff0c;旨在为用户提供个性化、高效的学习辅助服务。今天&#xff0c;我们将向大家介绍如何利用文心智能体平台&#xff0c;创建一个专属于你的四六级学习小助手。…

Scikit-Learn随机森林回归

Scikit-Learn随机森林回归 1、随机森林1.1、集成学习1.2、Bagging方法1.3、随机森林算法1.4、随机森林的优缺点2、Scikit-Learn随机森林回归2.1、Scikit-Learn随机森林回归API2.2、随机森林回归实践(加州房价预测)1、随机森林 随机森林是一种由决策树构成的集成算法,它在大多…

ACM实训冲刺第二十二天

【碎碎念】今天学习ACM时间有点紧&#xff0c;只有30分钟&#xff0c;能写到哪算哪吧 人见人爱A^B&#xff08;数值运算与字符串处理&#xff09; 这段代码实现的是模幂运算&#xff0c;具体说是计算 A^Bmod1000 的值。模幂运算是一种在计算机科学和密码学中常用的算法&#xf…