Advertisement
Falak_Ahmed_Shakib

bfs new

Oct 23rd, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. a to b
  2. //
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  9. typedef pair<ll,ll>pll;
  10. typedef pair<ll,pair<ll,ll>>plll;
  11. #define fi first
  12. #define se second
  13. #define pb push_back
  14. const int mod = 1e9+7;
  15. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  16. #define eb emplace_back
  17. int X[] = {-1,0,1,1,1,0,-1,-1};
  18. int Y[] = {-1,-1,-1,0,1,1,1,0};
  19. //int X[] = {-1,1,0,0};
  20. //int Y[] = {0,0,-1,1};
  21. char a[510][510];
  22. bool check(ll i,ll j)
  23. {
  24. ll n,m;
  25. if(i>=0 and j>=0 and i<n and j<m)
  26. return true;
  27. else
  28. return false;
  29. }
  30. bool iset(ll n, ll k)
  31. {
  32. if (n & (1ll << k))
  33. return true;
  34. else
  35. return false;
  36. }
  37. const int N=2e5+100;
  38. vector<ll>adj[N+1];
  39. ll n,m;
  40. ll u,v;
  41. bool f=true;
  42. bool vis[N];
  43. void bfs(ll s){
  44. queue<ll>q;
  45. q.push(s);
  46.  
  47. while(!q.empty()){
  48. ll x=q.front();
  49. cout<<x<<endl;
  50. vis[x]=true;
  51. q.pop();
  52. for(ll i=0;i<adj[x].size();i++){
  53. if(!vis[adj[x][i]]){
  54. q.push(adj[x][i]);
  55. vis[adj[x][i]]=true;
  56. }
  57. }
  58.  
  59. }
  60.  
  61.  
  62.  
  63. }
  64. int main()
  65. {
  66. fastread();
  67. cin>>n>>m;
  68.  
  69. for(ll i=0;i<m;i++){
  70. cin>>u>>v;
  71. adj[u].pb(v);
  72. adj[v].pb(u);
  73. }
  74.  
  75. for(ll i=0;i<n;i++){
  76. cout<<"node no : "<<i<<" -->";
  77. for(ll j=0;j<adj[i].size();j++){
  78. cout<<adj[i][j]<<" ";
  79. }cout<<endl;
  80. }
  81.  
  82.  
  83.  
  84. while(1){
  85. cin>>u>>v;
  86. bfs(u);
  87. if(vis[v])cout<<"yes"<<endl;
  88. else cout<<"no"<<endl;
  89. for(ll i=0;i<=N;i++)vis[i]=false;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. }
  97. /*
  98. */
  99.  
  100.  
  101.  
  102. ================================
  103. a to b with query
  104.  
  105. https://judge.u-aizu.ac.jp/onlinejudge/signin.jsp
  106. #include<bits/stdc++.h>
  107. using namespace std;
  108. typedef long long ll;
  109. typedef unsigned long long ull;
  110. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  111. typedef pair<ll,ll>pll;
  112. typedef pair<ll,pair<ll,ll>>plll;
  113. #define fi first
  114. #define se second
  115. #define pb push_back
  116. const int mod = 1e9+7;
  117. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  118. #define eb emplace_back
  119. int X[] = {-1,0,1,1,1,0,-1,-1};
  120. int Y[] = {-1,-1,-1,0,1,1,1,0};
  121. //int X[] = {-1,1,0,0};
  122. //int Y[] = {0,0,-1,1};
  123. char a[510][510];
  124. bool check(ll i,ll j)
  125. {
  126. ll n,m;
  127. if(i>=0 and j>=0 and i<n and j<m)
  128. return true;
  129. else
  130. return false;
  131. }
  132. bool iset(ll n, ll k)
  133. {
  134. if (n & (1ll << k))
  135. return true;
  136. else
  137. return false;
  138. }
  139. const int N=1e6+10;
  140. vector<ll>adj[N+1];
  141. ll p[N+10];
  142. ll n,m;
  143. ll u,v;
  144. bool f=true;
  145. bool vis[N];
  146. void bfs(ll s){
  147.  
  148.  
  149. queue<ll>q;
  150. q.push(s);
  151.  
  152. while(!q.empty()){
  153. ll x=q.front();
  154. //cout<<x<<endl;
  155. vis[x]=true;
  156. q.pop();
  157. for(ll i=0;i<adj[x].size();i++){
  158. if(!vis[adj[x][i]]){
  159. // cout<<"in "<<x<<" "<<adj[x][i]<<" "<<p[x]<<endl;
  160. q.push(adj[x][i]);
  161. p[adj[x][i]]=p[x];
  162. // cout<<p[adj[x][i]]<<endl;
  163. vis[adj[x][i]]=true;
  164. }
  165. }
  166.  
  167. }
  168.  
  169.  
  170.  
  171. }
  172. int main()
  173. {
  174. fastread();
  175. for(ll i=0;i<=N;i++){
  176. p[i]=i;
  177. }
  178. cin>>n>>m;
  179.  
  180. for(ll i=0;i<m;i++){
  181. cin>>u>>v;
  182. adj[u].pb(v);
  183. adj[v].pb(u);
  184. }
  185.  
  186. for(ll i=0;i<n;i++){
  187. // cout<<"node no : "<<i<<" -->";
  188. for(ll j=0;j<adj[i].size();j++){
  189. // cout<<adj[i][j]<<" ";
  190. }//cout<<endl;
  191. }
  192.  
  193.  
  194. for(ll i=0;i<n;i++){
  195. if(!vis[i])bfs(i);
  196. }
  197.  
  198.  
  199. for(ll i=0;i<n;i++){
  200. // cout<<i<<" "<<p[i]<<endl;
  201. }
  202.  
  203.  
  204. ll q;
  205. cin>>q;
  206.  
  207. while(q--){
  208. cin>>u>>v;
  209. if(p[u]==p[v])cout<<"yes"<<endl;
  210. else cout<<"no"<<endl;
  211. //for(ll i=0;i<=N;i++)vis[i]=false;
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. }
  219. /*
  220. */
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement