Cod sursa(job #209970)

Utilizator mircea_infoSuciu Mircea-Gabriel mircea_info Data 25 septembrie 2008 21:11:20
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <cstdio>
#include <stack>

using namespace std;

int v,x,y,nr,n,numara;
int viz[100];

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

nod *g[50];
stack <int> s;

void baga(int x, int y)
{
    nod *q=new nod;
    q->nr=y;
    q->urm=g[x];
    g[x]=q;
}

void read()
{
    freopen("dfs.in","r",stdin);
    scanf("%d%d%d",&v,&n,&nr);
    for(int i=0;i<nr;i++)
    {
        scanf("%d%d",&x,&y);
        baga(x,y);
        baga(y,x);
    }
}

void adauga(int v2)
{
    nod *x=g[v2];
    while(x)
    {
        if(viz[x->nr]==0)
        {
            s.push(x->nr);
            viz[x->nr]=1;
        }
        x=x->urm;
    }
}

void dfs()
{
    viz[v]=1;
    s.push(v);
    while(!s.empty())
    {
        int v2=s.top();
        numara++;
        s.pop();
        adauga(v2);
    }
}

int main()
{
    read();
    freopen("dfs.out","w",stdout);
    dfs();
    printf("%d\n",numara);
    fclose(stdout);
    return 0;
}