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,一经查实,立即删除!

相关文章

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;此时需要统计每个业务…

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…

杨中科 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的分布式编写和版本控…

协程池与新脚本语言

今天的主人公名为——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可以有多个容器&…

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;例如软件工程、人工智能、…

顺序表实现(下)(C语言)

几道相关例题,帮助大家更好理解顺序表. 文章目录 前言 一、顺序表二、创建顺序表并初始化三.删除非递减顺序表L中的重复元素四.在非递减顺序表中删除[s,t]之间的元素五.设计算法逆置顺序表L,并将序列L循环左移六.顺序表A和B的元素个数分别为m,n.A表升序排序,B表降序排序,两表中…

AI变现项目:刚做五天收益突破单日破50+,干货经验谈

今日是我单号操作的第五天。 打开今日头条&#xff0c;发现收益破新高了。 我这是一个号操作&#xff0c;10个号&#xff0c;20个号呢&#xff1f; 下面主要说说我的操作经验。 先确定领域 我是做的情感故事领域。 为什么做这个领域&#xff1f;(简单&#xff0c;原创度高…

家用洗地机哪款好用?洗地机品牌排行榜推荐

在如今的日常生活中&#xff0c;家用洗地机已经成为了家庭清洁中不可或缺的工具。然而&#xff0c;市面上各种不同品牌型号的洗地机让人眼花缭乱&#xff0c;让人难以选择。那么&#xff0c;家用洗地机现在买什么牌子质量好呢?为了解答这个问题&#xff0c;笔者选了几款品牌质…

120°AGV|RGV小车激光障碍物传感器|避障雷达DE系列安装与连线方法

120AGV|RGV小车激光障碍物传感器|避障雷达DE系列包含DE-4211、DE-4611、DE-4311、DE-4511等型号&#xff0c;根据激光飞行时间&#xff08;TOF&#xff09;测量原理运行的&#xff0c;利用激光光束对周围进行 120 半径 4m&#xff08;90%反射率&#xff09;扫描&#xff0c;获得…

鸿蒙开发解决agconnect sdk not initialized. please call initialize()

文章目录 项目场景:问题描述原因分析:解决方案:总结:项目场景: 鸿蒙开发报错: agconnect sdk not initialized. please call initialize() 问题描述 报错内容为: 10-25 11:41:01.152 6076-16676 E A0c0d0/JSApp: app Log: 数据查询失败: {“code”:1100001,“messag…