Cod sursa(job #1111101)
| Utilizator | Data | 18 februarie 2014 17:12:45 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <fstream>
#include<vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector<int> a[10];
int n, s=0;
bool viz[10];
void citire()
{
int m, i, x, y;
in>>n>>m;
for(i=1;i<=m;i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
viz[x]=true;
int y, i;
for(i=0;i<a[x].size();i++)
{
y=a[x][i];
if(!viz[y]) dfs(y);
}
}
int main()
{
int i;
citire();
for(i=1;i<=n;i++)
{
if(!viz[i])
{
dfs(i);
s++;
}
}
out<<s;
return 0;
}
