Obviously, this should be a template problem (because I quickly found the template in a book), but I am more curious about the concept of a root tree and a tree without roots, and the root of this problem is the use of eggs? (It seems that the tree this piece of knowledge should be mended). The isomorphism of the tree should be judged by the hash, of course, different people design the hash function is different. This question is correct should be in the center of the tree to run the template of the problem, (if you want to ask me what the center of gravity of the tree, I can only tell you, if I know, the following code will not be almost all of the points run once, but because N<=50,m <= 50 Obviously this run is very fast, It turns out that only 32 milliseconds, but in a pragmatic attitude, find a time to fill the tree again! )。 Obviously, although the two trees are identical in shape, the resulting hash value will be different if you start running the template from different nodes. So the right thing to do is to run at almost the sole center of gravity (it is said that a tree with a root has a maximum of two center of gravity). Come on! Believe in yourself. (I am a shameful source of Cheng).
1#include <cstdio>2#include <iostream>3#include <vector>4#include <Set>5#include <map>6#include <algorithm>7 #defineull unsigned long Long8 #defineMagic 107ull9 #defineRep (i,j,k) for (int i = j; I <= K; i++)Ten #defineMAXN 60 One using namespacestd; A - intRead () - { the ints =0, t =1;Charc =GetChar (); - while( !IsDigit (c)) { - if(c = ='-') T =-1; c =GetChar (); - } + while(IsDigit (c)) { -s = S *Ten+ C-'0'; c =GetChar (); + } A returnS *T; at } - -Ull Pow (ull key,intN) - { -ull ret =1ull; - while(n) { in if(N &1 ){ -RET *=key; to } +Key *= key, n >>=1; - } the returnret; * } $ Panax Notoginseng structhash{ - intlength; ull key; the +Hash (): Length (0), Key (0) {} AHashCharc): Length (1), key (c) {} theHashintl,ull Key): Length (L), key (key) {} + }; -VectorCHILDS[MAXN]; $ $ BOOL operator< (ConstHash&a,Consthash&b) { - returnA.key <B.key; - } the -Hashoperator+ (ConstHash&a,Consthash&b) {Wuyi returnHash (A.length+b.length,a.key*pow (magic,b.length) +b.key); the } - Wu void operator+ = (Hash&a,Consthash&b) { -A = a +b; About } $ -vector<int>G[MAXN]; - -Hash DFS (intPreintNow ) A { + hash ret; theChilds[now].clear ();ints =g[now].size (); -Rep (I,0. So1){ $ intto =G[now][i]; the if(to = = Pre)Continue; the Childs[now].push_back (Dfs (now,to)); the } the sort (Childs[now].begin (), Childs[now].end ()); - for(Vector){ inRET + = *iter; the } theRET ='('+ ret +')'; About returnret; the the } the +Ull Gethash (introot) - { the returnDFS (-1, Root). Key;Bayi } the the Set<ull>s; -Map<ull,int>MP; - the voidClear () the { theRep (I,1, MAXN) g[i].clear (); the } - the intMain () the { the intn =read ();94Rep (I,1, N) { the Clear (); S.clear (); the intm =read (); theRep (J,1, M) {98 intx =read (); About if(x) g[j].push_back (x), G[x].push_back (j); - }101 BOOLOK =0;102Rep (J,1, M) {103ull key =Gethash (j);104 if(Mp[key]) { theprintf"%d\n", Mp[key]);106OK =1;107 Break;108 }109 ElseS.insert (key); the } 111 if( !OK) { theprintf"%d\n", i);113 for(Set<ull>::iterator iter = S.begin (); Iter! = S.end (); iter++ ){ theMp[*iter] =i; the } the }117 }118 return 0;119}
Isomorphism of Bzoj 4337 trees