#QHD0000082. Đường đua xe dê (Moorio Kart)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đua xe dê (Moorio Kart)
Moorio Kart
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
There are meadows connected by undirected roads. Road joins meadows and and has nonnegative integer length . The existing network is a forest with no cycles, and every connected component (farm) contains at least two vertices. Consequently, every pair of distinct meadows in the same farm is joined by exactly one simple path.
Suppose there are farms. Bessie wants to add exactly roads, each of length , to create one closed racing loop. The loop must visit each farm exactly once and traverse at least one existing road inside each farm. Within any farm, the loop follows the unique simple path between two distinct selected entry and exit meadows.
Only loops of total length at least are interesting. Two loops are different precisely when the sets of adjacent meadow pairs differ after adding the new roads. Reversing the travel direction or choosing a different starting point does not create a new loop. New roads may join different farms; if there is only one farm, its added road closes the loop between two distinct meadows in that farm.
Compute the sum of lengths of all interesting loops, modulo . Print neither the number of loops nor their road sets.
Input
The first line contains . Each of the following lines contains (vertices are numbered from to ). The existing roads form a forest in which every vertex is incident to at least one road.
Output
Print one integer: the sum of lengths of all loops of length at least , modulo .
Subtasks
-
Subtask 1 (20 points): ; ; ; ; forest with no isolated vertices.
-
Subtask 2 (30 points): ; ; ; ; forest with no isolated vertices.
-
Subtask 3 (50 points): ; ; ; ; forest with no isolated vertices.
Examples
Example 1
Input
5 3 1 12
1 2 3
2 3 4
4 5 6
Output
54
Explanation
There are two farms. The six loops have lengths . The qualifying four sum to .
Example 2
Input
2 1 2 4
1 2 3
Output
5
Explanation
There is one farm and one existing path of length ; a new road of length closes the only loop, of length .