Pagini recente » Cod sursa (job #335994) | Cod sursa (job #257333) | Cod sursa (job #2878336) | Cod sursa (job #1553355) | Cod sursa (job #2382399)
#include<bits/stdc++.h>
using namespace std;
vector <int> v[100010];
int n,m,x,y,k;
bool viz[100010];
void dfs(int nod){
viz[nod]=1;
for(int i=0;i<v[nod].size();i++)
if(!viz[v[nod][i]])dfs(v[nod][i]);
}
int main(){
ifstream cin("dfs.in");
ofstream cout("dfs.out");
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(!viz[i]){
dfs(i);
k++;
}
cout<<k;
return 0;
}