A flower is an undirected graph with 2n vertices, having exactly one cycle of length n, and where each node in the cycle is connected to exactly one node of degree 1.
Example:
Detect that it is a flower with O(n) time complexity.
My attempt:
Use DFS to detect the cycle and store the start and end points. Then backtrace on that cycle.
What I don't have figured out yet is this: while backtracing I need a function which can decide a node is valid. Can someone help me out with this?
Also DFS is V + E, so how can I make it O(V)?
bool dfs(v, prev):
vis[v] = true
for u in adj[v]:
if prev == u:
continue
if vis[u]:
start = u
end = v
return true
parent[u] = v
if dfs(u, parent[u]):
return true
return false
while start != end:
# check that start is valid here
start = parent[start]
