composer 检查镜像_检查N元树中的镜像

composer 检查镜像

Problem statement:

问题陈述:

Given two n-ary trees, the task is to check if they are mirrors of each other or not.

给定两个n元树,任务是检查它们是否互为镜像。

Note: you may assume that root of both the given tree as 1.

注意:您可以假设两个给定树的根均为1。

Input:

输入:

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. The first line of each test case contains two space-separated values N and M denoting the no. of nodes and edges respectively. Then in the next line, two lines are 2*M space-separated values u, v denoting an edge from u to v for both trees.

输入的第一行包含一个整数T,表示测试用例的数量。 然后是T测试用例。 每个测试用例的第一行包含两个以空格分隔的值NM,表示编号。 分别为节点和边。 然后在下一行中,两行是2 * M个空格分隔的值uv表示两棵树从uv的边。

Output:

输出:

For each test case in a new line print "YES" if both the trees are mirrors of each other else print "NO".

对于新行中的每个测试用例,如果两棵树都是彼此的镜像,则打印“是”,否则打印“否”。

Examples:

例子:

INPUT:	
T=1
N=3,M=3
1 2 1 3 2 4
1 3 1 2 2 4
OUTPUT: 
YES
1                  1
/ \                / \
2   3              3   2
/                        \
4                         4
Since they are mirror of each other.
INPUT:
T=1
N=4,M=4
1 2 1 3 2 4 2 5
1 3 1 2 2 4 2 5
OUTPUT: 
NO
1                     1
/ \                   / \
2   3                 3   2
/ \                       / \
4   5                     4   5
Here, 
node 4 and 5 are not as in mirror so the tree is not mirror.

Solution Approach

解决方法

We will use stack and queue data structure since stack follow LIFO that is last in first out the way and queue follow FIFO first in first out pattern, is the trees are mirror then the top of the stack will be equal to the front of the queue and if they aren't equal it means that they are not the mirror of each other.

我们将使用堆栈和队列数据结构,因为堆栈遵循后进先出的方式,而队列遵循先进先出的先入先出模式,因为树是镜像的,因此堆栈的顶部等于队列的前端如果它们不相等,则意味着它们不是彼此的镜像。

We will follow this approach, taking each node at a time and checking its connected component in stack and queue. For checking whether each subtree in itself is a mirror or not we will use a boolean variable flag, initially, the flag is true and each time we check if the top of stack and queue front are equal or not, if not then simply return NO as the answer and after checking all nodes return true if all are valid nodes.

我们将采用这种方法,一次获取每个节点,并在堆栈和队列中检查其连接的组件。 为了检查每个子树本身是否是镜像,我们将使用布尔变量标志,该标志最初为true,并且每次我们检查栈顶和队列前部是否相等时(如果不相等),则简单地返回NO作为答案,并且在检查所有节点后,如果所有节点都是有效节点,则返回true。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cout << "Enter number of test cases: ";
cin >> t;
while (t--) {
ll n, m;
cout << "Enter number of nodes and edges: ";
cin >> n >> m;
// declare a vector of stacks of size (n+1)
// since index start from 0.
stack<int> v1[n + 1];
// declare a vector of queue of size (n+1)
// since index start from 0.
queue<int> v2[n + 1];
cout << "Enter tree 1: ";
for (ll i = 0; i < m; i++) {
ll x, y; //enter the elements of its.
cin >> x >> y;
v1[x].push(y);
}
cout << "Enter tree 2: ";
for (ll i = 0; i < m; i++) {
ll x, y;
cin >> x >> y; //enter elements of tree 2.
v2[x].push(y);
}
bool flag;
// iterate through each node.
for (int i = 1; i <= n; i++) {
// store nodes of tree 1 connected components in stack
stack<int>& s = v1[i];
// store nodes of tree 1 connected components in queue
queue<int>& q = v2[i];
// declare a boolean variable to check mirror
// property among the elements.
flag = true;
//compare the stack top to queue top.
while (!s.empty() and !q.empty()) {
// if not similar then break from the loop.
if (s.top() != q.front()) {
flag = false;
break;
}
s.pop();
q.pop();
}
// if not similar then break from the nodes loop
// since no further comparison is needed.
if (flag == false)
break;
}
// check if mirror or not.
cout << "Is Mirror: ";
if (flag == true)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}

Output:

输出:

Enter number of test cases: 3
Enter number of nodes and edges: 4 4 
Enter tree 1: 1 2 1 3 2 4 2 5
Enter tree 2: 1 2 1 3 2 5 2 4
Is Mirror: NO
Enter number of nodes and edges: 3 2
Enter tree 1: 1 2 1 3
Enter tree 2: 1 3 1 2
Is Mirror: YES
Enter number of nodes and edges: 3 3
Enter tree 1: 1 2 1 3 2 4
Enter tree 2: 1 3 1 2 2 4
Is Mirror: YES

  • The time complexity for the above approach in worst case: O(n*n)

    最坏情况下上述方法的时间复杂度: O(n * n)

  • Space complexity for the above approach in worst case : O(n)

    上述方法在最坏情况下的空间复杂度: O(n)



Also tagged in: Amazon, DE-Shaw, Hike, MakeMyTrip

也标记在: 亚马逊 , DE-Shaw , 远足 , MakeMyTrip

Problem source: https://practice.geeksforgeeks.org/problems/check-mirror-in-n-ary-tree/0

问题来源:https://practice.geeksforgeeks.org/problems/check-mirror-in-n-ary-tree/0

翻译自: https://www.includehelp.com/icp/check-mirror-in-n-ary-tree.aspx

composer 检查镜像

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

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

相关文章

浪潮各机型前面板指示灯含义

NF560D2 NF3020M2 NF5020M3 NF5140M3 NF5212H2 NF5220 NF5224L2 NF5240M3 NF5270M3 NF5280M2 NF5280M3 NF5540M3 NF5580M3 NF8420M3 NF8520 NF8560M2 说明&#xff1a;转浪潮官网。

python dll 混合_Python | 条线混合图

python dll 混合In some of the cases, we need to plot a bar-line hybrid plot. This plot helps in a better understanding of dynamics as well as the relative magnitude of each point in the plot. Bar-Line Hybrid Plots are mostly used for the representation of …

测试八 赛后感受

测试八 当我打开T1的时候&#xff0c;就没有往下看题目了&#xff0c;主要是发现T1就是之前做过&#xff0c;而且我也看过题解的题目&#xff0c;接着就开始钻研&#xff0c;当然&#xff0c;也没什么好钻研的&#xff0c;大概思路还是知道的&#xff0c;再写写数据就已经很清晰…

推荐五个免费的网络安全工具

导读&#xff1a; 在一个完美的世界里&#xff0c;信息安全从业人员有无限的安全预算去做排除故障和修复安全漏洞的工作。但是&#xff0c;正如你将要学到的那样&#xff0c;你不需要无限的预算取得到高质量的产品。这里有SearchSecurity.com网站专家Michael Cobb推荐的五个免费…

bios部署模式审核模式_BIOS的完整形式是什么?

bios部署模式审核模式BIOS&#xff1a;基本输入输出系统 (BIOS: Basic Input Output System) BIOS is an abbreviation of the Basic Input Output System. In the beginning, when you first set on your computer, the first software which starts run by the computer is &…

day04-装饰器

一、装饰器定义 1&#xff09;装饰器&#xff1a;本质是函数。 2&#xff09;功能&#xff1a;用来装饰其他函数&#xff0c;顾名思义就是&#xff0c;为其他的函数添加附件功能的。 二、原则 1&#xff09;不能修改被装饰函数的源代码 2&#xff09;不能修改被装饰函数的调用方…

c 语言bool 类型数据_C ++中的bool数据类型

c 语言bool 类型数据In C programming language, to deal with the Boolean values – C added the feature of the bool data type. A bool variable stores either true (1) or false (0) values. 在C 编程语言中&#xff0c;为了处理布尔值– C 添加了bool数据类型的功能 。…

C ++中的std :: binary_search()

binary_search()作为STL函数 (binary_search() as a STL function) Syntax: 句法&#xff1a; bool binary_search (ForwardIterator first, ForwardIterator last, const T& value);Where, 哪里&#xff0c; ForwardIterator first iterator to start of the range For…

HNUSTOJ-1437 无题

1437: 无题 时间限制: 1 Sec 内存限制: 128 MB提交: 268 解决: 45[提交][状态][讨论版]题目描述 tc在玩一个很无聊的游戏&#xff1a;每一次电脑都会给一个长度不超过10^5的字符串&#xff0c;tc每次都从第一个字符开始&#xff0c;如果找到两个相邻相一样的字符&#xff0c;…

凯撒密码pythin密码_凯撒密码术

凯撒密码pythin密码Caesar cipher is one of the well-known techniques used for encrypting the data. Although not widely used due to its simplicity and being more prone to be cracked by any outsider, still this cipher holds much value as it is amongst the fir…

MultiQC使用指导

MultiQC使用指导 官网资料文献&#xff1a;MultiQC --- summarize analysis results for multiple tools and samples in a single report参考资料一&#xff1a; 整合 fastq 质控结果的工具 简介 MultiQC 是一个基于Python的模块, 用于整合其它软件的报告结果, 目前支持以下软…

FYFG的完整形式是什么?

FYFG&#xff1a;对您的未来指导 (FYFG: For Your Future Guidance) FYFG is an abbreviation of "For Your Future Guidance". FYFG是“ For your Future Guidance”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also wr…

WorkerMan 入门学习之(二)基础教程-Connection类的使用

一、TcpConnection类 的使用 1、简单的TCP测试 Server.php <?php require_once __DIR__./Workerman/Autoloader.php; use Workerman\Worker; $worker new Worker(websocket://0.0.0.0:80);// 连接回调 $worker->onConnect function ($connection){echo "connecti…

kotlin获取属性_Kotlin程序获取系统名称

kotlin获取属性The task is to get the system name. 任务是获取系统名称。 package com.includehelpimport java.net.InetAddress/*** Function for System Name*/fun getSystemName(): String? {return try {InetAddress.getLocalHost().hostName} catch (E: Exception) {S…

71文件类型

1.kit类型 标准的SeaJs模块文件类型&#xff0c;直接对外暴露方法。 2.units类型 依赖pageJob&#xff0c;对外暴露一个名字&#xff0c;pageJob依赖暴露的名字对模块进行初始化&#xff0c;在pageJob内部逻辑自动执行init方法&#xff1b; 由于没有对外暴露方法&#xff0c;只…

ruby 生成哈希值_哈希 Ruby中的运算符

ruby 生成哈希值In the last article, we have seen how we can carry out a comparison between two hash objects with the help of "" operator? "" method is a public instance method defined in Ruby’s library. 在上一篇文章中&#xff0c;我们看…

七牛大数据平台的演进与大数据分析实践--转

原文地址&#xff1a;http://www.infoq.com/cn/articles/qiniu-big-data-platform-evolution-and-analysis?utm_sourceinfoq&utm_mediumpopular_widget&utm_campaignpopular_content_list&utm_contenthomepage 七牛大数据平台的演进与大数据分析实践 (点击放大图像…

最大化切割段

Description: 描述&#xff1a; In this article we are going to review classic dynamic programing problem which has been featured in interview rounds of amazon. 在本文中&#xff0c;我们将回顾在亚马逊的采访轮次中已经介绍的经典动态编程问题。 Problem statemen…

响应数据传出(springMVC)

1. SpringMVC 输出模型数据概述 提供了以下几种途径输出模型数据&#xff1a; ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 Map 及 Model: 入参为 org.springframework.ui.Model、 org.springframework.ui.ModelMap 或 java.uti…

python 字母顺序计数_计数并说出顺序

python 字母顺序计数Problem statement: 问题陈述&#xff1a; The count-and-say sequence is the sequence of integers with the first five terms as following: 计数序列是具有前五个项的整数序列&#xff0c;如下所示&#xff1a; 1 1个 11 11 21 21 1211 1211 111221 …