#include#include#include#include#includeusingnamespacestd;constintMod=9901;constintMax=1010;//节点数constintMul=9110;
vectoradj[Max];intg[Max][Max], q[Max], level[Max], visit[Max];intfather[Max], hash[Max], one[2], two[2];boolcmp(inta,intb)
{returnhash[a]
}voidTop(intn,intx)//拓扑排序找根{
memset(visit,0,sizeof(visit));inthead=0, tail=0;for(inti=0; i
visit[i]=1;
level[tail]=0;
q[tail++]=i;
}
}while(head
g[i][n]--;if(g[i][n]<=1){
level[tail]=l+1;
q[tail++]=i;
visit[i]=1;
}
}
}intl=level[tail-1], k=0;for(inti=tail-1; level[i]==l; i--){if(k==0){
one[x]=q[i]; k++;
}elsetwo[x]=q[i];
}
}voidbuild(introot,intn)
{
visit[root]=1;for(inti=0; i
adj[root].push_back(i);
build(i, n);
}
}
}intdfs(intk)
{intval=0;if(adj[k].size()==0)
val=1;else{for(inti=0; i
hash[j]=dfs(j);
}
sort(adj[k].begin(), adj[k].end(), cmp);
val=1908;for(inti=0; i
val=((val*Mul)^hash[j])%Mod;
}
}returnval;
}intmain()
{inti,j,n,cas,a,b;chars[Max];
scanf("%d",&cas);while(cas--){inthash1, hash2;
scanf("%d",&n);//读入n个节点memset(g,0,sizeof(g));
one[0]=-1; one[1]=-1; two[0]=-1; two[1]=-1;for(i=0; i
a--; b--;
g[a][b]++; g[b][a]++;
g[a][n]++; g[b][n]++;
}
Top(n,0);
memset(visit,0,sizeof(visit));for(inti=0; i
adj[i].clear();
build(one[0],n);
hash1=dfs(one[0]);
memset(g,0,sizeof(g));for(i=0; i
scanf("%d %d",&a,&b);
a--; b--;
g[a][b]++; g[b][a]++;
g[a][n]++; g[b][n]++;
}
Top(n,1);
memset(visit,0,sizeof(visit));for(inti=0; i
adj[i].clear();
build(one[1],n);
hash2=dfs(one[1]);if(hash1!=hash2&&two[1]!=-1){
memset(visit,0,sizeof(visit));for(inti=0; i
adj[i].clear();
build(two[1],n);
hash2=dfs(two[1]);
}//printf("%d %d %d %d\n", one[0], two[0], one[1], two[1]);printf("%s\n", hash1==hash2?"same":"different");
}return0;
}