Pagini recente » Clasament simulare-cartita-30 | Clasament iconcurs10 | Cod sursa (job #2602339) | Cod sursa (job #1463173) | Cod sursa (job #3152773)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int const nMax=100005;
vector<int> l[nMax];
bool viz[nMax];
void dfs(int node){
viz[node] = true;
for(int vec:l[node]){
if(!viz[vec]){
dfs(vec);
}
}
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m;
cin>>n>>m;
while(m--){
int x,y;
cin>>x>>y;
l[x].push_back(y);
l[y].push_back(x);
}
int nrComp=0;
for(int i=1;i<=n;i++){
if(!viz[i]){
nrComp++;
dfs(i);
}
}
cout<<nrComp;
return 0;
}