Pagini recente » Cod sursa (job #1489309) | Cod sursa (job #2298460) | Cod sursa (job #1714687) | Cod sursa (job #431583) | Cod sursa (job #1656068)
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, *A[100005], Valid[100005], nr;
void DFS(int x)
{
int i;
for (i=1; i<=A[x][0]; i++)
{
if (Valid[A[x][i]]==0)
{
Valid[A[x][i]]=1;
DFS(A[x][i]);
}
}
}
int main()
{
int i, x, y;
fin>>n>>m;
for (i=1; i<=n; i++)
{
A[i] = (int *) realloc(A[i], sizeof(int));
A[i][0]=0;
}
for (i=1; i<=m; i++)
{
fin>>x>>y;
A[x][0]++;
A[x] = (int *) realloc(A[x], (A[x][0]+1)*sizeof(int));
A[x][A[x][0]]=y;
A[y][0]++;
A[y] = (int *) realloc(A[y], (A[y][0]+1)*sizeof(int));
A[y][A[y][0]]=x;
}
for (i=1; i<=n; i++)
{
if (Valid[i]==0)
{
nr++;
DFS(i);
}
}
fout<<nr;
return 0;
}