fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. int n, m;
  11.  
  12. if (!(cin >> n >> m)) return 0;
  13.  
  14. map<string, vector<string>> adjList;
  15. map<string, int> dist;
  16. map<string, string> parent;
  17. map<string, bool> visited;
  18.  
  19. for (int i = 0; i < m; i++) {
  20. string u, v;
  21. cin >> u >> v;
  22.  
  23. if (u == v) {
  24. adjList[u];
  25. } else {
  26. adjList[u].push_back(v);
  27. adjList[v].push_back(u);
  28. }
  29. }
  30.  
  31. string tag, assemblyPoint;
  32. cin >> tag >> assemblyPoint;
  33. adjList[assemblyPoint];
  34.  
  35. for (auto const& pair : adjList) {
  36. visited[pair.first] = false;
  37. dist[pair.first] = -1;
  38. }
  39.  
  40. queue<string> q;
  41. dist[assemblyPoint] = 0;
  42. visited[assemblyPoint] = true;
  43. q.push(assemblyPoint);
  44.  
  45. while (!q.empty()) {
  46. string current = q.front();
  47. q.pop();
  48.  
  49. for (const string& neighbor : adjList[current]) {
  50. if (!visited[neighbor]) {
  51. visited[neighbor] = true;
  52. dist[neighbor] = dist[current] + 1;
  53. parent[neighbor] = current;
  54. q.push(neighbor);
  55. }
  56. }
  57. }
  58.  
  59. for (auto const& pair : adjList) {
  60. string point = pair.first;
  61.  
  62. if (point == assemblyPoint) continue;
  63.  
  64. if (!visited[point]) {
  65. cout << "Point: " << point << " Reachable: No Steps: - Route: No path exists\n";
  66. } else {
  67. cout << "Point: " << point << " Reachable: Yes Steps: " << dist[point] << " Route: ";
  68.  
  69. string curr = point;
  70. while (curr != assemblyPoint) {
  71. cout << curr << " -> ";
  72. curr = parent[curr];
  73. }
  74. cout << assemblyPoint << "\n";
  75. }
  76. }
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty