Cod sursa(job #1464248)

Utilizator dex4Darjan Catalin dex4 Data 22 iulie 2015 18:29:39
Problema Parcurgere DFS - componente conexe Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <string.h>
#define nmax 100005

using namespace std;

vector<int> G[nmax];
int T[nmax], viz[nmax], head, n, m, x, y, comps;

void build(){
    ifstream f("dfs.in");
    f >> n >> m;
    for(int i=1; i<=m; i++){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void bfs(int start){
    int i, j;
    memset(T, 0, sizeof(T));
    head = 1;
    viz[start] = 1;
    T[head] = start;
    for(i = 1; i <= head; i++)
        for(j = 0; j < G[T[i]].size(); j++){
            if(!viz[G[T[i]][j]]){
                T[++head] = G[T[i]][j];
                viz[G[T[i]][j]] = 1;

            }
        }

}

int main()
{
    ofstream g("dfs.out");
    build();
    for(int i=1; i<=n; i++)
        if(!viz[i]){
            comps++;
            bfs(i);
        }
    g << comps;
    return 0;
}