Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 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> graph[30005];
  9. int res[30005];
  10.  
  11. int main()
  12. {
  13.     ifstream fin("pathbge1.in");
  14.     ofstream fout("pathbge1.out");
  15.     int n, m, x, y;
  16.     fin >> n >> m;
  17.  
  18.     for (int i = 0; i < m; i++)
  19.     {
  20.         fin >> x >> y;
  21.         graph[x].push_back(y);
  22.         graph[y].push_back(x);
  23.     }
  24.     int s = 1;
  25.     queue<int> q;
  26.     q.push(s);
  27.     int u;
  28.     while (!q.empty())
  29.     {
  30.         u = q.front();
  31.         q.pop();
  32.         for (int v = 0; v < graph[u].size(); v++)
  33.         {
  34.             if (!res[graph[u][v]] && graph[u][v]!=s)
  35.             {
  36.                 res[graph[u][v]] = res[u] + 1;
  37.                 q.push(graph[u][v]);
  38.             }
  39.         }
  40.     }
  41.  
  42.     for (int i = 1; i <= n; i++)
  43.         fout << res[i] << " ";
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement