POJ 1635 Subway tree systems

来源:互联网 发布:潍坊学院淘宝地址 编辑:程序博客网 时间:2024/06/16 12:49

Description

Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, there is one and only one way of going by subway. Moreover, most of these cities have a unique central station. Imagine you are a tourist in one of these cities and you want to explore all of the subway system. You start at the central station and pick a subway line at random and jump aboard the subway car. Every time you arrive at a station, you pick one of the subway lines you have not yet travelled on. If there is none left to explore at your current station, you take the subway line back on which you first came to the station, until you eventually have travelled along all of the lines twice,once for each direction. At that point you are back at the central station. Afterwards, all you remember of the order of your exploration is whether you went further away from the central station or back towards it at any given time, i.e. you could encode your tour as a binary string, where 0 encodes taking a subway line getting you one station further away from the central station, and 1 encodes getting you one station closer to the central station. 

Input

On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test scenario consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a correct exploration tour of a subway tree system.

Output

exploration tours of the same subway tree system, or the text "different" if the two strings cannot be exploration tours of the same subway tree system.

Sample Input

20010011101001011010001101100101101001011001001110011000111010101

Sample Output

samedifferent

Source

Northwestern Europe 2003
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

树的同构~

因为给出的是dfs序,所以我们就按照dfs来记录每一个节点的父亲节点,因为遍历到的点不会相同,所以直接将dfs序中的标号作为树节点的标号,然后再对于每个节点求出他的子树的大小,排序。

因为两棵树同构当且仅当树的每个节点都有相对应的节点使得两节点子树大小相同,所以直接比对两树子树大小的序列是否相同即可。

注意串的长度不同时两树一定不同构~


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int t,len[2],a[30001],b[30001],fa[2][30001],num[2][30001];bool flag;char s[2][30001];void findd(int u){int now=-1;for(int i=1;i<=len[u];i++)  if(s[u][i]=='0')  {  fa[u][i]=now;now=i;  }  else now=fa[u][now];}void cal(int u){int now=-1;for(int i=1;i<=len[0];i++)  if(s[u][i]='1')  {  now=i;  while(now!=-1)  {  num[u][now]++;  now=fa[u][now];  }  }}int main(){scanf("%d",&t);while(t--){scanf("%s%s",s[0]+1,s[1]+1);flag=0;len[0]=strlen(s[0]+1);len[1]=strlen(s[1]+1);if(len[0]!=len[1]){puts("different");continue;}memset(fa[0],-1,sizeof(fa[0]));memset(fa[1],-1,sizeof(fa[1]));memset(num[0],0,sizeof(num[0]));memset(num[1],0,sizeof(num[1]));findd(0);cal(0);findd(1);cal(1);sort(num[0]+1,num[0]+len[0]+1);sort(num[1]+1,num[1]+len[1]+1);for(int i=1;i<=len[0];i++)  if(num[0][i]!=num[1][i])  {  puts("different");flag=1;break;  }if(!flag) puts("same");}return 0;}


1 0