#K5796. Path Existence in an Undirected Graph
Path Existence in an Undirected Graph
Path Existence in an Undirected Graph
You are given an undirected graph \(G = (V, E)\) with weighted edges. Although each edge has an associated weight, the weight is irrelevant for determining connectivity. Your task is to determine if there exists a path between two specified nodes in the graph. The graph is given as an adjacency list; for each edge between nodes u and v with weight w, you should treat it as an undirected edge (i.e. add both u → v and v → u). Note that if the start and end nodes are the same, a path exists by definition.
Example:
Input: 4 4 1 4 1 2 5 1 3 10 2 4 1 3 4 2</p>Output: True
inputFormat
The first line of input contains four integers: \(n\), \(m\), \(s\), and \(t\) where:
- \(n\) is the number of nodes.
- \(m\) is the number of edges.
- \(s\) is the starting node.
- \(t\) is the target node.
The following \(m\) lines each contain three integers \(u\), \(v\) and \(w\) indicating there is an undirected edge between node \(u\) and node \(v\) with weight \(w\). Note that the weight \(w\) does not affect connectivity.
outputFormat
Output a single line: True
if there is a path between node \(s\) and \(t\), and False
otherwise.
4 4 1 4
1 2 5
1 3 10
2 4 1
3 4 2
True