StoneHaos

16

Nov 29th, 2021
708
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int g[4][4] = {
  6.     {0, 1, 1, 0},
  7.     {1, 0, 0, 0},
  8.     {1, 0, 0, 1},
  9.     {0, 0, 1, 0},
  10. };
  11. int used[4];
  12.  
  13. void DFS(int v) {
  14.     cout << v + 1 << " ";
  15.     used[v] = true;
  16.     for (int j = 0; j < 4; ++ j) {
  17.         if (g[v][j] != 0 && !used[j]) DFS(j);
  18.     }
  19. }
  20.  
  21. int main() {
  22.     for (int i = 0; i < 4; ++ i)
  23.         if (!used[i]) {
  24.             DFS(i);
  25.             cout << endl;
  26.         }
  27.     return 0;
  28. }
RAW Paste Data