Check connectivity with BFS/DFS, then check for cycles separately.
This requires two passes. Also, cycle detection in undirected graphs needs care to avoid false positives from back-edges to parent.
Can you check both conditions in one pass?
The trap
Check connectivity with BFS/DFS, then check for cycles separately.
This requires two passes. Also, cycle detection in undirected graphs needs care to avoid false positives from back-edges to parent.
Can you check both conditions in one pass?