Cod sursa(job #1016032)

Utilizator Athena99Anghel Anca Athena99 Data 25 octombrie 2013 17:02:25
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax= 100000;

vector <int> v[nmax+1];
bool u[nmax+1];

void update( int p ) {
    u[p]= 1;
    for ( int i= 0; i<(int)v[p].size(); ++i ) {
        if ( u[v[p][i]]==0 ) {
            update(v[p][i]);
        }
    }
}

int main(  ) {
    int n, m;
    fin>>n>>m;
    for ( int i= 1; i<=n; ++i ) {
        int x, y;
        fin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    int sol= 0;
    for ( int i= 1; i<=n; ++i ) {
        if ( u[i]==0 ) {
            update(i);
            ++sol;
        }
    }

    fout<<sol<<"\n";

    return 0;
}