The difficulty of this question is actually not as difficult questions of purple. Mainly in the details of processing more complicated when a hash;
The first is the tree hash template:
long long treehash(int u,int fa) { long long q[1001]; long long num=0; long long ans=1; for(int i=head[u];i;i=star[i].nxt){ int v=star[i].to; if(v==fa) continue; q[++num]=treehash(v,u); } sort(q+1,q+num+1); for(int i=1;i<=num;i++){ ans=ans*2333+q[i]; } return ans*2333+1; }
For unrooted trees in little time data may be sequentially enumerated root hash value for each point of time when the root, then the tree becomes a hash value of the hash value of a set of numbers;
N ^ 2 and then compare each linear tree can be a hash value;
#include <bits/stdc++.h> using namespace std; struct littlestar{ int to; int nxt; }star[20010]; long long head[20010],cnt; void add(int u,int v) { star[++cnt].to=v; star[cnt].nxt=head[u]; head[u]=cnt; } long long myhash[20010]; long long treehash(int u,int fa) { long long q[1001]; long long num=0; long long ans=1; for(int i=head[u];i;i=star[i].nxt){ int v=star[i].to; if(v==fa) continue; q[++num]=treehash(v,u); } sort(q+1,q+num+1); for(int i=1;i<=num;i++){ ans=ans*2333+q[i]; } return ans*2333+1; } long long lala[20010]; long long ans[20010]; int main () { int t; cin>>t; for(int i=1;i<=t;i++){ memset(head,0,sizeof(head)); memset(lala,0,sizeof(lala)); cnt=0; int n; cin>>n; int root=1; for(int j=1;j<=n;j++){ int x; scanf("%d",&x); if(x==0){ root=j; continue; } add(x,j); add(j,x); } for(intj = 1 ; j <= n; j ++) myhash [j] = treehash (j, 0 ); sort (myhash + 1 , myhash + 1 + n); for ( int j = 1 ; j <= n; j ++) lala [j] = lala [j- 1 ] * myhash [j] + 233 ; years [i] = lala [n]; for ( int j = 1 ; j <= i; j ++ ) { if (years [i] == years [j]) { cout << d << endl; break ; } } } return 0; }