Advertisement
jeff69

Untitled

Apr 6th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. // ConsoleApplication211.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <functional>
  8. #include <algorithm>
  9. #include <math.h>
  10. #include <cmath>
  11. #include <string>
  12. #include <cstring>
  13. #include <vector>
  14. #include<set>
  15. #include<map>
  16. #include <time.h>
  17. #include <fstream>
  18. #include <queue>
  19. typedef long long ll;
  20. using namespace std;
  21. int n, m;
  22. vector<int> v[1000];
  23.  
  24. ll vis[1000];
  25. int main()
  26. {
  27. cin >> n >> m;
  28. for (int i = 0; i < m; i++){
  29. int x, y;
  30. scanf("%d %d", &x, &y);
  31. // cin >> x >> y;
  32. v[x].push_back(y);
  33. v[y].push_back(x);
  34. }
  35. int s;
  36. cin >> s;
  37.  
  38. memset(vis, -1, sizeof vis);
  39. queue <int> Q;
  40. Q.push(s);
  41. vis[s] = 0;
  42. int h1, h2;
  43. h1 = s;
  44. while (!Q.empty())
  45. {
  46. h1 = Q.front();
  47. Q.pop();
  48. for (int i = 0; i < v[h1].size(); i++){
  49. int nxt = v[h1][i];
  50. if (vis[nxt] == -1){
  51. vis[nxt] = vis[h1] + 1;
  52. Q.push(nxt);
  53. }
  54. }
  55.  
  56.  
  57.  
  58. }
  59. for (int i = 0; i < n; i++){
  60. printf("%d %d\n", i, vis[i]);
  61. }
  62.  
  63.  
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement