-
Notifications
You must be signed in to change notification settings - Fork 35
feature/Building-Roads #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a C++ solution for the "Building Roads" problem from CSES, which finds the minimum number of roads needed to connect all cities in a graph. The solution uses Depth-First Search (DFS) to identify connected components and then connects their representatives sequentially.
- Uses iterative DFS to find connected components and avoid recursion depth issues
- Calculates minimum roads needed as (number of components - 1)
- Outputs specific road connections between component representatives
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| // Read number of cities (nodes) and existing roads (edges) | ||
| int num_nodes, num_edges; | ||
| if (!(cin >> num_nodes >> num_edges)) return 0; |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The input validation should handle the case where input fails more explicitly. Consider using proper error handling or removing this check entirely since competitive programming problems typically guarantee valid input.
| if (!(cin >> num_nodes >> num_edges)) return 0; | |
| cin >> num_nodes >> num_edges; |
| } | ||
|
|
||
| // visited[i] == true means node i has been visited in component discovery | ||
| vector<char> visited(num_nodes + 1, false); |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using vector<char> instead of vector<bool> is unconventional for boolean flags. Consider using vector<bool> which is specifically designed for boolean values and provides space optimization.
| vector<char> visited(num_nodes + 1, false); | |
| vector<bool> visited(num_nodes + 1, false); |
| int component_count = static_cast<int>(representatives.size()); | ||
|
|
||
| // Minimum roads required to connect all components = components - 1 | ||
| int roads_to_add = max(0, component_count - 1); |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The max(0, component_count - 1) is unnecessary since component_count will always be at least 1 (when num_nodes >= 1). This can be simplified to component_count - 1.
| int roads_to_add = max(0, component_count - 1); | |
| int roads_to_add = component_count - 1; |
🎯 Problem Information
Problem Name: Building Roads
Category: Graphs (Connected Components, DFS)
CSES Link: https://cses.fi/problemset/task/1666/
Difficulty: Medium
#110
📝 Description
This PR adds the C++ solution for the Building Roads problem from the CSES problem set.
The task is to determine the minimum number of new roads required to make all cities connected and list the specific roads that need to be built.
🧩 Solution Approach
✅ Checklist
Please ensure your PR meets these requirements:
Code Quality
Testing
Documentation
graphs/)building_roads.cpp)Style Guide
#include <bits/stdc++.h>)long longfor large numbers)🏷️ Type of Change
🧪 Testing Details
Test Cases Used:
Input:
6 3
1 2
2 3
4 5
Expected Output:
2
3 4
5 6
Explanation:
There are 3 connected components: {1, 2, 3}, {4, 5}, {6}.
We need to add 2 roads to connect them all, e.g., (3–4) and (5–6).
📸 Screenshots (if applicable)
verified on CSES judge with AC (Accepted).

📎 Additional Notes
visitedarray and adjacency list.For Maintainers: