Pagini recente » Cod sursa (job #1121373) | Cod sursa (job #2251738) | Cod sursa (job #1657208) | Cod sursa (job #1256936) | Cod sursa (job #2055368)
#include <fstream>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n, m, viz[100005], cnt;
struct nod
{
int x;
nod *urm;
} ;
nod *p[100005];
void adaug(int z,nod *&p)
{
nod *t;
t = new nod;
t -> x = z;
t -> urm = p;
p=t;
}
void citire()
{
in>>n>>m;
int i, x, y;
for (i = 1; i <= m; i++)
{
in>>x>>y;
adaug(y,p[x]);
adaug(x,p[y]);
}
}
void DFS(int x)
{
nod *q;
viz[x] = 1;
for (q = p[x]; q != NULL; q = q -> urm)
if (!viz[q -> x]) DFS(q -> x);
}
int main()
{
citire();
int i;
for (i = 1; i <= n; i++) if (!viz[i])
{
cnt++;
DFS(i);
}
out<<cnt;
return 0;
}