Mai intai trebuie sa te autentifici.
Cod sursa(job #1359740)
| Utilizator | Data | 25 februarie 2015 01:36:01 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 55 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.74 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> g[10005];
int n, m, x, y, nr=0;
bool viz[10005];
void dfs(int nod)
{
viz[nod]=1;
for (int i=0; i<g[nod].size(); i++)
if(!viz[g[nod][i]])
{
viz[g[nod][i]]=1;
dfs(g[nod][i]);
}
}
int main()
{
fin >> n >> m;
for (int i=1; i<=m; i++)
{
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i=1; i<=n; i++)
if(!viz[i])
{
dfs(i);
nr++;
}
fout << nr;
fin.close();
fout.close();
return 0;
}
