Cod sursa(job #1717682)

Utilizator stefanchistefan chiper stefanchi Data 15 iunie 2016 16:02:33
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <stdio.h>
#include <vector>
#define N 100005
using namespace std;
int n, m;
vector <int> G[N];
bool vis[N];

void citire()
{
    int x , y;
    freopen("","r",stdin);
    freopen("","w",stdout);
    scanf("%d %d", &n , &m);
    for(int i = 0 ; i < m ; i++)
    {
        scanf("%d %d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void dfs(int x)
{
   vector <int> :: iterator it;
    vis[x] = true;

    for(it = g[x].begin(), it != g[x].end(), ++it)
    {
        if(!vis[*it])
            dfs(*it);
    }
}

void parcurgere()
{
    int raspuns;

    for(int i = 0 ; i <= n ; i++)
    {
        if(!vis[i])
        {
            dfs(i);
            raspuns++;
        }
    }
    printf("%d",raspuns);
}


int main()
{
     citire();
     parcurgere();
    return 0;
}