Problem 501118 · easy · Level 05 Advanced Algorithms & Graphs

Every Edge Is Counted Twice

graphs · degree · handshaking lemma

The degree of a node is the number of edge ends meeting it; a self-loop contributes 2. Adding up the degrees counts each edge from both ends, which is the handshaking lemma:

\sum_{v \in V} \deg(v) \;=\; 2|E|

An immediate consequence is that a graph can never have an odd number of odd-degree nodes.

Write degree_report(n, edges) for an undirected graph on nodes 0 .. n-1, returning the pair (degree_sum, odd_nodes).

Examples

Input:  n = 4, edges = [(0, 1), (1, 2), (2, 0), (2, 3)]
Output: (8, 2)
Explanation: four edges, so the degrees add up to 8; nodes 2 and 3 have odd degree.

Input:  n = 3, edges = []
Output: (0, 0)

Constraints

  • 1 <= n <= 10**5, 0 <= len(edges) <= 2 * 10**5
  • Edges may repeat, and (v, v) is a self-loop worth 2 to that node's degree.

Goals

  • Compute the degree of every node from an edge list
  • Check the handshaking lemma on a concrete graph
  • Notice that the number of odd-degree nodes is always even
Starting Python…