Halcyon

시간 제한10초메모리 제한1024 MB

요약
같은 n개 정점 위의 두 가중치 트리가 주어질 때, 각 k에 대해 첫 번째 트리에서 k개, 두 번째 트리에서 n-1-k개의 간선을 사용하는 최소 가중치 신장 트리의 무게를 구하고 불가능하면 -1을 출력한다.
난이도

어려움10점 중 9점

유형
최소 신장 트리, 그래프, 행렬, 수학
정답자
아직 제출이 없습니다

문제

Minseok and Martin have two weighted trees T_1,T_2T\_1, T\_2. They share the same vertex set of size nn, where we index each vertex with integers from 1,2,…,n1, 2, \ldots, n.

For a given kk, Minseok selects kk edges from T_1T\_1, and Martin selects n−1−kn-1-k edges from T_2T\_2. The union of their selected edges should form a tree. If this is possible, they should minimize the total weight of selected edges.

입력

In the first line, a single integer NN denoting the number of vertices in both trees is given.

In the next N−1N-1 lines, description of the first tree is given. Each of the N−1N-1 lines contains three integers S_i,E_i,W_iS\_i, E\_i, W\_i, which indicates there is an edge connecting two vertices S_i,E_iS\_i, E\_i with weight W_iW\_i.

In the next N−1N-1 lines, description of the second tree is given in the same format.

출력

For all 0≤k≤n−10 \le k \le n - 1, print the minimum total weight, or print -1 if it is impossible.

제한

  • 2≤N≤250,0002 \le N \le 250\\,000
  • 1≤S_i,E_i≤N,1≤W_i≤1091 \le S\_i, E\_i \le N, 1 \le W\_i \le 10^9 (1≤i≤N−11 \le i \le N-1)

예제2

  1. 예제 1

    입력
    5
    1 2 10
    2 4 20
    3 4 30
    4 5 50
    1 2 15
    1 3 25
    1 4 35
    1 5 25
    
    예상 출력
    100
    85
    80
    85
    110
    
  2. 예제 2

    입력
    9
    5 7 6577
    4 5 8869
    5 9 9088
    2 1 124
    6 2 410
    2 8 8154
    4 8 4810
    3 4 4268
    3 9 763
    6 2 8959
    7 4 7984
    3 8 504
    8 6 9085
    5 2 4861
    1 9 8539
    1 7 7834
    
    예상 출력
    48529
    39568
    31019
    26748
    25491
    25661
    29669
    33975
    42300