Advertisement
STANAANDREY

dfs

Jan 9th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define MAXPEEKSNR 100
  5. int peeks, links;
  6. vector<int> graph[MAXPEEKSNR];
  7. vector<bool> visited;
  8.  
  9. int dfs(int node)
  10. {
  11.     int visnr = 1;
  12.     visited[node] = true;
  13.     for (int i = 0; i < (int)graph[node].size(); i++)
  14.         if (!visited[graph[node][i]])
  15.             visnr += dfs(graph[node][i]);
  16.     return visnr;
  17. }
  18.  
  19. int main()
  20. {
  21.     cin >> peeks >> links;
  22.     int node;
  23.     visited.resize(peeks + 1);
  24.     for (int i = 0; i < links; i++)
  25.     {
  26.         int x, y;
  27.         cin >> x >> y;
  28.         graph[x].push_back(y);
  29.         graph[y].push_back(x);
  30.         node = x;
  31.     }
  32.  
  33.     int visnr = dfs(node);
  34.     cout << endl << visnr;
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement