SHARE
TWEET

Untitled

a guest Dec 9th, 2019 83 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.     cnt++;
  12.     for(int i = 0; i < g[x].size(); i++){
  13.         int y = g[x][i];
  14.         if(vis[y] == false)
  15.             dfs(y);
  16.     }
  17. }
  18.  
  19. void bfs(int x){
  20.     memset(vis, false, sizeof vis);
  21.     vis[x] = true;
  22.     queue < int > q;
  23.     q.push(x);
  24.     while(!q.empty()){
  25.         printf("%d ", q.front());
  26.         int atual = q.front();  q.pop();
  27.         for(int i = 0; i < g[atual].size(); i++){
  28.             int adj = g[atual][i];
  29.             if(!vis[adj]){
  30.                 vis[adj] = true;
  31.                 q.push(adj);
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40.     int t, inicial;
  41.     cin >> t;
  42.     while(t--){
  43.         cnt = 0;
  44.         memset(vis, 0, sizeof vis);
  45.         cin >> inicial;
  46.         scanf("%d %d", &n, &m);
  47.         for(int i = 0; i < m; i++){
  48.             scanf("%d %d", &a, &b);
  49.             g[a].push_back(b);
  50.             g[b].push_back(a);
  51.         }
  52.         dfs(inicial);
  53.         cnt--;
  54.         cout << cnt*2 << endl;
  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