Pagini recente » Cod sursa (job #1768991) | Cod sursa (job #1998826) | Cod sursa (job #2270259) | Cod sursa (job #3220729) | Cod sursa (job #1464247)
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;
int nrNodes, nrPairs, x, y, nrComps;
bool nCheck[nmax];
vector <int> graph[nmax];
void build(){
ifstream f("dfs.in");
f >> nrNodes >> nrPairs;
for(int i=1; i<=nrPairs; i++){
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void dfs(int x){
nCheck[x] = true;
for(int i=0; i<graph[x].size(); i++){
int y = graph[x][i];
if(!nCheck[y])
dfs(y);
}
}
int main()
{
build();
for(int i=1; i<=nrNodes; i++)
if(!nCheck[i]){
nrComps++;
dfs(i);
}
ofstream g("dfs.out");
g << nrComps << "\n";
return 0;
}