uva12489 Combating cancer (tree isomorphism)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 3933

Give you two tree without root, let you judge whether the two trees are isomorphic

Does not judge the tree isomorphism, decisively copied a template, messed up gave.

First, given that there is no root tree, but to determine whether the root tree is isomorphic to the center of gravity as the root, and then do a sequence of parentheses hash.

So we need to find the center of gravity, the center of the tree to know the diameter first.

Find the diameter, the number of points on the diameter is even, then the center of gravity is two points in the middle, if it is an odd number, then the center of gravity is the middle.

or according to the extension row, each time the degree of 1 points into the queue, leaving the last batch is.

However, I was brain-pumped, to find the diameter, after the second to engage in ... In fact, to find the diameter of the time to save the path is good.

Finally, if you have two center of gravity to do two hashes, get two hashes, one at a time.

Finally, compare the hash values of the two trees with the same.

/** * Code generated by Jhelper * More info:Https://github.com/AlexeyDmitriev/JHelper* @author Xyiyy @Https://github.com/xyiyy */#include<iostream>#include<fstream>//#####################//Author:fraud//Blog:http://www.cnblogs.com/fraud///#####################//#pragma COMMENT (linker, "/stack:102400000,102400000")#include <iostream>#include<sstream>#include<ios>#include<iomanip>#include<functional>#include<algorithm>#include<vector>#include<string>#include<list>#include<queue>#include<deque>#include<stack>#include<Set>#include<map>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<climits>#include<cctype>using namespacestd;#definePB (x) push_back (x)#defineRep (X, N) for (int x=0; x<n; X + +)#defineAll (x) (x). Begin (), (x). End ()typedef unsignedLong Longull;Const intMaxnode =10010;Const intMaxEdge = (Maxnode <<1);////Created by Xyiyy on 2015/8/14.//#ifndef icpc_hashtree_hpp#defineIcpc_hashtree_hpp//the isomorphism of the tree, returns the hash value//Enter the root of a root tree, or the center of gravity without a root treetypedef unsignedLong Longull;Constull MAGIC =321;////Created by xyfra_000 on 2015/8/14.//#ifndef icpc_adjlist_array_hpp#defineIcpc_adjlist_array_hpp#defineForedge (A, X) for (int a = from[x]; A!=-1; A = Next[a])intFrom[maxedge], To[maxedge];intNext[maxedge];intedgecnt;voidInitintN) {Rep (i, N+1) From[i] =-1; Edgecnt=0;}voidAddedge (intUintv) {to[edgecnt]=v; NEXT[EDGECNT]=From[u]; From[u]= edgecnt++;}#endif //icpc_adjlist_array_hppull powmod (ull A,intN) {ull ret=1ULL;  while(n) {if(N &1) RET *=A; A*=A; N>>=1; }    returnret;}structHash {intlength;    Ull value; Hash (): Length (0), Value (0) {} Hash (Charc): Length (1), value (c) {} Hash (intLintv): Length (L), value (v) {}};BOOL operator< (ConstHash &a,ConstHash &b) {returnA.value <B.value;} Hashoperator+(ConstHash &a,ConstHash &b) {returnHash (A.length + b.length, A.value * POWMOD (MAGIC, b.length) +b.value);}void operator+ = (hash &a, Hash &b) {a= A +b;} Vector<Hash>Childs[maxnode]; Hash DFS (intPreintcur)    {Hash ret;    Childs[cur].clear ();  for(intiter = From[cur]; Iter! =-1; ITER =Next[iter]) {        if(To[iter]! =pre)        {CHILDS[CUR].PB (DFS (cur, to[iter]));    }} sort (All (childs[cur]));  for(Vector) {ret+= *ITER; } Hash Retl= Hash ('('); RET='('+ ret +')'; returnret;} Ull Gethash (introot) {    returnDFS (-1, root). Value;}#endif //icpc_hashtree_hpp////Created by xyfra_000 on 2015/8/14.//#ifndef icpc_treediameter_hpp#defineIcpc_treediameter_hpp//finding the diameter of a tree//the diameter of the tree that can be obtained by modifying the DFS section into a weightedVector<int>Dist;voidDfsintPintUintd) {Dist[u]=D; Foredge (i, u) {if(To[i]! =p) {DFS (U, to[i], D+1); }    }}intGetdiameter (intN) {dist.resize (n); DFS (-1,0,0); intU = max_element (All (dist))-Dist.begin (); DFS (-1, U,0); return*max_element (All (dist));}#endif //icpc_treediameter_hppintDeg[maxnode];intVis[maxnode];classTaskh { Public:    voidSolve (Std::istream &inch, Std::ostream & out) {        intN;  while(inch>>N) {vector<ull> ans[2]; Rep (Times,2) {                intu, v;                Init (n); Rep (i, N+1) Deg[i] =0; Rep (i, N+1) Vis[i] =0; Queue<int>Q; Rep (i, N-1) {                    inch>> u >>v; U--, v--; Deg[u]++; DEG[V]++;                    Addedge (U, v);                Addedge (V, u); }                intDia =getdiameter (n); intnum =N; Rep (i, N) {if(Deg[i] = =1) {Q.push (i); }                }                intGao =1; if(Dia &1) gao++;  while(num > Gao &&!)Q.empty ()) {u=Q.front ();                    Q.pop (); Vis[u]=1; Num--; Deg[u]--;  for(inti = From[u]; I! =-1; i =Next[i]) {                        intv =To[i]; if(!Vis[v]) {Deg[v]--; if(Deg[v] = =1) {Q.push (v); }}}} rep (I, N) {if(!Vis[i])                    {ANS[TIMES].PB (Gethash (i)); }                }            }            BOOLOK =0; Rep (i, ans[0].size ()) {Rep (J, ans[1].size ()) {if(ans[0][i] = = ans[1][J]) OK =1; }            }            if(OK) out<<"S"<<Endl; Else  out<<"N"<<Endl; }    }};intMain () {Std::ios::sync_with_stdio (false); Std::cin.tie (0);    Taskh solver; Std::istream&inch(std::cin); Std::ostream& out(std::cout); Solver.solve (inch, out); return 0;}

uva12489 Combating cancer (tree isomorphism)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.