Cod sursa(job #773402)

Utilizator gicu_01porcescu gicu gicu_01 Data 1 august 2012 17:06:03
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");

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

void dfs(int nod)
{
    int i,t;
    b[nod]=true;
    t=a[nod].size();
    for (i=0; i<t; i++)
     if ( !b[ a[nod][i] ] ) dfs( a[nod][i] );
}

int main()
{
    int n,m,i,x,y,rez=0;;

    in>>n>>m;
    for (i=1; i<=m; i++)
    {
        in>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    for (i=1; i<=n; i++)
    if (!b[i])
    {
        rez++;
        dfs(i);
    }

    out<<rez<<"\n";

    in.close();
    out.close();
    return 0;
}