Codeforces Round 911 C. Anji‘s Binary Tree

原题:
C. Anji’s Binary Tree
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Keksic keeps getting left on seen by Anji. Through a mutual friend, he’s figured out that Anji really likes binary trees and decided to solve her problem in order to get her attention.

Anji has given Keksic a binary tree with n vertices. Vertex 1 is the root and does not have a parent. All other vertices have exactly one parent. Each vertex can have up to 2 children, a left child, and a right child. For each vertex, Anji tells Keksic index of both its left and its right child or tells him that they do not exist.

Additionally, each of the vertices has a letter s i s_i si on it, which is either ‘U’, ‘L’ or ‘R’.

Keksic begins his journey on the root, and in each move he does the following:

If the letter on his current vertex is 'U', he moves to its parent. If it doesn't exist, he does nothing.
If the letter on his current vertex is 'L', he moves to its left child. If it doesn't exist, he does nothing.
If the letter on his current vertex is 'R', he moves to its right child. If it doesn't exist, he does nothing. 

Before his journey, he can perform the following operations: choose any node, and replace the letter written on it with another one.

You are interested in the minimal number of operations he needs to do before his journey, such that when he starts his journey, he will reach a leaf at some point. A leaf is a vertex that has no children. It does not matter which leaf he reaches. Note that it does not matter whether he will stay in the leaf, he just needs to move to it. Additionally, note that it does not matter how many times he needs to move before reaching a leaf.

Help Keksic solve Anji’s tree so that he can win her heart, and make her come to Čačak.
Input

Each test consists of multiple test cases. The first line contains a single integer t
(1≤t≤ 5 ⋅ 1 0 4 5⋅10^4 5104) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n
(2≤n≤ 3 ⋅ 1 0 5 3⋅10^5 3105) — the number of vertices in a tree.

The second line of each test case contains a string s
of n characters — characters are written on the vertices. It is guaranteed that s

consists only of characters ‘U’, ‘L’, and ‘R’.

The i-th of the next n lines contains two integers li and ri (0≤li,ri≤n) — indices of left and right child of the vertex i. If l i l_i li=0, it means that vertex i does not have a left child. If r i r_i ri=0, it means that vertex i does not have a right child. It is guaranteed that this data describes a valid binary tree rooted at 1

.

It is guaranteed that the sum of n
over all test cases does not exceed 3 ⋅ 1 0 5 3⋅10^5 3105.
Output

For each test case, output a single integer — the minimal number of operations Keksic needs to do to reach a leaf.
Example
Input
Copy

5
3
LRU
2 3
0 0
0 0
3
ULR
3 2
0 0
0 0
2
LU
0 2
0 0
4
RULR
3 0
0 0
0 4
2 0
7
LLRRRLU
5 2
3 6
0 0
7 0
4 0
0 0
0 0

Output
Copy

0
1
1
3
1

Note

In the first test case, vertex 1
has 2 as its left child and 3 as its right child. Vertices 2 and 3 do not have children and are therefore leaves. As ‘L’ is written on vertex 1, Keksic will go to vertex 2, therefore he has to do no operations.

In the second test case, vertex 1
has 3 as its left child and 2 as its right child. Vertices 2 and 3 are leaves. As ‘U’ is written on vertex 1

, Keksic needs to change it to either ‘L’ or ‘R’ in order for him to reach a leaf.

In the third case, vertex 1
has only a right child, which is vertex 2. As ‘L’ is written on it, Keksic needs to change it to ‘R’, otherwise he would be stuck on vertex 1

.

In the fourth case, he can change 3
characters so that letters on the vertices are “LURL”, which makes him reach vertex 2

.

In the fifth case, there are 3
leaves, 3, 6 and 7. To reach either leaf 6 or leaf 7, he needs to change 2 characters. However, if he changes character on vertex 1 to ‘R’, he will reach leaf 3, therefore the answer is 1
在这里插入图片描述

中文:
给你一个二叉树,每个节点上要么有字母L要么有字母R要么有字母U,你从根节点出发,如果使遇到字母U就不动,遇到字母L就走左子树,遇到字母R就走右子树。现在让你走到一个叶子节点,走的过程中可以把节点上的字母替换成自己想要的,问你最少替换多少次能走到一个叶子节点。

代码:

    #include <bits/stdc++.h>using namespace std;const int maxn = 3e5 + 2;struct node {int lch, rch;};node ns[maxn];int dp[maxn];bool vis[maxn];char s[maxn];int t, n;int bfs() {queue<int> Q;memset(vis, false, sizeof(vis));memset(dp, 0, sizeof(dp));int ans = INT_MAX;Q.push(1);vis[1] = true;while(!Q.empty()) {int cur = Q.front();Q.pop();int lch = ns[cur].lch;int rch = ns[cur].rch;if (lch != 0) {if (s[cur] == 'L') {dp[lch] = dp[cur];}if (s[cur] == 'R' || s[cur] == 'U') {dp[lch] = dp[cur] + 1;}Q.push(lch);}if (rch != 0) {if (s[cur] == 'R') {dp[rch] = dp[cur];}if (s[cur] == 'L' || s[cur] == 'U') {dp[rch] = dp[cur] + 1;}Q.push(rch);}if (lch == 0 && rch == 0) { // 此时叶子节点ans = min (ans, dp[cur]);}}return ans;}int main() {ios::sync_with_stdio(false);cin >> t;while (t--) {cin >> n;cin >> s + 1;for (int i = 1; i <= n; i++) {cin >> ns[i].lch >> ns[i].rch;}int ans = bfs();cout << ans << endl;}return 0;}

解答:
简单的树形dp,要找从根节点到某一个叶子节点替换字符最少。广搜根节点到每个节点的距离,维护一个dp数组,记录当前节点所需要替换字符的最少次数。
d p [ c u r ] dp[cur] dp[cur] 表示 到达 编号为cur的节点时最少的替换字符数,如果cur节点不是叶子节点,且字母是L,那么 d p [ l e f t _ c h i l d ( c u r ) ] = d p [ c u r ] dp[left\_child(cur)] = dp[cur] dp[left_child(cur)]=dp[cur],left_child(cur)表示 cur节点左孩子的编号。否则,就要在dp[cur]上加一,因为要改变一次字母。

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

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

相关文章

浏览器刷新页面,缓存的处理方式,强制刷新

刷新页面的缓存处理的方式对比 地址栏回车/直接访问 URL保留强缓存&#xff0c;保留协商缓存&#xff0c;走正常请求流程点击浏览器刷新按钮忽略强缓存&#xff0c;保留协商缓存按f5【command r】忽略强缓存&#xff0c;保留协商缓存ctrl f5 【command shift r 】忽略强缓…

ECharts 图表简单示例,中国地图

目录 ECharts官网链接: [ECharts](https://echarts.apache.org/zh/index.html)在项目中引入 Apache ECharts柱状图折线图饼图仪表盘中国地图完整示例代码 ECharts官网链接: ECharts 在项目中引入 Apache ECharts <!DOCTYPE html> <html><head><meta char…

JavaWeb——后端AOP面向特定方法编程

七、AOP 1. 概述 AOP&#xff08;Aspect Oriented Programming&#xff09;&#xff1a;面向切面编程、面向方法编程&#xff0c;其实就是面向特定方法编程 场景&#xff1a; 案例部分功能运行较慢&#xff0c;定位执行耗时较长的业务方法&#xff0c;此时需要统计每个业务…

数据分析:从数据中获取有价值的洞察

在当今数据驱动的世界中&#xff0c;数据分析已经成为了企业和组织取得成功的关键因素。通过对数据的深入分析&#xff0c;我们可以揭示隐藏在数据背后的模式、趋势和关系&#xff0c;从而为决策提供有力的支持。本文将探讨数据分析的重要性&#xff0c;常用的分析方法、工具和…

C语言中的关键字与标识符详解

1. 关键字(keyword) 定义&#xff1a; 关键字是C语言中预定义的一组特殊字符串&#xff0c;它们具有特定的含义和用途。在程序编译过程中&#xff0c;编译器会根据这些关键字执行相应的操作。 特点&#xff1a; 所有C语言的关键字均采用小写字母形式。 举例&#xff1a; 例如…

2024年数学建模美赛能用chatGPT之类的AI吗?官方给了明确规定!

这两年chatGPT等大语言模型火了&#xff0c;能对话&#xff0c;自然也能回答数学建模方面的问题。 那美赛能不能用这些AI呢&#xff1f;2024年美赛官方对chatGPT等的使用做出了明确的规定&#xff08;其中的VI. Contest Instructions部分&#xff09;&#xff1a; https://ww…

JavaScript高级程序设计读书记录(六):定型数组,Map

1. 定型数组 定型数组&#xff08;typed array&#xff09;是 ECMAScript 新增的结构&#xff0c;目的是提升向原生库传输数据的效率。实际上&#xff0c;JavaScript 并没有“TypedArray”类型&#xff0c;它所指的其实是一种特殊的包含数值类型的数组。 1.1 历史 随着浏览器…

LaTex引用字体变色

使用下面这条语句进行修改。 ‘citecolor’改变参考文献颜色&#xff0c; ‘linkcolor’改变图标公式引用的颜色&#xff0c; ‘urlcolor’ 文本网站超链接颜色。 \usepackage[colorlinks,bookmarksopen,bookmarksnumbered,citecolorblue, linkcolorblue, urlcolorblue]{hyper…

数据库-简单表的操作And查看表的结构

查看表的结构 desc 表名;mysql> use study; Database changed mysql> create table Class(class_id int ,class_name varchar(128),class_teachar varchar(64)) ; Query OK, 0 rows affected (0.06 sec) mysql> show tables; ----------------- | Tables_in_study…

杨中科 ASP.NET Core前后端分离开发

一、 前后端分离 1、传统MVC开发模式: 前后端的代码被放到同一个项目中&#xff0c;前端人员负责编写页面的模板&#xff0c;而后端开发人员负责编写控制器和模型的代码并且“套模板”。 缺点: 互相依赖&#xff0c;耦合性强&#xff0c;责任划分不清。 2、主流的“前后端分离…

【openGauss服务器端工具的使用】

【openGauss服务器端工具的使用】 gs_checkperf openGauss 不仅提供了gs_checkperf工具来帮助用户了解openGauss的负载情况。 使用数据库安装用户登录服务器&#xff0c;执行如下命令进行查看数据库性能&#xff1a; 简要信息展示&#xff1a;[ommopengauss03 ~]$ gs_checkperf…

跨平台的传输协议@WebDav协议@windows系统配置WedDav服务器@局域网内的WebDav传输系统

文章目录 WebDav协议基本信息启用必要的windows功能启动站点管理器IIS站点根目录访问权限设置站点的功能设置端口通行防火墙IMME文件类型(文件后缀)其他设备登录和访问本机的WebDav服务站点 小结优点缺点 refs WebDav 协议基本信息 来自wikipedia:基于Web的分布式编写和版本控…

借名买房合同的效力

此前司法实践对借名买房合同效力的认定较为统一&#xff0c;即借名买房合同无效主要系因行为人恶意串通、通谋虚伪意思表示&#xff0c;或者借名购买的房屋为经济适用房等保障性住房。借名买房鲜少因悖俗而无效。2020年12月26日最高人民法院作出&#xff08;2020&#xff09;最…

协程池与新脚本语言

今天的主人公名为——Melang。 这是一款使用C语言开发的“新”的脚本语言&#xff0c;然而其已经默默问世了6年之久。 下面笔者就带你走进Melang world。 What is Melang Melang是一款协程并发脚本语言。它是一款解释型&#xff0c;而非编译型语言。 在Melang中&#xff…

Tmux 使用小记

本文参考自 阮一峰老师Tmux 使用教程[1] Tmux,不仅仅是分屏那么简单。。。 与tmux类似的工具是screen 会话管理 将窗口与会话"解绑" 对于没有图形界面只有shell的场景(如服务器)&#xff0c;尤其有用..这是其最核心解决的问题(窗口管理啥的只能算锦上添花的辅助功能)…

代码随想录算法训练营第20天 | 654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

目录 654.最大二叉树 &#x1f4a1;解题思路 &#x1f4bb;实现代码 617.合并二叉树 &#x1f4a1;解题思路 递归 &#x1f4bb;实现代码 700.二叉搜索树中的搜索 &#x1f4a1;解题思路 递归法 迭代法 &#x1f4bb;实现代码 98.验证二叉搜索树 &#x1f4a1;解题…

pod进阶版(1)

pod的相关知识 k8s的pad重启策略: Always deployment的yaml文件只能是Always pod的yaml三种模式都可以。 Onfailure:只有异常退出状态码非0才会重启。正常退出不重启。 Never&#xff1a;非正常退出和非正常退出都不重启。 容器的退出了pod才会重启。 pod可以有多个容器&…

实战:使用docker容器化服务

本文介绍使用docker安装mysql和redis&#xff0c;通过这两个的实战&#xff0c;了解一般的安装容器化服务的流程&#xff0c;体会服务容器化的好处 1.使用docker安装MySQL docker 拉取 mysql 镜像 docker pull mysql:5.7运行 mysql 镜像 docker run -p 3306:3306 --name mysql…

spring Security源码讲解-WebSecurityConfigurerAdapter

使用security我们最常见的代码&#xff1a; Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin().permitAll();http.authorizeRequests().antMatcher…

为什么我国的计算机教育那么差?

建议看看计算机科学速成课&#xff0c;一门很全面的计算机原理入门课程&#xff0c;短短10分钟可以把大学老师十几节课讲不清楚的东西讲清楚&#xff01;整个系列一共41个视频&#xff0c;B站上有中文字幕版。 每个视频都是一个特定的主题&#xff0c;例如软件工程、人工智能、…