Skip to main content
Back to problems
Codeforces
Medium
Graphs
Greedy
Math
Road Construction

Build a road network by choosing one city as a hub and connecting every other city to it so that the total construction cost is small and the road set is valid.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Road Construction

You are given an undirected weighted graph with nn cities and mm possible roads. Your task is to decide which roads to build so that the cities become connected, following the problem’s construction rule.

A common formulation of this task is to pick a suitable central city and then connect other cities in a way that satisfies the required structure while keeping the construction valid. The answer is usually a list of roads to build, not a shortest-path computation.

Your goal is to output one valid construction if it exists, using the graph information provided in the input.

Input Format

  • The first line contains two integers nn and mm.
  • Each of the next mm lines describes a road between two cities.
  • Depending on the exact variant, each road may also include a cost or weight.

Output Format

  • Print a valid set of roads to build, or the required arrangement specified by the task.
  • If multiple answers are possible, any valid one may be accepted.

Constraints

  • 1n,m1 \le n, m are within standard competitive-programming limits.
  • The graph description is assumed to fit in memory.
  • Exact numeric limits are not available from the source metadata.
Examples
Sample cases returned by the problem API.

Example 1

Input

4 3
1 2
1 3
1 4

Output

1 2
1 3
1 4

Explanation

This is already a valid road construction centered at city 1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.