Let's look at the maximum possible congestion score. If no cities are upgraded to "VIP" status, every road has a score of . Thus, the initial total score of the network is 3 \cdot M.
What happens when Fady upgrades a single city to "VIP"?
Every road connected to city changes its state from (Standard–Standard) to (VIP–Standard). Its score drops from to . This decreases the total network score by for each road connected to .
If the city at the other end of a road is also upgraded to "VIP", the road state changes from (VIP–Standard) to (VIP–VIP). Its score drops from to . This decreases the total score by another .
Key Observation: Upgrading a city to "VIP" status independently reduces the total network score by exactly for every road connected to it. In graph terms, upgrading node reduces the total score by .
Therefore, to minimize the total congestion score, we must maximize the total reduction. We calculate the degree of all cities, sort them in descending order, and select the top cities.
Complexity Analysis:
Time Complexity: \mathcal{O}(N \log N + M) per test case due to sorting the node degrees.
Space Complexity: \mathcal{O}(N + M) to store the adjacency list / degree table.