Pagini recente » Cod sursa (job #1158247) | Cod sursa (job #1366429) | Cod sursa (job #906152) | Cod sursa (job #2273172) | Cod sursa (job #2751259)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
bool mat[1001][1001], f[1001];
int n;
void dfs (int x)
{
f[x] = 1;
for (int i = 1; i <= n; i++)
if (mat[x][i] && f[i] == 0)
dfs(i);
}
int main()
{
int m;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
mat[x][y] = mat[y][x] = 1;
}
int componente = 0;
for (int i = 1; i <= n; i++)
{
if (f[i] == 0)
{
componente++;
dfs(i);
}
}
fout << componente;
return 0;
}