Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. vector<int> g[30005];
  9. int res[30005];
  10.  
  11. int main()
  12. {
  13.     ifstream fin("pathbge1.in");
  14.     ofstream fout("pathbge1.out");
  15.     int n, s = 1, m, u, v;
  16.     fin >> n >> m;
  17.  
  18.     for (int i = 0; i < m; i++)
  19.     {
  20.         fin >> u >> v;
  21.         g[v].push_back(u);
  22.         g[u].push_back(v);
  23.     }
  24.     queue<int> q;
  25.     q.push(s);
  26.     while (!q.empty())
  27.     {
  28.         u = q.front();
  29.         q.pop();
  30.         for(int i = 0; i < g[u].size(); i++)
  31.         {
  32.             if (!res[v] && v!=s)
  33.             {
  34.                 res[v] = res[u] + 1;
  35.                 q.push(v);
  36.             }
  37.         }
  38.     }
  39.  
  40.     for (int i = 1; i <= n; i++)
  41.     {
  42.         if (i == s) fout << 0 <<" ";
  43.         else
  44.         {
  45.             if (res[i] != 0) fout << res[i]<<" ";
  46.             else fout << -1 <<" ";
  47.         }
  48.     }
  49.     fout << "\n";
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement