Under construction

Still being built. Things may change

Walking Around

Interview

Time limit1sMemory limit2048 MB

Summary
Given a weighted tree, find the minimum and maximum XOR of edge weights over all paths between two distinct vertices.
Level

Medium4 of 10

Topics
Tree, Bit manipulation, Trie
Solved
No attempts yet

Problem

You are given a weighted tree with NN vertices, numbered from 11 to NN. The edges are numbered from 11 to N−1N - 1, where edge ii connects two vertices UiU_i and ViV_i and has a non-negative integer weight WiW_i.

A path in the tree is a sequence of distinct vertices (u0,u1,…,um)(u_0, u_1, \dots, u_m) for some m≥1m \ge 1, such that every pair of adjacent vertices (uj,uj+1)(u_j, u_{j+1}) for 0≤j<m0 \le j < m is connected by an edge of the tree. The score of the path (u0,…,um)(u_0, \dots, u_m) is the bitwise XOR of the weights of all edges on the path, that is XOR(w0,w1,…,wm−1)\text{XOR}(w_0, w_1, \dots, w_{m-1}), where wjw_j is the weight of the edge connecting uju_j and uj+1u_{j+1}.

Find the minimum and the maximum score among all paths in the tree.

Input

The first line contains an integer NN (2≤N≤100 0002 \le N \le 100\,000), the number of vertices. Each of the next N−1N - 1 lines contains three integers UiU_i, ViV_i, and WiW_i (1≤Ui<Vi≤N1 \le U_i < V_i \le N; 0≤Wi≤1090 \le W_i \le 10^9), describing edge ii.

Output

Print the minimum and the maximum score separated by a space, on a single line.

Examples3

  1. Example 1

    Input
    6
    1 2 3
    1 3 4
    2 4 2
    2 5 8
    3 6 1
    
    Expected output
    1 15
    
  2. Example 2

    Input
    6
    1 2 4
    1 3 3
    1 4 1
    4 5 7
    1 6 2
    
    Expected output
    1 7
    
  3. Example 3

    Input
    10
    1 2 5
    1 3 3
    2 4 8
    3 5 7
    2 6 6
    5 7 9
    4 8 8
    1 9 6
    6 10 11
    
    Expected output
    0 14