SHARE
TWEET

Untitled

a guest Dec 9th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 500
  4. #define inf 0x3F3F3F3F
  5. vector <int> g[maxn];
  6. bool vis[maxn];
  7. int n, m, a, b;
  8. int cnt = 0;
  9. void dfs(int x){
  10.     vis[x] = true;
  11.     cout << x << endl;
  12.     cnt++;
  13.     for(int i = 0; i < g[x].size(); i++){
  14.         int y = g[x][i];
  15.         if(vis[y] == false)
  16.             dfs(y);
  17.     }
  18. }
  19.  
  20. void bfs(int x){
  21.     memset(vis, false, sizeof vis);
  22.     vis[x] = true;
  23.     queue < int > q;
  24.     q.push(x);
  25.     while(!q.empty()){
  26.         printf("%d ", q.front());
  27.         int atual = q.front();  q.pop();
  28.         for(int i = 0; i < g[atual].size(); i++){
  29.             int adj = g[atual][i];
  30.             if(!vis[adj]){
  31.                 vis[adj] = true;
  32.                 q.push(adj);
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38.  
  39. int main()
  40. {
  41.     int t, inicial;
  42.     cin >> t;
  43.     while(t--){
  44.         cnt = 0;
  45.         memset(vis, 0, sizeof vis);
  46.         cin >> inicial;
  47.         scanf("%d %d", &n, &m);
  48.         for(int i = 0; i < m; i++){
  49.             scanf("%d %d", &a, &b);
  50.             g[a].push_back(b);
  51.             g[b].push_back(a);
  52.         }
  53.         dfs(inicial);
  54.         bfs(inicial);
  55.         for(int i = 0; i < n; i++)g[i].clear();
  56.     }
  57.     return 0;
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top