Pagini recente » Cod sursa (job #2550493) | Cod sursa (job #2965353) | Cod sursa (job #1577218) | Cod sursa (job #1348584) | Cod sursa (job #2256605)
#include <fstream>
using namespace std;
ifstream f("dfs.in"); ofstream g("dfs.out");
int a[1010][1010],viz[1010],n,m,cc;
void citire()
{ f>>n>>m;int x=0,y=0;
while(m)
{ f>>x>>y; a[x][y]=a[y][x]=1;
m--;
}
}
void depth_first(int vf)
{ viz[vf]=1;
for(int j=1;j<=n;j++)
if( a[vf][j] == 1 and viz[j] == 0 ) depth_first(j);
}
void afis()
{ for(int i=1;i<=n;i++)
{ for(int j=1;j<=n;j++) g<<a[i][j]<<" ";
g<<'\n';
}
}
int main()
{ citire();
for(int i=1;i<=n;i++)
if( viz[i] == 0 ) {depth_first(i); cc++;}
g<<cc;
g.close();
return 0;
}