Cod sursa(job #914332)

Utilizator mihai995mihai995 mihai995 Data 14 martie 2013 01:42:27
Problema Parcurgere DFS - componente conexe Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>
using namespace std;

const int N = 100001;

vector<int> graph[N];
bool use[N];
int n;

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

void dfs(int x){
    use[x] = false;

    for (unsigned int i = 0 ; i < graph[x].size() ; i++)
        if (!use[ graph[x][i] ])
            dfs( graph[x][i] );
}

int main(){
    int m, x, y;

    in >> n >> m;

    while (m--){
        in >> x >> y;

        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    int nr = 0;

    for (int i = 1 ; i <= n ; i++)
        if (!use[i]){
            dfs(i);

            ++nr;
        }

    out << nr << "\n";

    return 0;
}