#C2996. Reachability in an Undirected Graph
Reachability in an Undirected Graph
Reachability in an Undirected Graph
You are given an undirected graph with n nodes and m edges. The graph is described by a list of edges connecting pairs of nodes. You are also given a starting node.
Your task is to determine whether every node in the graph is reachable from the given starting node. Two nodes are considered connected if there exists a path between them. Formally, if we let \(G=(V,E)\) be an undirected graph with \(|V|=n\) and if the starting node can reach every node in V, then output reachable
; otherwise, output unreachable
.
The input is read from stdin and the result should be written to stdout.
inputFormat
The first line contains two integers n and m representing the number of nodes and the number of edges in the graph respectively. The following m lines each contains two integers u and v representing an undirected edge between node u and node v. The last line contains an integer which is the starting node.
Constraints:
- 1 \(\leq n \leq 10^5\)
- 0 \(\leq m \leq 10^5\)
- 1 \(\leq u, v, \text{starting node} \leq n\)
outputFormat
Output a single string: reachable
if all the nodes are reachable from the starting node, otherwise output unreachable
.
5 4
1 2
2 3
3 4
4 5
1
reachable