luogu#P6922. [ICPC2016 WF] Longest Rivers

[ICPC2016 WF] Longest Rivers

题目描述

The Chao Phraya River System is the main river system of Thailand. Its six longest rivers listed by decreasing length are:

Tha Chin (765765 km)

Nan (740740 km)

Yom (700700 km)

Ping (658658 km)

Pa Sak (513513 km)

Wang (335335 km)

A simplified model of this river system is shown in Figure 1, where the smaller red numbers indicate the lengths of various sections of each river. The point where two or more rivers meet as they flow downstream is called a confluence. Confluences are labeled with the larger black numbers. In this model, each river either ends at a confluence or flows into the sea, which is labeled with the special confluence number 00. When two or more rivers meet at a confluence (other than confluence 00), the resulting merged river takes the name of one of those rivers. For example, the Ping and the Wang meet at confluence 11 with the resulting merged river retaining the name Ping. With this naming, the Ping has length 658658 km while the Wang is only 335335 km. If instead the merged river had been named Wang, then the length of the Wang would be 688688 km while the length of the Ping would be only 305305 km.

Figure 1: The river system in Sample Input 1. Same-colored edges indicate a river.

The raised awareness of this phenomenon causes bitter rivalries among the towns along the rivers. For example, the townspeople along the Wang protest that maybe with a proper naming scheme, their river could actually be the longest, or maybe the second longest (or at least not last!). To end all the guessing, your task is to validate all such claims.

The rank of a river is its position in a list of all rivers ordered by decreasing length, where the best rank is 11 for the longest river. For each river, determine its best possible rank over all naming schemes. At any confluence, the name of a new, larger river in any naming scheme must be one of the names of the smaller rivers which join at that confluence. If two or more rivers have equal lengths in a naming scheme, all the tied rivers are considered to have the best possible ranking. For example, if one river is the longest and all other rivers are equal, those rivers all have rank 22.

输入格式

The first line of input contains two integers nn (1n500000)(1 \le n \le 500\, 000), which is the number of river sources in the system, and mm (0mn1)(0 \le m \le n - 1), which is the number of confluences with positive labels. These confluences are numbered from 11 to mm.

The next nn lines describe the rivers. Each of these lines consists of a string, which is the name of the river at the source, and two integers cc (0cm)(0 \leq c \leq m) and dd (1d109)(1 \leq d \leq 10^9), where cc is the identifier of the nearest confluence downstream, and dd is the distance from the source to that confluence in kilometers. River names use only lowercase and uppercase letters a–z, and consist of between 11 and 1010 characters, inclusive.

The final mm lines describe confluences 11 to mm in a similar fashion. The kthk^\text {th} of these lines describes the confluence with identifier kk and contains two integers: the identifier of the nearest confluence downstream and the distance from confluence kk to this confluence in kilometers.

It is guaranteed that each confluence 11 through mm appears as “the nearest downstream” at least twice, confluence 00 appears at least once, and all sources are connected to confluence 00.

输出格式

Display one river per line in the same order as in the input. On that line, display the name of the river and its best possible rank.

题目大意

nn 条河和 m+1m+1 个交汇处构成一棵以 00 号点(即大海) 为根的树。

每条河有各自的名称。对于一个交汇处,从它流出的干流的名称是流入这个交汇处的各个支流的名称之一。一条河流的长度是以它为名称的河流的长度之和。对于一个可能的命名方案,一条河流的排名等于长度大于它的河流数 +1+1

对于每条河,求出它在所有命名方案中,最小的排名。

6 2
PaSak 0 513
Nan 2 675
Yom 2 700
Wang 1 335
Ping 1 305
ThaChin 0 765
0 353
0 65

PaSak 5
Nan 2
Yom 1
Wang 3
Ping 4
ThaChin 1

提示

Time limit: 9000 ms, Memory limit: 1048576 kB.

International Collegiate Programming Contest (ACM-ICPC) World Finals 2016