#C8137. Tree Height and Leaf Count

    ID: 52086 Type: Default 1000ms 256MiB

Tree Height and Leaf Count

Tree Height and Leaf Count

You are given a binary tree defined by a set of nodes and edges. The tree is rooted at node 1. For each edge, you are given three integers u, v and c where c indicates whether v is the left child (when c = 0) or the right child (when c = 1) of u.

Your task is to construct the binary tree from the given description, and then compute two quantities for the tree:

  • The height of the tree, defined as the number of edges in the longest path from the root to a leaf. In mathematical terms, if the height of a node is \(h\) then the height of the tree is \(\max_{leaf}(h)\).
  • The number of leaf nodes in the tree.
  • </p>

    Note: A single node is considered a leaf, and its height is defined to be 0.

    inputFormat

    The input starts with an integer T indicating the number of test cases. Each test case is described as follows:

    1. An integer N representing the number of nodes in the tree.
    2. N-1 lines each containing three integers u, v, and c separated by spaces, representing an edge from node u to node v. Here, c = 0 indicates that v is the left child and c = 1 indicates that v is the right child of u.

    All input is read from standard input (stdin).

    outputFormat

    For each test case, output two integers on a single line: the height of the tree and the number of leaf nodes, separated by a space. The output for all test cases should be printed on standard output (stdout), each on a new line.

    ## sample
    1
    1
    
    0 1
    

    </p>