Haskell
View as PDFIndra is excited, because they have just learned Haskell! Now that they have acquired the skills of
a functional programming god, they want to start working on a project made of tasks. There are
ways to move between tasks while working: each move goes from some task
to another task
and changes Indra's productivity by an integer
. Positive values help; negative values
represent fighting compiler errors.
Indra can start at any task and keep following moves, possibly using the same move more than once. Josh has claimed that no matter how Indra works, they can never return to a previously visited task with a strictly positive total productivity change along the way. In other words, Josh claims there is no productive loop in the project.
Help Indra check whether Josh is wrong.
A move from a task to itself counts as a loop. There may be several different moves between the same pair of tasks.
Input
The first line contains two integers and
.
The next lines each contain three integers
,
, and
, describing a move from task
to task
that changes productivity by
.
Tasks are numbered through
.
Output
Print YES if there exists a sequence of moves that starts and ends at the same task and has
strictly positive total productivity change. Otherwise, print NO.
Constraints
Example 1
Input
3 6
3 1 -80
1 3 -58
1 3 81
2 3 -81
2 1 24
2 1 -55
Output
YES
Explanation
Starting at task , Indra can move
with productivity
, then
with
productivity
, returning to task
with total change
.
Example 2
Input
2 2
1 2 10
2 1 -10
Output
NO
Explanation
The only way to return to the starting task has total productivity change , which is not
strictly positive.
Comments