There is a bi-directional graph with
n
vertices, where each vertex is labeled from0
ton - 1
(inclusive). The edges in the graph are represented as a 2D integer arrayedges
, where eachedges[i] = [ui, vi]
denotes a bi-directional edge between vertexui
and vertexvi
. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
You want to determine if there is a valid path that exists from vertexsource
to vertexdestination
.
Given edges and the integersn
,source
, anddestination
, returntrue
if there is a valid path fromsource
todestination
, orfalse
otherwise.
有一個包含
n
個頂點的雙向圖,每個點被標記成0
到n - 1
(包含)。圖中的邊使用二維整數陣列deges
表示,edges[i] = [ui, vi]
表示一條在頂點ui
和vi
的雙向邊。每個頂點對之間只會有一條邊,且不會有點自己連結到自己。
你需要判斷從頂點source
到destnation
之間是否存在一條合法路徑。
給予edges
和整數n
、source
和destination
,回傳true
如果source
到destination
之間有一條合法路徑,否則回傳false
。
touched
屬性,代表該點是否已經走過
vector<node> stack
去記錄目前的待走節點
LeetCode
C++