All ICPC entries
Competitive Programming

ICPC 2016 - F. Longest Rivers

State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.

Source sync Apr 19, 2026
Track ICPC
Year 2016
Files TeX, C++, statement assets
Folder competitive_programming/icpc/2016/F-longest-rivers
ICPC2016TeXC++statement textstatement pdf

Source-first archive entry

This page is built from the copied files in competitive_programming/icpc/2016/F-longest-rivers. Edit competitive_programming/icpc/2016/F-longest-rivers/solution.tex to update the written solution and competitive_programming/icpc/2016/F-longest-rivers/solution.cpp to update the implementation.

The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.

Problem Statement

Copied statement text kept beside the solution archive for direct reference.

Problem F
                                        Longest Rivers
                                    Time limit: 10 seconds
The Chao Phraya River System is the main river system of
Thailand. Its six longest rivers listed by decreasing length are:

   1. Tha Chin (765 km)

   2. Nan (740 km)

   3. Yom (700 km)

   4. Ping (658 km)

   5. Pa Sak (513 km)

   6. Wang (335 km)
                                                                          The Chao Phraya River System. Picture from Wikimedia Commons.

A simplified model of this river system is shown in Figure F.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 0. When two or more rivers meet at a confluence (other than confluence 0), the
resulting merged river takes the name of one of those rivers. For example, the Ping and the Wang meet
at confluence 1 with the resulting merged river retaining the name Ping. With this naming, the Ping has
length 658 km while the Wang is only 335 km. If instead the merged river had been named Wang, then
the length of the Wang would be 688 km while the length of the Ping would be only 305 km.

                                                         Yom Nan
                                                      Wang
                                                                  700
                                                                        675
                                      Ping          335                        Pa Sak
                                             305          1
                             Tha Chin                         2               513
                                                      353          65
                                              765
                                                                    0

         Figure F.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 1 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 2.

Input

The first line of input contains two integers n (1 ≤ n ≤ 500 000), which is the number of river sources
in the system, and m (0 ≤ m ≤ n − 1), which is the number of confluences with positive labels. These
confluences are numbered from 1 to m.
The next n 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 c (0 ≤ c ≤ m) and d (1 ≤ d ≤ 109 ), where c is the identifier of the
nearest confluence downstream, and d 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 1 and 10 characters,
inclusive.
The final m lines describe confluences 1 to m in a similar fashion. The k th of these lines describes the
confluence with identifier k and contains two integers: the identifier of the nearest confluence down-
stream and the distance from confluence k to this confluence in kilometers.
It is guaranteed that each confluence 1 through m appears as “the nearest downstream” at least twice,
confluence 0 appears at least once, and all sources are connected to confluence 0.

Output

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.

 Sample Input 1                                       Sample Output 1
 6 2                                                  PaSak 5
 PaSak 0 513                                          Nan 2
 Nan 2 675                                            Yom 1
 Yom 2 700                                            Wang 3
 Wang 1 335                                           Ping 4
 Ping 1 305                                           ThaChin 1
 ThaChin 0 765
 0 353
 0 65

Editorial

Rendered from the copied solution.tex file. The original TeX source remains available below.

Key Observations

  • Write the structural observations that make the problem tractable.

  • State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.

  • If the constraints matter, explain exactly which part of the solution they enable.

Algorithm

  1. Describe the data structures and the state maintained by the algorithm.

  2. Explain the processing order and why it is sufficient.

  3. Mention corner cases explicitly if they affect the implementation.

Correctness Proof

We prove that the algorithm returns the correct answer.

Lemma 1.

State the first key claim.

Proof.

Provide a concise proof.

Lemma 2.

State the next claim if needed.

Proof.

Provide a concise proof.

Theorem.

The algorithm outputs the correct answer for every valid input.

Proof.

Combine the lemmas and finish the argument.

Complexity Analysis

State the running time and memory usage in terms of the input size.

Implementation Notes

  • Mention any non-obvious implementation detail that is easy to get wrong.

  • Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.

Code

Exact copied C++ implementation from solution.cpp.

C++ competitive_programming/icpc/2016/F-longest-rivers/solution.cpp

Exact copied implementation source.

Raw file
#include <bits/stdc++.h>
using namespace std;

namespace {

void solve() {
    // Fill in the full solution logic for the problem here.
}

}  // namespace

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();
    return 0;
}

Source Files

Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.

TeX write-up competitive_programming/icpc/2016/F-longest-rivers/solution.tex

Exact copied write-up source.

Raw file
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}

\title{ICPC World Finals 2016\\F. Longest Rivers}
\author{}
\date{}

\begin{document}
\maketitle

\section*{Problem Summary}

State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.

\section*{Key Observations}

\begin{itemize}[leftmargin=*]
    \item Write the structural observations that make the problem tractable.
    \item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
    \item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}

\section*{Algorithm}

\begin{enumerate}[leftmargin=*]
    \item Describe the data structures and the state maintained by the algorithm.
    \item Explain the processing order and why it is sufficient.
    \item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}

\section*{Correctness Proof}

We prove that the algorithm returns the correct answer.

\paragraph{Lemma 1.}
State the first key claim.

\paragraph{Proof.}
Provide a concise proof.

\paragraph{Lemma 2.}
State the next claim if needed.

\paragraph{Proof.}
Provide a concise proof.

\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.

\paragraph{Proof.}
Combine the lemmas and finish the argument.

\section*{Complexity Analysis}

State the running time and memory usage in terms of the input size.

\section*{Implementation Notes}

\begin{itemize}[leftmargin=*]
    \item Mention any non-obvious implementation detail that is easy to get wrong.
    \item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}

\end{document}