#P6381. 『MdOI R2』Odyssey
『MdOI R2』Odyssey
Background
Beyond the limit of supersonic speed, yet not as magnificent and ever-changing as the aurora.
Faint pulses carve out a world that was once pure chaos.
A deep blue flickers slowly, and epic red welcomes the peak.
At the end of the blood-red sunset lie the stars of the coming night.
Even the star-filled midnight sky can be covered by smoke from hell.
The blazing red purgatory fades away; only golden ruins last forever.
What awaits everyone here is a hard yet brilliant journey.
Problem Description
If positive integers and satisfy:
- The product of and is the -th power of a positive integer, that is, there exists a positive integer such that .
Then we call a pair of perfect numbers.
There is a directed acyclic graph with nodes and edges. Each edge in this graph has two attributes: a weight and a length.
If a path satisfies one of the following conditions, then it is called a perfect path:
-
contains only one edge.
-
Starting from its beginning, consists of edges . For any , the weights of and form a perfect-number pair.
You need to find the length of the longest perfect path in the graph. The length of a path is defined as the sum of the lengths of all edges on the path.
Input Format
The first line contains three integers , representing the number of nodes, the number of edges, and the parameter of perfect-number pairs.
The next lines each contain four integers , meaning there is a directed edge from to with weight and length .
Output Format
The first line contains one integer , representing the length of the longest perfect path.
5 7 2
2 5 2 5
5 3 18 9
2 4 6 7
4 3 6 3
2 1 24 2
1 4 6 8
1 5 8 4
14
Hint
【Help and Hints】
To make it easier for contestants to test their code, this problem additionally provides two extra sample tests.
Sample Input 1 Sample Output 1
Sample Input 2 Sample Output 2
【Sample Explanation】
The directed acyclic graph in the sample is shown in the figure, where the red numbers on edges are the weights, and the black numbers are the lengths:

The longest perfect path is , because the weights and of these two edges satisfy , which is a perfect-number pair. The path length is .
In addition, and so on are also perfect paths, but they are not the longest.
In the graph, the path has length , which is longer, but it is not a perfect path, because the product of the first two edge weights and is , which is not a square of a positive integer. That is, is not a perfect-number pair.
【Constraints】
This problem uses bundled tests.
For of the testdata: $1\leq n\leq 10^5,\ \ 1\leq m\leq 2\times 10^5,\ \ 1\leq k\leq 10,\ \ 1\leq u,v\leq n,\ \ 1\leq w\leq 10^5,\ \ 1\leq l\leq 10^4$.
The given graph is not guaranteed to be weakly connected. There may be multiple edges from one node to another, but it is guaranteed that the given graph is a directed acyclic graph.
| Subtask ID | Special Property | Score | ||||
|---|---|---|---|---|---|---|
| Subtask 1 | None | |||||
| Subtask 2 | ||||||
| Subtask 3 | ||||||
| Subtask 4 | is prime | |||||
| Subtask 5 | None | |||||
| Subtask 6 | ||||||
| Subtask 7 | ||||||
Translated by ChatGPT 5