Problem 575099 · easy · Level 05 Advanced Algorithms & Graphs

Can the Pen Draw It Without Lifting?

graphs · Eulerian path · degree

A trail that uses every edge exactly once is Eulerian. For an undirected graph whose edges all lie in one connected piece:

  • every node of even degree → there is an Eulerian cycle, which ends where it started;
  • exactly two nodes of odd degree → there is an Eulerian path between those two;
  • anything else → there is none.

If the edges fall into two or more separate pieces no trail can cover them all, whatever the degrees. Nodes with no edges at all are ignored. A graph with no edges counts as having an Eulerian cycle: the empty one.

Write euler_kind(n, edges) returning "cycle", "path" or "none".

Examples

Input:  n = 3, edges = [(0, 1), (1, 2), (2, 0)]
Output: "cycle"

Input:  n = 4, edges = [(0, 1), (2, 3)]
Output: "none"
Explanation: the degrees are all odd in pairs, but the two edges are in different pieces.

Constraints

  • 1 <= n <= 10**5, 0 <= len(edges) <= 2 * 10**5
  • Self-loops add 2 to a node's degree and keep it in its own piece.

Goals

  • Apply the Eulerian conditions for an undirected graph
  • Remember that connectivity is as necessary as the degree parity
  • Ignore isolated nodes, which take part in no trail
Starting Python…