Cod sursa(job #1317210)

Utilizator retrogradLucian Bicsi retrograd Data 14 ianuarie 2015 18:30:41
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include<fstream>
#include<algorithm>
#include<vector>
#include<queue>

#define NMAX 100001

using namespace std;

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

vector<int> G[NMAX];
bool VIZ[NMAX];
queue<int> Q;
int n;

void bfs(int s) {
    int nod;
    Q.push(s);
    VIZ[s] = 1;
    while(!Q.empty()) {
        nod = Q.front();
        Q.pop();
        for(vector<int>::iterator it=G[nod].begin(); it!=G[nod].end(); ++it) {
            if(!VIZ[*it]) {
                VIZ[*it] = 1;
                Q.push(*it);
            }
        }
    }
}

void read() {
    int m;
    fin>>n>>m;
    int a, b;
    while(m--) {
        fin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int cc = 0;
    for(int i=1; i<=n; i++) {
        if(!VIZ[i]) {
            bfs(i);
            cc++;
        }
    }
    fout<<cc;
}

int main() {
    read();
    return 0;
}