#K73697. Safest Route System
Safest Route System
Safest Route System
You are given a network of planets connected by bidirectional routes. Each route has an associated safety rating. Your task is to determine if it is possible to connect all the planets by selecting some of these routes such that every planet is reachable from every other planet, i.e. a spanning tree exists.
This problem can be solved using Kruskal's algorithm to compute a Minimum Spanning Tree (MST). If the MST contains exactly \(n-1\) edges (where \(n\) is the number of planets), then the network is fully connected and the safest route system can be formed; otherwise, it cannot.
Note: Input is provided via stdin
and the result should be printed to stdout
.
inputFormat
The first line of input contains two integers \(n\) and \(e\), where \(n\) is the number of planets (nodes) and \(e\) is the number of available routes (edges). The next \(e\) lines each contain three integers \(u\), \(v\), and s representing a bidirectional route between planet \(u\) and planet \(v\) with a safety rating of s.
It is guaranteed that the planets are labeled from 1 to \(n\).
outputFormat
Print YES
if it is possible to form a safest route system that connects all planets (i.e. a spanning tree exists); otherwise, print NO
.
5 6
1 2 5
1 3 10
2 3 3
2 4 7
3 5 9
4 5 2
YES
</p>