Walking Around

시간 제한1초메모리 제한2048 MB

요약
가중치가 있는 트리에서 임의의 단순 경로가 가질 수 있는 간선 가중치 XOR의 최솟값과 최댓값을 구한다.
난이도

어려움10점 중 8점

유형
트리, 비트 연산, 트라이, DFS
정답자
아직 제출이 없습니다

문제

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 U_iU\_i and V_iV\_i with a weight of a non-negative integer W_iW\_i.

A path in the tree is defined as a sequence of unique vertices (u_0,u_1,…,u_m)(u\_0, u\_1, \dots , u\_m) for some m≥1m ≥ 1 such that each pair of adjacent vertices, (u_j,u_j+1)(u\_j , u\_{j+1}) for all 0≤j<m0 ≤ j < m, is connected by an edge in the tree. Define the score of a path (u_0,u_1,…,u_m)(u\_0, u\_1, \dots , u\_m) as the bitwise XOR of the weight of all edges in the path, i.e. XOR(w_0,w_1,…,w_m−1)\text{XOR}(w\_0, w\_1, \dots , w\_{m-1}) where w_jw\_j is the weight of the edge that connects u_ju\_j and u_j+1u\_{j+1} (for all 0≤j<m0 ≤ j < m).

Your task is to find the minimum and the maximum score of any path that can be obtained from the given tree.

For example, the minimum and the maximum score of any path in the following tree are path (4,2,1)(4, 2, 1) with a score of XOR(2,3)=1\text{XOR}(2, 3) = 1, and path (5,2,1,3)(5, 2, 1, 3) with a score of XOR(8,3,4)=15\text{XOR}(8, 3, 4) = 15, respectively.

입력

Input begins with an integer NN (2≤N≤100,0002 ≤ N ≤ 100\\, 000) representing the number of vertices in the given tree. Each of the next N−1N - 1 lines contains three integers U_iU\_i V_iV\_i W_iW\_i (1≤U_i<V_i≤N1 ≤ U\_i < V\_i ≤ N; 0≤W_i≤1090 ≤ W\_i ≤ 10^9) representing edge ii.

출력

Output two space-separared integers in a single line, representing the minimum and the maximum score of any path that can be obtained from the given tree in that order.

예제3

  1. 예제 1

    입력
    6
    1 2 3
    1 3 4
    2 4 2
    2 5 8
    3 6 1
    
    예상 출력
    1 15
    
  2. 예제 2

    입력
    6
    1 2 4
    1 3 3
    1 4 1
    4 5 7
    1 6 2
    
    예상 출력
    1 7
    
  3. 예제 3

    입력
    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
    
    예상 출력
    0 14