Pagini recente » Cod sursa (job #2377277) | Cod sursa (job #1891021) | Cod sursa (job #1643897) | Cod sursa (job #1501496) | Cod sursa (job #2113398)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
struct graf{
int nod;
int dist;
graf* next;
};
graf* v[100001];
int comp[100001];
int c=1;
int counter=0;
void add(int a, int b)
{
graf* q = new graf;
q->nod = b;
q->next = v[a];
v[a] = q;
}
void DFS(int node){
comp[node] = c;
graf *p;
p=v[node];
while(p!=NULL){
if(comp[p->nod] != c)
DFS(p->nod);
p = p->next;
}
}
int main()
{
int i,x,y;
int n,m;
fin >> n >> m;
for(i=1;i<=m;++i){
fin >> x >> y;
add(x,y);
add(y,x);
}
for(i=1;i<=n;++i){
if(comp[i]==0)
DFS(i),
++c;
}
fout << c - 1;
return 0;
}