Pagini recente » Cod sursa (job #2024464) | Cod sursa (job #3305672) | Cod sursa (job #3344279) | Cod sursa (job #3037048) | Cod sursa (job #3338866)
#include <iostream>
#include <fstream>
using namespace std;
int n, m, nod;
bool A[101][101], viz[101];
int S[101], vf;
ifstream f("dfs.in");
ofstream g("dfs.out");
void citire()
{
int x, y;
f >> n >> m;
for(int i = 1; i <= m; i++)
{
f >> x >> y;
A[x][y] = A[y][x] = 1;
}
}
void DFS(int nod)
{
vf = 1;
S[1] = nod;
viz[nod] = 1;
while(vf > 0)
{
int crt = S[vf];
bool ok = 0;
for(int i = 1; i <= n; i++)
if(A[crt][i] == 1 && viz[i] == 0)
{
S[++vf] = i;
viz[i] = 1;
ok = 1;
break;
}
if(ok == 0)
vf--;
}
}
int main()
{
citire();
int comp = 0;
for(int i = 1; i <= n; i++)
if(viz[i] == 0)
{
comp++;
DFS(i);
}
g << comp;
f.close();
g.close();
return 0;
}