Cod sursa(job #921851)

Utilizator vandrei95Zamfir Vlad vandrei95 Data 21 martie 2013 17:50:00
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <iostream>
#include<fstream>
using namespace std;
struct nod
{
    int vf;
    nod *next;
}*p[100003],*k,*nou;
int viz[100003];
int main()
{
    fstream f("dfs.in",ios::in), g("dfs.out",ios::out);
    int n,m,i,j,nrC=0,x,y;
    f>>n>>m;
    for(i=1;i<=n;i++)
    {
        f>>x>>y;
        nod *nou;
        nou=new nod;
        nou->vf=y;
        nou->next=p[x];
        p[x]=nou;
        nou=new nod;
        nou->vf=x;
        nou->next=p[y];
        p[y]=nou;
    }
    int vfcrt=1,gasit,vecin;
    do
    {
        nrC++;
        viz[vfcrt]=1;
        k=new nod;
        k->vf=vfcrt;
        k->next=NULL;
        while(k!=NULL)
        {
            vfcrt=k->vf;
            cout<<vfcrt<<" ";
            vecin=0;
            while(p[vfcrt]!=NULL)
                if(viz[p[vfcrt]->vf]==0)
                {
                    nou=new nod;
                    nou->vf=p[vfcrt]->vf;
                    viz[nou->vf]=1;
                    nou->next=k;
                    k=nou;
                    vecin=1;
                    break;
                }
                else
                    p[vfcrt]=p[vfcrt]->next;
                if(vecin==0)
                    k=k->next;
        }
        gasit=0;
        for(i=1;i<=n;i++)
            if(viz[i]==0)
            {
                gasit=1;
                vfcrt=i;
            }
    }
    while(gasit);
    g<<nrC;




}