#K95517. Wizard's Village Journey

    ID: 38881 Type: Default 1000ms 256MiB

Wizard's Village Journey

Wizard's Village Journey

A wizard wishes to travel from Village 1 to every other village in the kingdom. There are n villages (numbered 1 through n) and m one-way pathways. Each pathway is described by three integers u, v, and w, representing a directed edge from village u to village v which can be used if and only if \(w > 0\). The wizard can only travel along the available pathways. Your task is to determine whether the wizard can reach every village starting from Village 1.

Input: The first line contains two integers \(n\) and \(m\). The next \(m\) lines each contain three integers \(u\), \(v\), and \(w\) describing a pathway.

Output: Print "YES" if the wizard can travel to every village starting from Village 1, or "NO" otherwise.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two space-separated integers \(n\) and \(m\), representing the number of villages and the number of pathways.
  • The next \(m\) lines each contain three space-separated integers \(u\), \(v\), and \(w\). Each line represents a one-way pathway from village \(u\) to village \(v\) that can be used if \(w > 0\).

outputFormat

The output is printed to stdout. It is a single line containing either "YES" if the wizard can travel from Village 1 to every other village, or "NO" otherwise.

## sample
5 6
1 2 3
1 3 1
2 3 2
2 4 1
3 4 1
4 5 2
YES