Euler: count the odd-degree vertices
The key quantity for "can it be drawn in one stroke" is the number of odd-degree vertices. Read an undirected graph and print how many vertices have an odd degree.
Complete it: add 1 to both endpoints' degree for each edge, then count those with an odd degree.
Hint: an undirected edge
u–vdoesdeg[u]++anddeg[v]++. A vertex withdeg[i] % 2 == 1is odd. Fact: by the handshake lemma, the count of odd-degree vertices is always even (0, 2, 4, ...).
Input
- Line 1:
V E(vertices, edges). - Next
Elines:u v, an undirected edge (Emay be 0).
Output
- One line: the number of odd-degree vertices.
Example: input 4 3 / 0 1 / 1 2 / 2 3 -> 2 (vertices 0 and 3 have degree 1; 1 and 2 have degree 2).