Paste Search Dynamic
Untitled
  1. /*
  2.        Wake up to reality !
  3.        Nothing ever goes as planned in this accursed world.
  4.        The longer you live, the more you realize
  5.        that in this reality only pain, suffering and futility exist.
  6.        Wherever there is light, there are also shadows.
  7.        As long as the concept of winners exists, there must also be losers.
  8.        The selfish desire of wanting to maintain peace causes wars,
  9.        and hatred is born to protect love.
  10.                  
  11.        ~ Madara Uchiha
  12. */
  13. #include <iostream>
  14. #include <string>
  15. #include <vector>
  16. #include <algorithm>
  17. #include <sstream>
  18. #include <queue>
  19. #include <deque>
  20. #include <bitset>
  21. #include <iterator>
  22. #include <list>
  23. #include <stack>
  24. #include <map>
  25. #include <set>
  26. #include <functional>
  27. #include <numeric>
  28. #include <utility>
  29. #include <limits>
  30. #include <time.h>
  31. #include <math.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <assert.h>
  36. #include <climits>
  37. #include <iomanip>
  38. using namespace std;
  39. typedef long long ll;
  40. typedef long double ld;
  41. #define mod 1000000007
  42. typedef vector<int> vi;
  43. typedef pair<int, int> pi;
  44. #define inf 1e17
  45. #define endl 'n'
  46.  
  47. vector<int> arr[10];
  48. vector<int> visited(10, 0);
  49. vector<int> level(10);
  50.  
  51. // 7 6
  52. // 1 2
  53. // 1 3
  54. // 2 4
  55. // 2 5
  56. // 3 6
  57. // 6 7
  58.  
  59. void bfs(int num)
  60. {
  61.     visited[num] = 1;
  62.     queue<int> q;
  63.     q.push(num);
  64.     level[num] = 0;
  65.     while (!q.empty())
  66.     {
  67.         int d = q.front();
  68.         q.pop();
  69.         for (int i = 0; i < arr[d].size(); i++)
  70.         {
  71.             if (visited[arr[d][i]] == 0)
  72.             {
  73.                 visited[arr[d][i]] = 1;
  74.                 level[arr[d][i]] = 1 + level[d];
  75.                 q.push(arr[d][i]);
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. void solve()
  82. {
  83.     int total = 0, total2 = 0, count = 0, count2 = 0, flag = 0;
  84.     int n;
  85.     cin >> n;
  86.     int e;
  87.     cin >> e;
  88.  
  89.     for (int i = 0; i < e; i++)
  90.     {
  91.         int a;
  92.         cin >> a;
  93.         int b;
  94.         cin >> b;
  95.         arr[a].push_back(b);
  96.         arr[b].push_back(a);
  97.     }
  98.     bfs(1);
  99.     for (int i = 1; i <= n; i++)
  100.     {
  101.         cout << i << " " << level[i] << endl;
  102.     }
  103. }
  104.  
  105. int main()
  106. {
  107.     ios::sync_with_stdio(false);
  108.     cin.tie(0);
  109.     cout.tie(0);
  110.     int t = 1;
  111.     // cin >> t;
  112.     while (t--)
  113.     {
  114.         solve();
  115.     }
  116. }
Parsed in 0.012 seconds