Advertisement
i_love_rao_khushboo

Untitled

Aug 8th, 2022
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define mp make_pair
  10. #define F first
  11. #define S second
  12. #define PI 3.1415926535897932384626
  13. #define sz(x) ((int)(x).size())
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<string> vs;
  23. typedef vector<pii> vpii;
  24. typedef vector<pll> vpll;
  25. typedef vector<vi> vvi;
  26. typedef vector<vll> vvll;
  27. typedef vector<vull> vvull;
  28. typedef vector<vb> vvb;
  29. typedef vector<vc> vvc;
  30. typedef vector<vs> vvs;
  31.  
  32. /************************************************** DEBUGGER *******************************************************************************************************/
  33.  
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  36. #else
  37. #define debug(x)
  38. #endif
  39.  
  40. void _print(ll t) { cerr << t; }
  41. void _print(int t) { cerr << t; }
  42. void _print(string t) { cerr << t; }
  43. void _print(char t) { cerr << t; }
  44. void _print(ld t) { cerr << t; }
  45. void _print(double t) { cerr << t; }
  46. void _print(ull t) { cerr << t; }
  47.  
  48. template <class T, class V> void _print(pair <T, V> p);
  49. template <class T> void _print(vector <T> v);
  50. template <class T> void _print(vector <vector<T>> v);
  51. template <class T> void _print(set <T> v);
  52. template <class T, class V> void _print(map <T, V> v);
  53. template <class T> void _print(multiset <T> v);
  54. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  55. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  57. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60.  
  61. /*******************************************************************************************************************************************************************/
  62.  
  63. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  64. int rng(int lim) {
  65.     uniform_int_distribution<int> uid(0,lim-1);
  66.     return uid(rang);
  67. }
  68.  
  69. const int INF = 0x3f3f3f3f;
  70. const int mod = 1e9+7;
  71.  
  72. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  73.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  74.                          
  75. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  76. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  77.  
  78. /******************************************************************************************************************************/
  79.  
  80. // stores the input graph
  81. vvi g;
  82.  
  83. // to keep track of visited vertices
  84. vb vis;
  85.  
  86. int n, m;
  87.  
  88. vi bfs(int src) {
  89.     // to store one of the many possible bfs traversal, using source(starting vertex) as src
  90.     vi res;
  91.    
  92.     // marking all nodes as unvisited initially
  93.     for(int i = 0; i < n; i++) vis[i] = 0;
  94.    
  95.     // queue to remember which vertex to visit next in case of dead end in iteration
  96.     queue<int> q;
  97.  
  98.     // initialisation of the bfs process pushing the src in queue
  99.     q.push(src);
  100.  
  101.     // marking a node visited as soon as it is pushed in q
  102.     vis[src] = 1;
  103.  
  104.     // iterative process
  105.     while(!q.empty()) {
  106.         // taking out vertex from q
  107.         int cur = q.front();
  108.  
  109.         // popping out this vertex from q
  110.         q.pop();
  111.        
  112.         // push v in the result
  113.         res.pb(cur);
  114.  
  115.        // visiting all the adjacent vertices of cur
  116.        for(auto x: g[cur]) {
  117.             // check if already visited or not
  118.             if(!vis[x]) {
  119.                 vis[x] = 1; // mark it visited
  120.                 q.push(x); // push it in the queue
  121.             }
  122.         }
  123.     }
  124.  
  125.     // return the final result of traversal
  126.     return res;
  127. }
  128.  
  129. void solve()
  130. {
  131.     cin >> n >> m;
  132.    
  133.     g.clear(); g.resize(n);
  134.     vis.clear(); vis.resize(n, 0);
  135.    
  136.     // 0-based vertices
  137.     for(int i = 0; i < m; i++) {
  138.         int x, y; cin >> x >> y;
  139.         g[x].pb(y);
  140.         g[y].pb(x);
  141.     }
  142.        
  143.     vi res = bfs(0);
  144.    
  145.     for(auto x: res) cout << x << " ";
  146.     cout << "\n";
  147. }
  148.  
  149. int main()
  150. {
  151.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  152.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  153.  
  154.     // #ifndef ONLINE_JUDGE
  155.     //     freopen("input.txt", "r", stdin);
  156.     //     freopen("output.txt", "w", stdout);
  157.     // #endif
  158.    
  159.     // #ifndef ONLINE_JUDGE
  160.     //      freopen("error.txt", "w", stderr);
  161.     // #endif
  162.    
  163.     int t = 1;
  164.     // int test = 1;
  165.     // cin >> t;
  166.     while(t--) {
  167.         // cout << "Case #" << test++ << ": ";
  168.         solve();
  169.     }
  170.  
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement