Solution for Message Routes #118
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎯 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
1finds shortest distances (in edges) to every reachable node because it explores level-by-level.parent[node]while doing BFS to reconstruct the shortest path from1tonafter traversal.distance[n]remains INF, nodenis unreachable → outputIMPOSSIBLE.distance[n] + 1.✅ Checklist
Please ensure your PR meets these requirements:
Code Quality
adj,distance,parent,q)IMPOSSIBLEwhen disconnected, handlesn = 1)Testing
N ≤ 1e5,M ≤ 2e5)Documentation
graphs/)message_route.cpp)Style Guide
#include <bits/stdc++.h>)ios::sync_with_stdio(false); cin.tie(nullptr);)long longnot needed)🏷️ Type of Change
🧪 Testing Details
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)
📎 Additional Notes
parent[]andreverse()to output1 → ... → n.For Maintainers:
graphs,good first issue,hacktoberfest)