Cod sursa(job #1447343)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 4 iunie 2015 08:30:11
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

vector <int> a[100005];
bool v[100005];

int n, m, nr;

inline void DFS(int k)
{
    v[k] = true;
    int l = a[k].size();
    for (int i = 0; i < l; i++)
        if (!v[a[k][i]])
            DFS(a[k][i]);
}

int main()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (!v[i])
        {
            DFS(i);
            nr++;
        }
    fout << nr << "\n";
    return 0;
}