Cod sursa(job #1628203)
| Utilizator | Data | 3 martie 2016 21:52:37 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int x, y, i, j, n, m, nr;
vector <int> a[100005];
bool viz[100005];
void df(int nod)
{
viz[nod] = 1;
for (int i = 0; i < a[nod].size(); i++)
if (viz[a[nod][i]] == 0)
df(a[nod][i]);
}
int main()
{
f >> n >> m;
for (i = 1; i <= m; i++)
{
f >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
{
df(i);
nr++;
}
g << nr;
return 0;
}
