Cod sursa(job #1733792)

Utilizator HuskyezTarnu Cristian Huskyez Data 25 iulie 2016 19:06:43
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");

typedef struct node{
    int name;
    node* next;
}*nodep;

nodep graphList[100005];
bool trecut[100005];

int n, m;
int x, y;
int conex;

void add(nodep &n, int val)
{
    nodep p = new node;
    p->name = val;
    p->next = n;
    n = p;
}

void DFS(int poz)
{
     trecut[poz] = true;
     for(nodep i=graphList[poz]; i!= NULL; i=i->next){
        if(!trecut[i->name]){
            DFS(i->name);
        }
     }
}

int main()
{
    in >> n >> m;
    for(int i=0; i<m; i++){
        in >> x >> y;
        add(graphList[x], y);
        add(graphList[y], x);
    }

    for(int j=0; j<n; j++){
        if(!trecut[j]){
            conex++;
            DFS(j);
        }
    }

    out << conex << '\n';

    return 0;
}