PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000019. Tổng và nhỏ nhất trên hành trình (Analysis of Pathes in Functional Graph)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng và nhỏ nhất trên hành trình (Analysis of Pathes in Functional Graph)
Analysis of Pathes in Functional Graph
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are vertices numbered to . Each vertex has exactly one directed outgoing edge to , carrying weight .
For every starting vertex , follow exactly outgoing edges. Compute the sum of weights and the minimum weight among those traversed edges.
A vertex or edge can be visited multiple times; each traversal contributes to the sum.
Input
- The first line contains .
- The second line contains destination indices .
- The third line contains outgoing-edge weights .
Output
Print lines for starting vertices . Each line contains the sum of weights and the minimum weight, separated by a space.
Subtasks
- Subtask 1 (20 points): ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
7 3
1 2 3 4 3 2 6
6 3 1 4 2 2 3
Output
10 1
8 1
7 1
10 2
8 2
7 1
9 3
Explanation
Starting at vertex , the next three traversed edges have weights , so their sum is and the minimum is . The remaining lines correspond to other starting vertices.