#C10776. Bus Route Connectivity
Bus Route Connectivity
Bus Route Connectivity
In this problem, you are given a network of bus stops and bus routes. The bus stops are numbered from 1 to n, and you are provided with m bus routes. Each route is described by three integers u, v, and d, where:
- u and v are the endpoints of the route.
- d indicates the direction of the route: if d = 0 then the route is bidirectional, and if d = 1 then the route is one-way from u to v.
Your task is to determine whether there exists a path from bus stop 1 to bus stop n.
Formally, you are given integers $n$ and $m$, followed by $m$ triples $(u, v, d)$. Construct a graph with the following rules:
- If $d = 0$, add an edge from $u$ to $v$ and an edge from $v$ to $u$.
- If $d = 1$, add a directed edge from $u$ to $v$ only.
Then, using a graph traversal algorithm (such as Breadth First Search), determine if there is a path from node 1 to node $n$. Output Yes
if such a path exists, otherwise output No
.
inputFormat
The input is given from stdin and consists of:
- A line containing two integers $n$ and $m$, where $1 \leq n \leq 10^5$ and $0 \leq m \leq 10^5$.
- $m$ lines each containing three integers $u$, $v$, and $d$ representing a bus route.
It is guaranteed that $1 \leq u,v \leq n$ and $d \in \{0, 1\}$.
outputFormat
Print to stdout a single line with the answer: Yes
if there is a path from bus stop 1 to bus stop $n$, or No
otherwise.
4 4
1 2 0
2 3 1
3 4 0
2 4 1
Yes