Pagini recente » Cod sursa (job #1457844) | Cod sursa (job #2428670) | Cod sursa (job #455937) | Cod sursa (job #562735) | Cod sursa (job #673375)
Cod sursa(job #673375)
#include<fstream>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n,m,viz[100001],cc;
struct nod{
int info;
nod *next;
};
nod *LA[100001];
void add(int x, int y){
nod *n=new(nod);
n->info=y;
n->next=LA[x];
LA[x]=n;
n=new(nod);
n->info=x;
n->next=LA[y];
LA[y]=n;
}
void citire(){
in>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
in>>x>>y;
add(x,y);
}
}
void df(int nodd){
//out<<nod<<" ";
nod *nc=LA[nodd];
while(nc){
int y=nc->info;
if(viz[y]==0){
//out<<i<<" ";
viz[y]=1;
df(y);
}
nc=nc->next;
}
}
int main(){
citire();
for(int i=1;i<=n;i++)
if(!viz[i]){
cc++;
viz[i]=1;
df(i);
}
out<<cc;
return 0;
}