Advertisement
Okorosso

modues c++

Jun 4th, 2021
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. bool dfs(int v, vector<vector<int>> mat, vector<int> &used) {
  9.     used[v] = 1;
  10.     for (int i = 0; i < mat[v].size(); i++) {
  11.         if (used[mat[v][i]] == 0) {
  12.             dfs(mat[v][i], mat, used);
  13.             used[v] = 2;
  14.         }
  15.     }
  16.     used[v] = 2;
  17.     return true;
  18. }
  19.  
  20. int main() {
  21.     ifstream fin("input.txt");
  22.     ofstream fout("output.txt");
  23.  
  24.     int vertex, edges;
  25.     fin >> vertex >> edges;
  26.     vector<vector<int>> mat(vertex + 1, vector<int>());
  27.     vector<int> used(vertex + 1);
  28.  
  29.     for (int i = 1; i < edges + 1; i++) {
  30.         int a, b;
  31.         fin >> a >> b;
  32.         mat[a].push_back(b);
  33.         mat[b].push_back(a);
  34.     }
  35.  
  36.     int count = 0;
  37.     for (int i = 1; i < used.size(); i++) {
  38.         if (used[i] == 0) {
  39.             dfs(i, mat, used);
  40.             count++;
  41.         }
  42.     }
  43.  
  44.     fout << count;
  45.     return 0;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement