#K84387. Road Network Safety Check
Road Network Safety Check
Road Network Safety Check
Given a road network of cities and roads, determine whether the network remains connected when any single road is removed. Each test case provides n cities and m roads. Every road connects two cities and has an associated weight.
A road is considered critical (or a bridge) if its removal disconnects the network. In other words, if there exists at least one road whose removal causes the graph to become disconnected, the network is deemed Disconnected; otherwise, it is Safe.
Mathematically, a graph is connected if for every pair of vertices \(u\) and \(v\), there exists a path connecting them. The removal of a bridge breaks this condition.
inputFormat
The input consists of multiple test cases. Each test case begins with two integers n and m, where n is the number of cities (nodes) and m is the number of roads (edges). This is followed by m lines, each containing three integers u, v, and w representing a road that connects cities u and v with weight w. The input terminates with a line containing "0 0".
outputFormat
For each test case, output a single line: Safe
if the removal of any one road does not disconnect the network, or Disconnected
if at least one road exists whose removal disconnects the network.
4 5
1 2 3
1 3 4
2 3 5
2 4 6
3 4 2
0 0
Safe