Advertisement
Guest User

Connectivity

a guest
Apr 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. int n, m, cnt = 0, a, b, v, arr[101][101], lamps[101];
  10.  
  11. void dfs(int v)
  12. {
  13.     cnt++;
  14.     lamps[v] = 1;
  15. //  cout << v << " ";
  16.    
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         if(arr[v][i] == 1 && lamps[i] == 0)
  20.         {
  21.             dfs(i);
  22.         }
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     //--------------------------------
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(0);
  31.     cout.tie(0);
  32.     //--------------------------------
  33.    
  34.     cin >> n >> m;
  35.    
  36.     for (int i = 0; i < m; i++)
  37.     {
  38.         cin >> a >> b;
  39.         arr[a-1][b-1] = arr[b-1][a-1] = 1;
  40.     }
  41.    
  42.     dfs(0);
  43.    
  44.     (cnt == n) ? (cout << "YES") : (cout << "NO");
  45.    
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement