Cod sursa(job #768638)
| Utilizator | Data | 17 iulie 2012 15:47:20 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.44 kb |
# include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int N,M,A[1001][1001],viz[1001],c=1;
void citire()
{
f>>N>>M;
while (!f.eof())
{
int x,y;
f>>x>>y;
A[x][y]=1;
A[y][x]=1;
f.close();
}
}
void DFS(int x,int& c)
{
viz[x]=1;
for (int i=1; i<=N; i++)
if(A[x][i]==1 && viz[i]==0)
{
c++;
DFS(i);
}
}
int main()
{
citire();
DFS(1,c);
if(c!=1)
g<<c;
g.close();
return 0;
}
