#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
char c;
char l, r;
} ls[1100], lt[1100];
int ans= 0;
struct Bnode
{
char c;
struct Bnode *l, *r;
};
bool vis[1100];
int n, m;
void creat1(struct Bnode *&T,int t)
{
T = new Bnode;
T->c = ls[t].c;
T->l = NULL;
T->r = NULL;
if(ls[t].l != '-')
{
creat1(T->l,ls[t].l-'0');
}
if(ls[t].r != '-')
{
creat1(T->r,ls[t].r-'0');
}
}
void creat2(struct Bnode *&T,int t)
{
T = new Bnode;
T->c = lt[t].c;
T->l = NULL;
T->r = NULL;
if(lt[t].l != '-')
{
creat2(T->l,lt[t].l-'0');
}
if(lt[t].r != '-')
{
creat2(T->r,lt[t].r-'0');
}
}
void tongguo(Bnode *T1,Bnode *T2)
{
if(T1&&T2)
{
if(T1->c == T2->c)
ans++;
else
return ;
tongguo(T1->l,T2->l);
tongguo(T1->r,T2->r);
tongguo(T1->l,T2->r);
tongguo(T1->r,T2->l);
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
for(int i = 0; i < n; i++)
{
cin>>ls[i].c>>ls[i].l>>ls[i].r;
if(ls[i].l !='-')
{
vis[ls[i].l-'0'] = 1;
}
if(ls[i].r != '-')
{
vis[ls[i].r-'0'] = 1;
}
}
int head1;
for(int i = 0; i < n; i++)
{
if(!vis[i])
{
head1 = i;
break;
}
}
memset(vis,0,sizeof(vis));
scanf("%d",&m);
for(int i = 0; i < m; i++)
{
cin>>lt[i].c>>lt[i].l>>lt[i].r;
if(lt[i].l !='-')
{
vis[lt[i].l-'0'] = 1;
}
if(lt[i].r != '-')
{
vis[lt[i].r-'0'] = 1;
}
}
int head2;
for(int i = 0; i < m; i++)
{
if(!vis[i])
{
head2 = i;
break;
}
}
if( n != m)
{
printf("No\n");
continue;
}
if(n == 0)
{
printf("Yes\n");
continue;
}
struct Bnode *T1, *T2;
T1 = NULL;
T2 = NULL;
creat1(T1,head1);
creat2(T2,head2);
ans = 0;
tongguo(T1,T2);
if(ans == n)
{
printf("Yes\n");
}
else
printf("No\n");
}
return 0;
}
SDUTOJ 树的同构
最新推荐文章于 2018-08-19 17:10:03 发布