Cod sursa(job #920625)

Utilizator baicuviorelBaicu Viorel baicuviorel Data 20 martie 2013 16:03:13
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>

using namespace std;
ifstream in ("dfs.in");
ofstream out ("dfs.out");
struct nod {int inf; nod *next;} *L[100001];
int n,m,used[100001],st[100001],k;
void ins(nod *&first , int inf)
{
    nod *p = new nod;
    p->inf = inf;
    p->next = first;
    first = p;
}
/*void l()
{
    for(int i=1;i<=k;i++)
    {
        nod *p=new nod;
        p=L[st[i]];
        while(p!=0)
        {
            int current_nod = p->inf;
            if(used[current_nod]==0)
            {
                st[++k]=current_nod;
                used[current_nod]++;
            }
            p=p->next;
        }
    }
}*/
void l(int x)
{
    used[x]=1;
    nod * p =L[x];
    while(p!=0)
    {
        int c=p->inf;
        if(used[c]==0)
            l(c);
        p=p->next;
    }
}
int main()
{
    in>>n>>m;
    for(int i(1);i<=m;i++)
    {
        int x,y;
        in>>x>>y;
        ins(L[x],y);
        ins(L[y],x);
    }
    int Y=0;
    for(int i(1);i<=n;i++)
    if(used[i]==0){Y++; l(i);}
    out<<Y;
    return 0;
}