#K33447. City Connectivity Check
City Connectivity Check
City Connectivity Check
You are given a city consisting of n islands (numbered from 1 to n) and m bridges. Each bridge connects two distinct islands. A bridge can be either unidirectional (denoted by 'D') or bidirectional (denoted by 'U').
Your task is to determine whether the entire city is strongly connected. This means that for every island i (where \(1 \le i \le n\)), it must be possible to reach island i from island 1 and also from island i one can reach island 1. In other words, the connectivity condition can be stated as:
\[ \forall i \in \{1,2,\dots,n\}, \quad \text{reachable}(1, i) \; \wedge \; \text{reachable}(i, 1) \]
If the condition above holds, print Connected
; otherwise, print Not Connected
.
inputFormat
The input consists of one or more test cases. Each test case begins with a line containing two integers n and m (\(1 \le n \le 10^5\), \(0 \le m \le 10^5\)), where n is the number of islands and m is the number of bridges. This is followed by m lines, each containing two integers u and v and a character t. The values u and v denote the islands connected by the bridge, and t is either 'U' for a bidirectional bridge or 'D' for a unidirectional bridge (from u to v).
The input is terminated by a test case where both n and m are 0. This test case should not be processed.
Note: Input is provided via standard input (stdin).
outputFormat
For each test case, output a single line containing either Connected
if the city's islands are strongly connected, or Not Connected
otherwise.
Note: Output should be printed to standard output (stdout).
## sample3 3
1 2 U
2 3 D
3 1 D
0 0
Connected
</p>