SDUTOJ 树的同构

SDUTOJ 树的同构#include#include#include#include#include#includeusingnamespacestd;structnode{charc;charl,r;}ls[1100],lt[1100];intans=0;structBnode{charc;

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/35633.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注