Cod sursa(job #1111210)

Utilizator vlad.rusu11Rusu Vlad vlad.rusu11 Data 18 februarie 2014 18:30:09
Problema Parcurgere DFS - componente conexe Scor 85
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#define NMax 100000
using namespace std;

struct nod  {
                int key;
                nod *next;
            };

nod start[NMax], *p;
int n, m, i, x, y, comp, vis[NMax];

void DF(int k)
{
    nod *p;
    p = start[k].next;

    while(p)
    {
        if(! vis[p->key])
        {
            vis[p->key] = comp;
            DF(p->key);
        }
        p = p->next;
    }
}

int main()
{
    ifstream fin("dfs.in");
    ofstream fout("dfs.out");

    fin >> n >> m;

    for(i=1 ; i<=m ; ++i)
    {
        fin >> x >> y;

        p = new nod;
        p->key = y;
        p->next = start[x].next;
        start[x].next = p;

        p = new nod;
        p->key = x;
        p->next = start[y].next;
        start[y].next = p;
    }

    for(i=1 ; i<=n ; ++i)
        if(! vis[i])
        {
            ++comp;
            vis[i] = comp;
            DF(i);
        }

    fout << comp;

    return 0;
}