Cod sursa(job #2028545)

Utilizator rangal3Tudor Anastasiei rangal3 Data 28 septembrie 2017 00:16:15
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>
#define in "dfs.in"
#define out "dfs.out"
#define N 100003

using namespace std;

ifstream fin(in);
ofstream fout(out);

int n,m,x,y;
bool f[N];

vector<int> v[N];

inline void DFS(int nod)
{
    f[nod] = 1;

    vector<int>::iterator it;
    for(it = v[nod].begin(); it != v[nod].end(); ++it)
        if(!f[*it])
            DFS(*it);
}

int main()
{
    fin>>n>>m;
    while(m--)
    {
        fin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    int sol = 0;

    for(int i=1; i<=n; ++i)
        if(!f[i])
            DFS(i),++sol;
    fout<<sol<<"\n";

    fin.close(); fout.close();
    return 0;
}