Pagini recente » Cod sursa (job #1026159) | Cod sursa (job #1105504) | Cod sursa (job #2706937) | Cod sursa (job #2099516) | Cod sursa (job #2282206)
#include <bits/stdc++.h>
using namespace std;
vector<int> E[100005];
bool D[100005];
void DFS(int k){
stack<int> Q;
Q.push(k);
D[k]=1;
while(!Q.empty()){
int X=Q.top();
for(auto it : E[X])
if(D[it]==0){
D[it]=1;
Q.push(it);
}
Q.pop();
}
}
int main(){
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;++i){
int st,dr;
scanf("%d %d",&st,&dr);
E[st].push_back(dr);
E[dr].push_back(st);
}
int ct=0;
for(int i=1;i<=n;++i){
if(D[i]==0){
++ct;
DFS(i);
}
}
printf("%d\n",ct);
return 0;
}