Pagini recente » Cod sursa (job #2747255) | Cod sursa (job #2794826) | Cod sursa (job #2857932) | Cod sursa (job #1973799) | Cod sursa (job #2612067)
#include<fstream>
#include<iostream>
#include<vector>
#define MAX_VERTICES 100000
using namespace std;
vector<int>g[MAX_VERTICES + 1];
bool used[MAX_VERTICES + 1];
int n, m, nrc;
void readGraph() {
int x, y;
ifstream fin("dfs.in");
fin >> n >> m;
for(int i = 1; i <= m; i++) {
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
//cout << x << " " << y << "\n";
}
fin.close();
}
void DFS(int node) {
used[node] = 1;
for(auto i : g[node]) {
if(!used[i])
DFS(i);
}
}
void DFS_master() {
for(int i = 1; i <= n; i++) {
if(!used[i]) {
nrc++;
DFS(i);
}
}
}
void printNrc() {
ofstream fout("dfs.out");
fout << nrc << "\n";
fout.close();
}
int main() {
readGraph();
DFS_master();
printNrc();
return 0;
}