Graph Node Degree Sum
EasyPractice library · +10 XPgraphsbfsdfs
Given an undirected graph with N nodes and M edges, calculate the sum of the degrees of all nodes. The degree of a node is the number of edges connected to it.
Input
The first line contains two integers N and M, the number of nodes and edges respectively. The next M lines each contain two integers u and v, representing an edge between node u and node v. Nodes are numbered from 1 to N.
Output
Print a single integer, the sum of the degrees of all nodes in the graph.
Constraints
1 <= N <= 1000; 0 <= M <= 1000; 1 <= u, v <= N; u != v
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
4 3 1 2 2 3 3 4
Output
6
Graph with 4 nodes and 3 edges. Each edge contributes 2 to the total degree sum. 2 * 3 = 6.
Example 2
Input
3 3 1 2 2 3 1 3
Output
6
Complete graph K3. 3 edges, so total degree sum is 2 * 3 = 6.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
4 3 1 2 2 3 3 4
Example 2 input
3 3 1 2 2 3 1 3