Cod sursa(job #1731039)

Utilizator Andrei_PopaAndreiCDG Andrei_Popa Data 18 iulie 2016 10:30:56
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

queue<int> G[100001];
int n,m;
bool viz[100001];

void read()
{
    f>>n>>m;
    int a,b;
    for(int i=1;i<=m;i++)
    {
        f>>a>>b;
        G[a].push(b);
    }

}
void dfs(int source)
{
    while(!G[source].empty())
    {
        int t= G[source].front();
        G[source].pop();

        if(viz[t]==false)
        {
            viz[t]=true;
            dfs(t);
        }
    }
}

int nrComp;
int main()
{
    read();
    for(int i=1;i<=n;i++)
    if(viz[i]==false)
    {
        viz[i]=true;
        nrComp++;
        dfs(i);

    }

    g<<nrComp;
    f.close();
    g.close();
    return 0;
}