-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,96 @@ | ||||||
| /* | ||||||
| Problem: Building Roads | ||||||
| Category: Graph Algorithms (Undirected Graph) | ||||||
| Difficulty: Medium | ||||||
| Time Complexity: O(n + m) | ||||||
| Space Complexity: O(n + m) | ||||||
|
|
||||||
| Approach: | ||||||
| Use graph traversal to find connected components. Pick one representative node from each | ||||||
| component; the minimum number of roads required to connect all components is (components - 1). | ||||||
| Connect the representatives in a simple chain: (rep[0], rep[1]), (rep[1], rep[2]), ... | ||||||
|
|
||||||
| Key Insights: | ||||||
| - Any connected component can be represented by any one of its vertices. | ||||||
| - Connecting k components requires exactly k-1 edges; connecting representatives in a chain | ||||||
| achieves this minimum. | ||||||
| - Use iterative DFS (explicit stack) to avoid recursion-depth issues on large inputs. | ||||||
| - Handle edge cases: n = 1, m = 0, already connected graphs. | ||||||
|
|
||||||
| Notes: | ||||||
| - Nodes are 1-indexed (as per CSES problem statement). | ||||||
| - Fast I/O is used for competitive programming. | ||||||
| */ | ||||||
|
|
||||||
| #include <bits/stdc++.h> | ||||||
| using namespace std; | ||||||
|
|
||||||
| int main() { | ||||||
| ios::sync_with_stdio(false); | ||||||
| cin.tie(nullptr); | ||||||
|
|
||||||
| // Read number of cities (nodes) and existing roads (edges) | ||||||
| int num_nodes, num_edges; | ||||||
| if (!(cin >> num_nodes >> num_edges)) return 0; | ||||||
|
|
||||||
| // Adjacency list for the undirected graph (1-indexed) | ||||||
| vector<vector<int>> adjacency(num_nodes + 1); | ||||||
| for (int i = 0; i < num_edges; ++i) { | ||||||
| int u, v; | ||||||
| cin >> u >> v; | ||||||
| // add both directions because graph is undirected | ||||||
| adjacency[u].push_back(v); | ||||||
| adjacency[v].push_back(u); | ||||||
| } | ||||||
|
|
||||||
| // visited[i] == true means node i has been visited in component discovery | ||||||
| vector<char> visited(num_nodes + 1, false); | ||||||
|
||||||
| vector<char> visited(num_nodes + 1, false); | |
| vector<bool> 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.
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; |
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.