Cod sursa(job #2081617)

Utilizator armand200Armand Cismaru armand200 Data 4 decembrie 2017 21:33:21
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

#define pb push_back;

ifstream f ("dfs.in");
ofstream g ("dfs.out");

vector <int> l[200000];
vector <int> :: iterator it;

double n,m;
bool sel[200000];

void load ()
{
    double i,x,y;
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        l[x].push_back(y);
        l[y].push_back(x);
    }
}

void dfs ( int x )
{
    vector <int> :: iterator it;
    sel[x]= true;
    for( it=l[x].begin(); it!=l[x].end(); it++ )
        if ( !sel[*it] )
            dfs (*it);
}

void print ()
{
    double nc=0,i;

    for(i=1; i<=n; i++)
        if(!sel[i])
        {
            nc++;
            dfs(i);
        }
    g<<nc;
}

int main()
{
    load();
    print ();

    return 0;
}