Cod sursa(job #2501949)

Utilizator DanSDan Teodor Savastre DanS Data 30 noiembrie 2019 12:07:27
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>

using namespace std;

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

const int N = 100001;
const int M = 2*N;
int lst[N], vf[2*M], urm[2*M], nr;
int viz[N], n, m, x, y, ncomp;

void adauga(int x, int y){
    vf[++nr] = y;
    urm[nr] = lst[x];
    lst[x] = nr;
}

void dfs(int x){
    viz[x] = 1;
}

int main()
{
    in>>n>>m;
    for(int i=1; i<=m; i++)
    {
        in>>x>>y;
        adauga(x, y);
    }
    for(int x=1; x<=n; x++)
    {
        for(int p=lst[x]; p!=0; p=urm[p])
        {
            int y = vf[p];
            if(viz[y] == 0)
            {
                ncomp++;
                dfs(y);
            }
        }
    }
    out<<ncomp;
    return 0;
}