Pagini recente » Cod sursa (job #1361740) | Cod sursa (job #17584) | Cod sursa (job #1275926) | Cod sursa (job #1239344) | Cod sursa (job #1666085)
#include<fstream>
using namespace std;
int y,x,rez;
bool viz[100005];
ifstream cin("dfs.in");
ofstream cout("dfs.out");
typedef struct nod{
int info;
nod* next;
} *graf;
graf A[100005];
void add(graf &p,int x){
graf m;
m= new nod;
m->info=x;
m->next=p;
p=m;
}
void dfs(int x){
graf p;
viz[x]=1;
for(p=A[x];p!=NULL;p=p->next){
if(!viz[p->info])dfs(p->info);
}
}
int main(){
int n,m;
cin>>n>>m;
while(m--){
cin>>x>>y;
add(A[x],y);
add(A[y],x);
}
for(int i=1;i<=n;i++){if(!viz[i]){rez++;dfs(i);}}
cout<<rez;
}