Skip to content

Conversation

@vingoel26
Copy link
Contributor

@vingoel26 vingoel26 commented Oct 21, 2025

🎯 Problem Information

Problem Name: Message Route
Category: Graph (Shortest Path, BFS)
CSES Link: https://cses.fi/problemset/task/1667
Difficulty: [Medium]
#111

📝 Description

This PR adds a clean, well-documented C++ solution for the Message Route problem from CSES.
The program determines whether a message can be sent from computer 1 to computer n in an undirected network, and if so prints the shortest route (in terms of number of computers visited). If there is no route, it prints IMPOSSIBLE.


🧩 Solution Approach

  • Algorithm Used: Breadth-First Search (BFS) on an unweighted undirected graph.
  • Time Complexity: O(N + M) — each vertex and edge is processed at most once.
  • Space Complexity: O(N + M) — adjacency list + distance + parent arrays.
  • Key Insights:
    • BFS from node 1 finds shortest distances (in edges) to every reachable node because it explores level-by-level.
    • Track parent[node] while doing BFS to reconstruct the shortest path from 1 to n after traversal.
    • If distance[n] remains INF, node n is unreachable → output IMPOSSIBLE.
    • Path length reported should be number of nodes on the route = distance[n] + 1.

✅ Checklist

Please ensure your PR meets these requirements:

Code Quality

  • Solution follows the required template format
  • Code is clean and well-commented (inline comments explain BFS, distance and parent usage)
  • Variable names are descriptive (adj, distance, parent, q)
  • Proper error/edge-case handling (IMPOSSIBLE when disconnected, handles n = 1)

Testing

  • Solution passes all CSES test cases
  • Tested with custom edge cases (disconnected graphs, single node, multiple shortest paths)
  • Handles large input constraints efficiently (N ≤ 1e5, M ≤ 2e5)
  • No runtime errors or timeouts (uses iterative BFS; no recursion)

Documentation

  • Added solution to appropriate category folder (graphs/)
  • File name follows naming convention (message_route.cpp)
  • Added brief comment explaining the approach (top of file header block)
  • Updated any relevant documentation if needed

Style Guide

  • Uses required header (#include <bits/stdc++.h>)
  • Includes fast I/O optimization (ios::sync_with_stdio(false); cin.tie(nullptr);)
  • Uses appropriate data types (int acceptable here; long long not needed)
  • Follows competitive programming best practices (1-indexed adjacency, INF sentinel)

🏷️ Type of Change

  • 🐛 Bug fix (fixes an existing solution)
  • ✨ New problem solution
  • 📚 Documentation improvement
  • 🔧 Code optimization/refactoring
  • 🎃 Hacktoberfest contribution

🧪 Testing Details

  • Verified correctness on CSES judge (Accepted).
  • Ran multiple custom tests including edge and stress cases (random graphs up to limits).
  • Verified no stack overflow (BFS uses queue), and memory usage is within bounds.

Test Cases Used:
Input:
5 5
1 2
1 3
2 4
3 4
4 5

Expected Output:
4
1 2 4 5

Actual Output:
4
1 2 4 5

Input:
4 2
1 2
3 4

Expected Output:
IMPOSSIBLE

Actual Output:
IMPOSSIBLE

Input:
1 0

Expected Output:
1
1

Actual Output:
1
1


📸 Screenshots (if applicable)

image

📎 Additional Notes

  • The implementation is iterative (BFS) and avoids recursion limits.
  • Reconstructing the path uses parent[] and reverse() to output 1 → ... → n.
  • The code prints the number of nodes on the route followed by the route nodes separated by spaces.

For Maintainers:

  • Code review completed
  • Solution verified on CSES judge
  • Documentation updated if needed
  • Labels applied appropriately (suggested: graphs, good first issue, hacktoberfest)

@ks-iitjmu ks-iitjmu added enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest-accepted Approved PRs for Hacktoberfest hacktoberfest_2025 Hacktoberfest 2025 specific contributions medium Medium difficulty problems category: graph Graph algorithm problems labels Oct 21, 2025
@ks-iitjmu ks-iitjmu linked an issue Oct 21, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu merged commit 9039c34 into ks-iitjmu:main Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: graph Graph algorithm problems enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest_2025 Hacktoberfest 2025 specific contributions hacktoberfest-accepted Approved PRs for Hacktoberfest medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add solution for Message Route

2 participants