Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- vector<vector<int>> mat;
- int vertex, edges;
- vector<int> used(vertex);
- bool dfs(int v) {
- used[v] = 1;
- for (int i = 0; i < mat[v].size(); i++) {
- if (used[mat[v][i]] == 0) {
- dfs(mat[v][i]);
- used[v] = 2;
- }
- }
- used[v] = 2;
- return true;
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- fin >> vertex >> edges;
- used.resize(vertex + 1);
- mat.resize(vertex + 1, vector<int>());
- for (int i = 1; i < edges + 1; i++) {
- int a, b;
- fin >> a >> b;
- mat[a].push_back(b);
- mat[b].push_back(a);
- }
- int count = 0;
- for (int i = 1; i < used.size(); i++) {
- if (used[i] == 0) {
- dfs(i);
- count++;
- }
- }
- fout << count;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement