Cod sursa(job #728723)

Utilizator horeste12Stoianovici Horatiu Andrei horeste12 Data 28 martie 2012 22:04:04
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <cstdio>
#include <bitset>
#include <vector>
using namespace std;

vector <vector <int> > G(100010);
bitset <100010> v;
int n,m,nr;

void citire()
{
    scanf("%d %d\n",&n,&m);
    int a,b;
    while(m--)
    {
        scanf("%d %d\n",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
}

void DFS(int i)
{
    v.set(i);
    for(vector<int>::iterator j=G[i].begin();j<G[i].end();j++)
        if(!v[*j])
            DFS(*j);
}
int main()
{
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    citire();
    for(int i=1;i<=n;i++)
    {
        if(!v[i])
        {
            DFS(i);
            nr++;
        }
    }
    printf("%d\n",nr);
    return 0;
}