Cod sursa(job #505830)

Utilizator mytzuskyMihai Morcov mytzusky Data 4 decembrie 2010 10:44:06
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>


using namespace std;

int n,m,viz[100001],nct;

struct nod{
    int inf;
    nod *urm;
};

nod *g[100001];

void add(int x,int y)
{
    nod *aux=new nod;
    aux->inf=y;
    aux->urm=g[x];
    g[x]=aux;
}

void citire()
{
    ifstream f("dfs.in");

    f>>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y;
        f>>x>>y;
        add(x,y);
    }
}

void dfs(int x)
{
    for(nod *p=g[x];p;p=p->urm)
        if(!viz[p->inf])
        {
            viz[p->inf]=1;
            dfs(p->inf);
        }

}


int main()
{
    ofstream g("dfs.out");
    citire();

    for(int i=1;i<=n;i++)
        if(!viz[i])
        {
            nct++;
            viz[i]=1;
            dfs(i);
        }

    g<<nct;

    return 0;
}