Cod sursa(job #1275965)

Utilizator hanganflorinHangan Florin hanganflorin Data 25 noiembrie 2014 20:41:10
Problema Parcurgere DFS - componente conexe Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("dfs.in");
ofstream os("dfs.out");

int n, m, x, y, nr;
bool b[101];
vector<int> G[101];

void Df(int x);

int main()
{
    is >> n >> m;
    while ( m-- )
    {
        is >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for ( int i = 1; i <= n; ++i )
    {
        if ( !b[i] )
        {
            Df(i);
            nr++;
        }
    }
    os << nr;
    is.close();
    os.close();
    return 0;
}
void Df(int x)
{
    b[x] = true;
    for ( int i = 0; i < G[x].size(); ++i )
        if ( !b[G[x][i]] )
        {
            b[G[x][i]] = true;
            Df(G[x][i]);
        }
}