Pagini recente » Cod sursa (job #261081) | Cod sursa (job #2422588) | Cod sursa (job #660282) | Cod sursa (job #2804832) | Cod sursa (job #2438217)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n,x,y,a[102][102],nrc,v1[102],viz[102],i,j,m;
void comp(int x)
{
int i;
viz[x] = 1;
for(i = 1 ; i <= a[x][0]; ++i)
if(viz[a[x][i]] == 0)
comp(a[x][i]);
}
int main()
{
f >> n >> m;
for(i = 1 ; i <= m ; ++i)
{ f >> x >> y;
a[x][0]++;
a[x][a[x][0]] = y;
a[y][0]++;
a[y][a[y][0]] = x;
}
for(i = 1 ; i <= n ; ++i)
if(viz[i] == 0){
nrc++;
v1[nrc] = i;
comp(i);
}
g << nrc;
return 0;
}