Problem 268
Node With Maximum Degree
Easy · 10 pts · Graph
Problem
You are given an undirected graph with N nodes labelled 1 to N and M edges. The degree of a node is the number of edges that touch it. Print the node with the highest degree together with that degree. If several nodes share the highest degree, print the one with the smallest label. A graph without edges has every degree equal to 0, so the answer is node 1 with degree 0. Keep a counter per node, add 1 to both ends of every edge, then scan the counters from node 1 upwards.
Input
First line: integers N (nodes, labelled 1..N) and M (edges). Next M lines: an edge u v.
Output
Print the node and its degree, separated by a space.
Constraints
1 <= N <= 100000; 0 <= M <= 200000; no self-loops or repeated edges
Example input
4 3 1 2 2 3 2 4
Example output
2 3
Explanation
Node 2 is an endpoint of all three edges (1-2, 2-3 and 2-4), so its degree is 3. Every other node has degree 1. The output is 2 3.