luogu#P4787. [BalkanOI 2018] Minmaxtree

    ID: 3810 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018Special JudgeBalkanOI(巴尔干半岛)

[BalkanOI 2018] Minmaxtree

Problem Description

Translated from BalkanOI 2018 Day 1 T3 “Minmaxtree”.

There is an unweighted tree with NN nodes, numbered 1N1 \dots N. You need to assign a weight to each edge to make it a weighted tree. The weighted tree must satisfy KK conditions. There are two types of conditions:

  1.  M x y z  \ \texttt{M }x\ y\ z\ \ The maximum edge weight on the path from node xx to node yy is zz.
  2.  m x y z  \ \texttt{m }x\ y\ z\ \ The minimum edge weight on the path from node xx to node yy is zz.

It is guaranteed that the zz values in these KK conditions are all distinct.

Please construct such a tree and output the weight of each edge.

Input Format

The first line contains an integer NN.
In the next N1N - 1 lines, each line contains two integers, indicating an edge connecting two nodes.
Line N+1N + 1 contains an integer KK.
In the next KK lines, each line starts with a letter M\texttt{M} or m\texttt{m}, followed by three integers x,y,zx, y, z.

Output Format

Output N1N - 1 lines. Each line contains three integers x,y,vx, y, v separated by spaces, meaning an edge (x,y)(x, y) in the weighted tree has weight vv.

4
1 2
2 3
3 4
3
M 1 2 1
m 1 4 0
M 2 3 100
3 2 100
1 2 1
4 3 0

Hint

Subtask #1 (7 points): 1N,z10001 \le N, z \le 1000.
Subtask #2 (22 points): Only condition type 1, and no condition type 2.
Subtask #3 (29 points): All paths from xx to yy given in condition type 1 are pairwise edge-disjoint. All paths from xx to yy given in condition type 2 are also pairwise edge-disjoint.
Subtask #4 (42 points): No other restrictions.
For all testdata, 1N,K700001 \le N, K \le 70000, 0z1090 \le z \le 10^9.

Thanks to Planet6174 for providing the translation.

Translated by ChatGPT 5