Back to problems
COMP2521
Graph: Maximum In/Out Degree Difference
Graph: maximum in/out degree difference
Read a directed graph: vertices 0 … V-1, E directed edges (u v means u → v). For each vertex i, compute its out-degree (edges leaving i) and in-degree (edges entering i), and print the maximum of |out − in| over all vertices.
Complete maxDegreeDiff.
Pitfall (straight from the bug lab): out-degree and in-degree go opposite ways — foru → v, incrementu's out-degree andv's in-degree; do not swap them. The answer is the maximum of the absolute difference.
Input
- Line 1:
V E. - Next
Elines:u v, a directed edgeu → v.
Output
- One line: the maximum of
|out − in|over all vertices (0 if empty / no edges).
Example: input 4 3 / 0 1 / 0 2 / 0 3 -> 3 (vertex 0 has out 3, in 0; diff 3 is the largest).
Samples
stdin
4 3 0 1 0 2 0 3
expected
3
stdin
3 3 0 1 1 2 2 0
expected
0
c-dcc
14