One morning, you wake up to find that it snowed hard overnight, and that school buses have now been cancelled. To celebrate, you decide to go tobogganing at the giant hill in your neighbourhood. At the top of the hill, you see that there are several possible paths down the hill, and that each path may split, merge, or intersect with others at "scenic points". More specifically, there are
You want to take the longest path down (from point
Input Specification
The first line of input will contain
Note: paths are not bidirectional. It would not be possible to slide up a hill.
Constraints
For all subtasks:
Subtask 1 [10%]
Subtask 2 [90%]
Output Specification
Output the total distance of the longest route possible and the number of scenic points on that route, separated by a space. Output
Sample Input 1
6 7
1 2 1
1 3 2
2 4 2
2 5 2
3 6 2
4 6 2
5 6 1
Sample Output 1
5 4
Explanation for Sample Output 1
Path
Path
Path
Therefore, the path that should be chosen is the first one, as it is the longest.
Sample Input 2
5 5
1 2 1
2 3 2
2 4 3
3 1 2
4 5 1
Sample Output 2
-1
Explanation for Sample Output 2
There is a cycle (
Comments
For the first test case, shouldn't only nodes 1, 2, and 6 be scenic points?
Is it possible that a 32-bit integer is not sufficient enough to store the distances? Please specify.
Why not try calculating the worst-case distance yourself? From the constraints, we see that
is at most 
and that 
is at most 
. If each path was of maximum length, then the greatest total distance would be 
(
paths of length 
). However, this isn't logical, because there aren't that many scenic points, so revisiting one would create a cycle. Thus, the greatest total distance (a line from point 
to point 
) would be 
, which is less than 
.
Problems that don't specify whether or not you need 64-bit variables will often give you important constraints, allowing you to determine that yourself. Just putting this here for anyone who doesn't know how to do this. Correct me if I'm wrong anywhere.