Skip to main content

Practice · 5 of 5

Is It a Tree?

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Decide whether an undirected graph with n vertices and m edges is a tree: connected AND exactly n-1 edges (no cycles). **Input:** Line 1: n m (1 <= n <= 100000, 0 <= m <= 200000). Next m lines: u v. **Output:** Print YES if the graph is a tree, otherwise NO. **Example:** 3 2 / 1 2 / 2 3 -> YES.

Back to lesson: Practice: Graph Theory Problem Set