Cod sursa(job #504891)

Utilizator mytzuskyMihai Morcov mytzusky Data 29 noiembrie 2010 13:42:53
Problema Parcurgere DFS - componente conexe Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include<fstream>

using namespace std;

int n,m,viz[100005];

struct graf{
    int vc;
    graf *urm;
}*L[100005];

void add(graf *&prim, int x)
{
    graf*p=new graf;
    p->vc=x;
    p->urm=prim;
    prim=p;
}


void citire()
{
    ifstream f("dfs.in");
    f>>n>>m;
    for(int i=0;i<m;i++)
    {
            int x,y;
            f>>x>>y;
            add(L[x],y);
            add(L[y],x);
    }
}

void dfs(int x)
{
    viz[x]=1;
    for(graf *p=L[x];p;p=p->urm)
        if(!viz[p->vc])
            dfs(p->vc);
}

int neviz()
{
    for(int i=1;i<=n;i++)
        if(viz[i]==0)
            return i;
    return 0;
}

int main()
{
    ofstream g("dfs.out");
    citire();
    int nc=0;
    while(neviz())
    {
        nc++;
        dfs(neviz());
    }
    g<<nc;
    return 0;
}