fork download
  1. #include <iostream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. const int MAX = 100;
  8.  
  9. vector<int> graph[MAX];
  10. bool visited[MAX];
  11. int priorityLevel[MAX];
  12.  
  13. void bfs(int start, int nodes) {
  14. if (start < 0 || start >= nodes) {
  15. cout << "Invalid burst location\n";
  16. return;
  17. }
  18.  
  19. queue<int> q;
  20. q.push(start);
  21. visited[start] = true;
  22.  
  23. cout << "Affected Areas:\n";
  24.  
  25. while (!q.empty()) {
  26. int node = q.front();
  27. q.pop();
  28.  
  29. cout << "Area " << node << " (Priority: " << priorityLevel[node] << ")\n";
  30.  
  31. for (int neighbor : graph[node]) {
  32. if (!visited[neighbor]) {
  33. visited[neighbor] = true;
  34. q.push(neighbor);
  35. }
  36. }
  37. }
  38. }
  39.  
  40. int main() {
  41. int nodes, edges;
  42. cin >> nodes >> edges;
  43.  
  44. if (nodes > MAX || nodes <= 0) {
  45. cout << "Invalid number of nodes\n";
  46. return 1;
  47. }
  48.  
  49. // Reset arrays
  50. for(int i = 0; i < MAX; i++) {
  51. visited[i] = false;
  52. graph[i].clear();
  53. priorityLevel[i] = 0;
  54. }
  55.  
  56. for (int i = 0; i < edges; i++) {
  57. int u, v;
  58. cin >> u >> v;
  59. if (u >= 0 && u < nodes && v >= 0 && v < nodes) {
  60. graph[u].push_back(v);
  61. graph[v].push_back(u);
  62. }
  63. }
  64.  
  65. for (int i = 0; i < nodes; i++) {
  66. cin >> priorityLevel[i];
  67. }
  68.  
  69. int burstLocation;
  70. cin >> burstLocation;
  71.  
  72. bfs(burstLocation, nodes);
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.01s 5288KB
stdin
45
stdout
Invalid burst location