数据结构实验之二叉树一:树的同构

来源:互联网 发布:游族网络(002174)股吧 编辑:程序博客网 时间:2024/05/21 22:36
<h3 class="problem-header" style="box-sizing: border-box; font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(48, 105, 190); margin: 0px 0px 10px; font-size: 24px; text-align: center;">数据结构实验之二叉树一:树的同构</h3><div class="prob-info" style="box-sizing: border-box; text-align: center; margin: 10px 0px; font-size: 14px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 20px;"><span class="user-black" style="box-sizing: border-box;">Time Limit: 1000MS</span> <span class="user-black" style="box-sizing: border-box;">Memory Limit: 65536KB</span></div><div align="center" class="form-group form-inline" style="box-sizing: border-box; margin-bottom: 15px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><a target=_blank href="http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestsubmit/cid/1845/pid/3340.html" class="btn btn-default btn-sm " style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; display: inline-block; padding: 5px 10px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 3px; background-image: none;">Submit</a> <a target=_blank href="http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/conteststatus/cid/1845/pid/3340.html" class="btn btn-default btn-sm" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; display: inline-block; padding: 5px 10px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; white-space: nowrap; vertical-align: middle; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 3px; background-image: none;">Statistic</a></div><h4 style="box-sizing: border-box; font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Problem Description</h4><div class="prob-content" style="box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(232, 232, 232); padding: 9px 15px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px;"><span style="box-sizing: border-box;">给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img src="http://acm.sdut.edu.cn/image/3340_1.png" width="700" height="226" alt="" style="box-sizing: border-box; border: 0px; vertical-align: middle; max-width: 100%;" /><br style="box-sizing: border-box;" /><br type="_moz" style="box-sizing: border-box;" /></span></p><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px; text-align: center;">图1</p><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px;"> </p><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px;"><span style="box-sizing: border-box;"><img src="http://acm.sdut.edu.cn/image/3340_2.png" width="700" height="267" alt="" style="box-sizing: border-box; border: 0px; vertical-align: middle; max-width: 100%;" /><br style="box-sizing: border-box;" /></span></p><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px; text-align: center;">图2</p><p class="\" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 3px;"><span style="box-sizing: border-box;">现给定两棵树,请你判断它们是否是同构的。</span></p></div><h4 style="box-sizing: border-box; font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Input</h4><div class="prob-content" style="box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(232, 232, 232); padding: 9px 15px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> <span style="box-sizing: border-box;">输入数据包含多组,每组数据给出</span><span new="" style="box-sizing: border-box;">2</span><span style="box-sizing: border-box;">棵二叉树的信息。对于每棵树,首先在一行中给出一个非负整数</span><span new="" style="box-sizing: border-box;">N (</span><span style="box-sizing: border-box;">≤</span><span new="" style="box-sizing: border-box;">10)</span><span style="box-sizing: border-box;">,即该树的结点数(此时假设结点从</span><span new="" style="box-sizing: border-box;">0</span><span style="box-sizing: border-box;">到</span><span new="" style="box-sizing: border-box;">N−1</span><span style="box-sizing: border-box;">编号);随后</span><span new="" style="box-sizing: border-box;">N</span><span style="box-sizing: border-box;">行,第</span><span new="" style="box-sizing: border-box;">i</span><span style="box-sizing: border-box;">行对应编号第</span><span new="" style="box-sizing: border-box;">i</span><span style="box-sizing: border-box;">个结点,给出该结点中存储的</span><span new="" style="box-sizing: border-box;">1</span><span style="box-sizing: border-box;">个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出</span><span new="" style="box-sizing: border-box;">”-”</span><span style="box-sizing: border-box;">。给出的数据间用一个空格分隔。<br style="box-sizing: border-box;" />注意:题目保证每个结点中存储的字母是不同的。</span></div><h4 style="box-sizing: border-box; font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Output</h4><div class="prob-content" style="box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(232, 232, 232); padding: 9px 15px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> <span style="box-sizing: border-box;">如果两棵树是同构的,输出“</span><span new="" style="font-family:\;box-sizing: border-box;">Yes</span><span style="font-family:\;box-sizing: border-box;">”,否则输出“</span><span new="" style="font-family:\;box-sizing: border-box;">No</span><span style="font-family:\;box-sizing: border-box;">”。</span></div><h4 style="box-sizing: border-box; font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(51, 51, 51); margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Example Input</h4><div class="prob-content" style="box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(232, 232, 232); padding: 9px 15px; color: rgb(51, 51, 51); font-family: 微软雅黑, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; background-color: rgb(245, 245, 245);">8A 1 2B 3 4C 5 -D - -E 6 -G 7 -F - -H - -8G - 4B 7 6F - -A 5 1H - -C 0 -D - -E 2 -

Example Output

Yes

Hint

测试数据对应图1

Author

 xam
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;class tree{ public: char data; int l,r;}t1[11],t2[11];int creat(tree t[],int n){ int root = -1; char a[11],b[11],c[11],cl,cr; int check[11]; if(n) { int i; for(i = 0; i < n; i ++) { check[i] = 0; } for(i = 0; i < n; i ++) { scanf("%s%s%s",a,b,c); t[i].data = a[0]; cl = b[0]; cr = c[0]; if(cl != '-') { t[i].l=cl - '0'; check[t[i].l] = 1; } else t[i].l = -1; if(cr != '-') { t[i].r = cr - '0'; check[t[i].r] = 1; } else t[i].r = -1; } for(i = 0; i < n; i ++) { if(! check[i]) { root = i; break; } } } return root;}int judge(int r1,int r2){ if(r1 == -1 && r2 == -1) return 1;//都是空树; if((r1 == -1 && r2 != -1) || (r1 != -1 && r2 == -1)) return 0;//有一个唯空,另一个不是; if(t1[r1].data != t2[r2].data) return 0;//根节点不同; if(t1[r1].l == -1 && t2[r2].l == -1) return judge(t1[r1].r,t2[r2].r);//右子树同构,判断左子树; if((t1[r1].l != -1) && (t2[r2].l != -1) && (t1[t1[r1].l].data) == (t2[t2[r2].l].data)) return (judge(t1[r1].l,t2[r2].l) && judge(t1[r1].r,t2[r2].r));//左右同构; else return (judge(t1[r1].l,t2[r2].r) && judge(t1[r1].r,t2[r2].l));}int main(){ int n; int r1,r2; while(cin >>n) { r1 = creat(t1,n); int m; cin >>m; r2 = creat(t2,m); if(judge(r1,r2)) { cout <<"Yes"<<endl; } else cout <<"No"<<endl; } return 0;}

0 0
原创粉丝点击