poj1635_树的同构

题意:

这个题首先给出一个01序列,这个序列的意思是,从一个树的根开始遍历,遇到0走到一个孩子,遇到1则当前节点返回到它的父亲节点,最后回到树的根。求对这样两个01序列进行遍历后形成的两颗树是否同构。

分析:

1.这个序列中的0和1个数必须相同,否则无法回到根。有几个0就有几个节点。

2.遍历01序列建树,记录每个节点的深度和孩子个数,然后排序进行比较。

3.一开始,我记录每个节点的孩子个数仅限于孩子(没有记录孙子,曾孙等),结果WA了。应该记录每个节点的所有后代的个数。

4.这道题我用的是静态链表。

代码:

View Code
  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <memory.h>
  4 #include <string>
  5 #include <algorithm>
  6 using namespace std;
  7 const int maxnum=1505;
  8 
  9 struct node
 10 {
 11     int depth;
 12     int child_num;  //儿子及其以下的节点个数
 13     int parent;
 14 }list[maxnum],llist[maxnum];
 15 int use,Use;
 16 
 17 void Init()
 18 {
 19     int i;
 20     for(i=0;i<maxnum;i++)
 21     {
 22         list[i].child_num=0;
 23         list[i].parent=-1;
 24     }
 25     list[0].depth=0;
 26     list[0].child_num=1;
 27 
 28     for(i=0;i<maxnum;i++)
 29     {
 30         llist[i].child_num=0;
 31         llist[i].parent=-1;
 32     }
 33     llist[0].depth=0;
 34     llist[0].child_num=1;
 35 }
 36 
 37 bool compare(struct node a,struct node b)
 38 {
 39     if(a.depth==b.depth)
 40         return a.child_num<b.child_num;
 41     else
 42         return a.depth<b.depth;
 43 }
 44 
 45 void fuction()
 46 {
 47     int i;
 48     int p,len;
 49     string s;
 50     cin>>s;
 51     len=s.size();
 52     p=0;
 53     use=1;
 54     for(i=0;i<len;i++)
 55     {
 56         if(s[i]=='0')
 57         {
 58            list[use].child_num=1;
 59            list[use].depth=list[p].depth+1;
 60            list[use].parent=p;
 61            p=use;
 62            use++;
 63         }
 64         else
 65         {
 66             int t=list[p].child_num;
 67             p=list[p].parent;
 68             list[p].child_num+=t;
 69         }
 70     }
 71     sort(list,list+use,compare);
 72 //    for(i=0;i<use;i++)
 73 //        cout<<list[i].depth<<" "<<list[i].child_num<<endl;
 74 //    cout<<endl;
 75 }
 76 
 77 void Fuction()
 78 {
 79     int i;
 80     int p,len;
 81     string s;
 82     cin>>s;
 83     len=s.size();
 84     p=0;
 85     Use=1;
 86     for(i=0;i<len;i++)
 87     {
 88         if(s[i]=='0')
 89         {
 90             llist[Use].child_num=1;
 91             llist[Use].depth=llist[p].depth+1;
 92             llist[Use].parent=p;
 93             p=Use;
 94             Use++;
 95         }
 96         else
 97         {
 98             int t=llist[p].child_num;
 99             p=llist[p].parent;
100             llist[p].child_num+=t;
101         }
102     }
103     sort(llist,llist+Use,compare);
104 //    for(i=0;i<Use;i++)
105 //        cout<<llist[i].depth<<" "<<llist[i].child_num<<endl;
106 }
107 
108 bool same()
109 {
110     if(use!=Use)
111         return false;
112     int i;
113     for(i=0;i<use;i++)
114     {
115         if(list[i].depth!=llist[i].depth)
116             return false;
117         if(list[i].child_num!=llist[i].child_num)
118             return false;
119     }
120     return true;
121 }
122 
123 int main()
124 {
125     int num;
126     scanf("%d",&num);
127     while(num--)
128     {
129         Init();
130         fuction();
131         Fuction();
132         if(same())
133             printf("same\n");
134         else
135             printf("different\n");
136     }
137     return 0;
138 }
139 /*
140 3
141 0010011101001011
142 0100011011001011
143 0010011101001011
144 0100011011001011
145 0100101100100111
146 0011000111010101
147 */

tjuoj 1503

posted @ 2012-08-06 08:51  pushing my way  阅读(526)  评论(0编辑  收藏  举报