Pagini recente » Borderou de evaluare (job #376212) | Borderou de evaluare (job #3207231) | Cod sursa (job #1471333)
#include <iostream>
#include <cstdio>
using namespace std;
FILE *in, *out;
const int dmax = 1000;
int N,M;
bool A[dmax+1][dmax+1]; bool viz[dmax+1];
int NR;
void DFS(int nod)
{
viz[nod] = 1;
for(int i=1; i<=N; i++)
if(viz[i] == false && A[nod][i] == true) DFS(i);
}
void CITIRE()
{
in = fopen("dfs.in", "r");
out = fopen("dfs.out", "w");
fscanf(in, "%d %d", &N,&M);
for(int i=1; i<=M; i++)
{
int x,y;
fscanf(in, "%d %d", &x,&y);
A[x][y] = A[y][x] = true;
}
}
int main()
{
CITIRE();
for(int i=1; i<=N; i++)
if(viz[i] == false) { NR++; DFS(i); }
fprintf(out, "%d", NR);
return 0;
}