-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q 501 Mirror Tree.c
75 lines (60 loc) · 1.54 KB
/
Q 501 Mirror Tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
When a tree is reflected across an imaginary straight vertical line right or left of the entire tree, the resultant tree hence obtained is the mirror tree of the original tree.
Your task is to write a program to check if two given trees are mirrors of each other.
Input
First line consists of the number of nodes N.
Next N-1 lines each describes the first tree. Each line has two integers U and V and a letter 'R' or 'L', denoting that V is the left or the right child of U.
Next N-1 lines each describes the second tree. Each line has two integers U and V and a letter 'R' or 'L', denoting that V is the left or the right child of U.
Output
Print "yes" if the two trees are mirrors of each other and "no" otherwise
Sample Input 0
3
1 2 R
1 3 L
1 2 L
1 3 R
Sample Output 0
yes
Explanation 0
The first tree is
1
/ \
3 2
The second tree is
1
/ \
2 3
Clearly, both the trees are mirrors of each other.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int t1[1000][2],t2[1000][2];
int main() {
int n; scanf("%d",&n);
int x,y; char c;
for(int i=0;i<n-1;i++)
{
scanf("%d%d %c",&x,&y,&c);
if(c=='L')t1[x][0]=y;
else t1[x][1]=y;
}
for(int i=0;i<n-1;i++)
{
scanf("%d%d %c",&x,&y,&c);
if(c=='L')t2[x][0]=y;
else t2[x][1]=y;
}
int f=0;
for(int i=1;i<=n;i++)
{
if(t1[i][0]!=t2[i][1] && t1[i][1]!=t2[i][0])
{
f=1;break;
}
}
if(f==0)printf("yes");
else printf("no");
return 0;
}