PTA树的同构c++版——山东科技大学

点击此处查看作者参考的大佬代码

在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct A
{
    char ch;
    int l,r;
    bool root;
}a[15],b[15];
int n1,n2;
char ch;
char l,r;
bool judge(int root1,int root2)
{
    if(root1==-1&&root2==-1)
        return true;
    if((root1==-1&&root2!=-1)||(root1!=-1&&root2==-1))
        return false;
    if(a[root1].ch!=b[root2].ch)
        return false;
    if(judge(a[root1].l,b[root2].l)&&judge(a[root1].r,b[root2].r))
        return true;
    if(judge(a[root1].l,b[root2].r)&&judge(a[root1].r,b[root2].l))
        return true;
    return false;
}
void print(int r)
{
    if(r==-1)
        return;
    cout<<b[r].ch<<' ';
    print(b[r].l);
    print(b[r].r);
}
void build()
{
    cin>>n1;
    for(int i=0;i<n1;i++)
    {
        cin>>ch>>l>>r;
        a[i].ch=ch;
        if(l=='-')
            a[i].l=-1;
        else
        {
            a[i].l=l-'0';
            a[a[i].l].root=true;//节点不是树根
                                //正常来说应该是false,但因为初始化是true
                                //所以用true来否定,下同。
        }
        if(r=='-')
            a[i].r=-1;
        else
        {
            a[i].r=r-'0';
            a[a[i].r].root=true;
        }
    }
    cin>>n2;
    for(int i=0;i<n2;i++)
    {
        cin>>ch>>l>>r;
        b[i].ch=ch;
        if(l=='-')
            b[i].l=-1;
        else
        {
            b[i].l=l-'0';
            b[b[i].l].root=true;
        }
        if(r=='-')
            b[i].r=-1;
        else
        {
            b[i].r=r-'0';
            b[b[i].r].root=true;
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    build();//建树
    int root1=-1,root2=-1;
    for(int i=0;i<n1;i++)//查找树根
    {
        if(!a[i].root)
            root1=i;
        if(!b[i].root)
            root2=i;
    }
    if(judge(root1,root2))
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

ps:答案仅供参考,请勿抄袭

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值