SDUTOJ 树的同构

#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;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值