#LCA0000036. Đường đi giữa các thành phố (Connections between cities)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi giữa các thành phố (Connections between cities)
Connections between cities
Source: HDU
Version: Phuoc Hung OJ Extended
Problem Statement
There are cities joined by undirected roads, each with a positive integer length. The remaining roads form a forest: any two connected cities have exactly one simple path, and isolated cities may exist.
For each pair , output the sum of road lengths along their unique path. If the cities are in different connected components, print exactly Not connected. For , the distance is .
The original HDU input has datasets until EOF; PHOJ uses one dataset per input.
Input
- First line: .
- Next lines: for each undirected positively weighted road.
- Last lines: city pairs .
Output
Print the integer distance or the exact string Not connected for each query.
Subtasks
General constraints (all subtasks): ; ; ; (PHOJ extended weight limit).
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5
Output
Not connected
6
Explanation
Cities and lie in different components. The path has length .