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.pswp.cn/news/540732.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540732.shtml
英文地址,請注明出處:http://en.pswp.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 …