Advertisement
splash365

dfs_string_map

Nov 18th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define     rep(i,k,n)      for(long long int i=k;i<n;i++)
  5. #define     fast            ios_base::sync_with_stdio(false);cin.tie(NULL)
  6. #define     read            freopen("input.txt","r",stdin)
  7. #define     write           freopen("output.txt","w",stdout)
  8. #define     D(x)            cout << '>' << #x << ':' << x << endl
  9. #define     DD(x,y)         cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << endl
  10. #define     DDD(x,y,z)      cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << ' ' << #z << ':' << z << endl
  11. #define     PI              acos(-1)
  12. #define     MP(x, y)        make_pair(x, y)
  13. #define     PB(x)           push_back(x)
  14. #define     ALL(p)          p.begin(),p.end()
  15. #define     CLR(p)          memset(p, 0, sizeof(p))
  16. #define     MEM(a, b)       memset(a, (b), sizeof(a))
  17. #define     ff              first
  18. #define     ss              second
  19. #define     sf              scanf
  20. #define     pf              printf
  21. #define     PII             pair<int, int>
  22. #define     ll              long long int
  23. #define     ull             unsigned long long int
  24.  
  25. inline int two(int n) { return 1 << n; }
  26. inline int test(int n, int b) { return (n>>b)&1; }
  27. inline void set_bit(int & n, int b) { n |= two(b); }
  28. inline void unset_bit(int & n, int b) { n &= ~two(b); }
  29. inline int last_bit(int n) { return n & (-n); }
  30. inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
  31.  
  32. template < class T > inline T gcd(T a, T b) {while(b) { a %= b; swap(a, b); } return a;}
  33. template < class T > inline T bigmod(T p, T e, T M){
  34.     ll ret = 1;
  35.     for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = (p * p) % M; }
  36.     return (T)ret;
  37. }
  38. template < class T > inline T power(T a, T n) {
  39.     if(n==0) return 1;
  40.     if(n==1) return a;
  41.     T R = power(a,n/2);
  42.     return (n%2==0) ? R*R : R*a*R;
  43. }
  44.  
  45. int fx4[] = {0, 0, -1, +1};
  46. int fy4[] = {+1, -1, 0, 0};
  47. int fx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
  48. int fy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
  49. int fx8Knight[] = {+2, +2, +1, -1, -2, -2, -1, +1};
  50. int fy8Knight[] = {+1, -1, -2, -2, -1, +1, +2, +2};
  51.  
  52.  
  53. const int MAXN = 107;
  54.  
  55. map<string, vector<string>> adj;
  56. map<string, bool> vis;
  57. map<string, int> dist;
  58. int N, E;
  59.  
  60.  
  61. void bfs(string src)
  62. {
  63.     queue<string> Q;
  64.     Q.push(src);
  65.     vis[src] = true;
  66.     dist[src] = 0;
  67.     while(!Q.empty())
  68.     {
  69.         string king = Q.front();
  70.         Q.pop();
  71.         cout << king << ' ';
  72.         for(auto v : adj[king])
  73.         {
  74.             if(!vis[v])
  75.             {
  76.                 vis[v] = true;
  77.                 dist[v] = dist[king] + 1;
  78.                 Q.push(v);
  79.             }
  80.         }
  81.     }
  82.     cout << "\n\n";
  83. }
  84.  
  85. int main() {
  86. #ifndef ONLINE_JUDGE
  87.     read;
  88.     write;
  89. #endif
  90.     fast;
  91.     cin >> N >> E;
  92.     for (int i = 0; i < E; i++)
  93.     {
  94.         string u, v;
  95.         cin >> u >> v;
  96.         adj[u].push_back(v);
  97.         adj[v].push_back(u);
  98.     }
  99.     bfs("shiary");
  100.     cout << dist["nawreen"] << "\n\n";
  101.     for(auto i : adj)
  102.     {
  103.         cout << i.first << ": ";
  104.         for(auto j : i.second)
  105.         {
  106.             cout << j << ' ';
  107.         }
  108.         cout << endl;
  109.     }
  110.     return 0;
  111. }
  112.  
  113.  
  114. //                           key       value
  115. // adj er ekta element, i = {shairy, {ruhan, sajid, abed}}
  116. // i.first = shiary
  117. // i.second.size() = 3;
  118. // i.second[0] = ruhan
  119. // i.second[1] = sajid
  120. // i.second[2] = abed
  121.  
  122. /*
  123. input:
  124. 7 7
  125. shiary ruhan
  126. shiary sajid
  127. ruhan mappy
  128. ruhan anika
  129. anika nawreen
  130. abed shiary
  131. mappy nawreen
  132. output:
  133. shiary ruhan sajid abed mappy anika nawreen
  134.  
  135. 3
  136.  
  137. abed: shiary
  138. anika: ruhan nawreen
  139. mappy: ruhan nawreen
  140. nawreen: anika mappy
  141. ruhan: shiary mappy anika
  142. sajid: shiary
  143. shiary: ruhan sajid abed
  144. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement