Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. ifstream input("pathbge1.in", ios_base::in);
  8. ofstream output("pathbge1.out", ios_base::out);
  9.  
  10. const int MARKED = INT32_MAX;
  11. int main(){
  12.     int n,m,b,e;
  13.     input >> n >> m;
  14.     vector<int> graph[n];
  15.     for (int i = 0; i < m; i++) {
  16.         input >> b >> e;
  17.         --b;
  18.         --e;
  19.         graph[e].push_back(b);
  20.         graph[b].push_back(e);
  21.     }
  22.  
  23.     queue<int> queue;
  24.     queue.push(0);
  25.     vector<int> cur(n, MARKED);
  26.     cur[0] = 0;
  27.  
  28.     while (!queue.empty()){
  29.         int v = queue.front();
  30.         queue.pop();
  31.         for (auto path : graph[v]) {
  32.             if(cur[path] == MARKED) {
  33.                 cur[path] = cur[v] + 1;
  34.                 queue.push(path);
  35.             }
  36.         }
  37.     }
  38.     for (auto path : cur) {
  39.         output << path << " ";
  40.     }
  41.     return 0;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement