SRM 578 1 1000 DeerInZooDivOne


Problem Statement

Deer Only has recently shed his antlers. He is now ashamed and wants to make fake ones until the real ones grow back.

Deer Only has found a tree with N vertices. The vertices are numbered 0 through N-1. You are given two int[]s a and b that contain N-1 integers each. These arrays describe the edges of the tree: for each i, the vertices a[i] and b[i] are connected by an edge.

Deer Only has decided to make his new antlers out of this tree. The only operation he is able to do is to remove some edges from his tree, producing several smaller trees. Then, he wants to take two of the newly created trees and attach them to his head. The two antlers must be isomorphic, otherwise he will be unbalanced when running. (See Notes for a formal definition of tree isomorphism.)

Return the maximal size of the deer's new fake antlers. The size of an antler is defined as the number of vertices in it. Note that the largest possible antlers may sometimes consist only of a single vertex (and no edges).

Definition

(be sure your method is public)

Limits

Notes

Constraints

Test cases

  1. input
    • a { 0, 1, 2 }
    • b { 1, 2, 3 }
    output
    Returns 2
    note
    The tree looks as follows: 0-1-2-3. Deer Only can remove the edge between the vertex 1 and the vertex 2, and he gets two new trees 0-1 and 2-3. These two trees are isomorphic, so he can use these two trees as antlers.
  2. input
    • a { 1, 8, 1, 7, 4, 2, 5, 2 }
    • b { 5, 3, 6, 8, 2, 6, 8, 0 }
    output
    Returns 4
    note
    The two new trees will contain vertices 0,2,4,6 and 5,8,7,3.
  3. input
    • a { 0 }
    • b { 1 }
    output
    Returns 1
  4. input
    • a { 0, 11, 10, 10, 19, 17, 6, 17, 19, 10, 10, 11, 9, 9, 14, 2, 13, 11, 6 }
    • b { 7, 5, 2, 12, 8, 9, 16, 8, 4, 18, 8, 13, 15, 13, 17, 16, 3, 1, 7 }
    output
    Returns 8
  5. input
    • a { 14, 13, 28, 15, 20, 4, 9, 6, 1, 23, 19, 25, 25, 8, 14, 16, 2, 8, 15, 25, 22, 22, 28, 10, 10, 14, 24, 27, 8 }
    • b { 21, 5, 12, 13, 27, 1, 24, 17, 27, 17, 23, 14, 18, 26, 7, 26, 11, 0, 25, 23, 3, 29, 22, 11, 22, 29, 15, 28, 29 }
    output
    Returns 11

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.